Class: Chef::DataBagItem
Constant Summary
collapse
- VALID_ID =
/^[\.\-[:alnum:]_]+$/
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#lazy, #set_or_return, #validate
#class_from_file, #from_file
Constructor Details
#initialize(chef_server_rest: nil) ⇒ DataBagItem
Create a new Chef::DataBagItem
55
56
57
58
59
|
# File 'lib/chef/data_bag_item.rb', line 55
def initialize(chef_server_rest: nil)
@data_bag = nil
@raw_data = Mash.new
@chef_server_rest = chef_server_rest
end
|
Instance Attribute Details
#chef_server_rest ⇒ Object
Returns the value of attribute chef_server_rest.
41
42
43
|
# File 'lib/chef/data_bag_item.rb', line 41
def chef_server_rest
@chef_server_rest
end
|
#raw_data ⇒ Object
Returns the value of attribute raw_data.
52
53
54
|
# File 'lib/chef/data_bag_item.rb', line 52
def raw_data
@raw_data
end
|
Class Method Details
.chef_server_rest ⇒ Object
65
66
67
|
# File 'lib/chef/data_bag_item.rb', line 65
def self.chef_server_rest
Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
|
.from_hash(h) ⇒ Object
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/chef/data_bag_item.rb', line 128
def self.from_hash(h)
h.delete("chef_type")
h.delete("json_class")
item = new
item.data_bag(h.delete("data_bag")) if h.key?("data_bag")
if h.key?("raw_data")
item.raw_data = Mash.new(h["raw_data"])
else
item.raw_data = h
end
item
end
|
.json_create(o) ⇒ Object
Create a Chef::DataBagItem from JSON
143
144
145
146
|
# File 'lib/chef/data_bag_item.rb', line 143
def self.json_create(o)
Chef.log_deprecation("Auto inflation of JSON data is deprecated. Please use Chef::DataBagItem#from_hash")
from_hash(o)
end
|
.load(data_bag, name) ⇒ Object
Load a Data Bag Item by name via either the RESTful API or local data_bag_path if run in solo mode
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/chef/data_bag_item.rb', line 149
def self.load(data_bag, name)
if Chef::Config[:solo_legacy_mode]
bag = Chef::DataBag.load(data_bag)
raise Exceptions::InvalidDataBagItemID, "Item #{name} not found in data bag #{data_bag}. Other items found: #{bag.keys.join(", ")}" unless bag.include?(name)
item = bag[name]
else
item = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("data/#{data_bag}/#{name}")
end
if item.kind_of?(DataBagItem)
item
else
item = from_hash(item)
item.data_bag(data_bag)
item
end
end
|
.object_name(data_bag_name, id) ⇒ Object
105
106
107
|
# File 'lib/chef/data_bag_item.rb', line 105
def self.object_name(data_bag_name, id)
"data_bag_item_#{data_bag_name}_#{id}"
end
|
.validate_id!(id_str) ⇒ Object
43
44
45
46
47
|
# File 'lib/chef/data_bag_item.rb', line 43
def self.validate_id!(id_str)
if id_str.nil? || ( id_str !~ VALID_ID )
raise Exceptions::InvalidDataBagItemID, "Data Bag items must have an id matching #{VALID_ID.inspect}, you gave: #{id_str.inspect}"
end
end
|
Instance Method Details
#==(other) ⇒ Object
193
194
195
196
197
198
|
# File 'lib/chef/data_bag_item.rb', line 193
def ==(other)
other.respond_to?(:to_hash) &&
other.respond_to?(:data_bag) &&
(other.to_hash == to_hash) &&
(other.data_bag.to_s == data_bag.to_s)
end
|
#create ⇒ Object
Create this Data Bag Item via RESTful API
188
189
190
191
|
# File 'lib/chef/data_bag_item.rb', line 188
def create
chef_server_rest.post("data/#{data_bag}", self)
self
end
|
#data_bag(arg = nil) ⇒ Object
85
86
87
88
89
90
91
|
# File 'lib/chef/data_bag_item.rb', line 85
def data_bag(arg = nil)
set_or_return(
:data_bag,
arg,
:regex => /^[\-[:alnum:]_]+$/
)
end
|
#destroy(data_bag = self.data_bag(), databag_item = name) ⇒ Object
167
168
169
|
# File 'lib/chef/data_bag_item.rb', line 167
def destroy(data_bag = self.data_bag(), databag_item = name)
chef_server_rest.delete("data/#{data_bag}/#{databag_item}")
end
|
#id ⇒ Object
213
214
215
|
# File 'lib/chef/data_bag_item.rb', line 213
def id
@raw_data["id"]
end
|
#inspect ⇒ Object
205
206
207
|
# File 'lib/chef/data_bag_item.rb', line 205
def inspect
"data_bag_item[#{data_bag.inspect}, #{raw_data['id'].inspect}, #{raw_data.inspect}]"
end
|
#name ⇒ Object
93
94
95
|
# File 'lib/chef/data_bag_item.rb', line 93
def name
object_name
end
|
#object_name ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/chef/data_bag_item.rb', line 97
def object_name
raise Exceptions::ValidationFailed, "You must have an 'id' or :id key in the raw data" unless raw_data.has_key?("id")
raise Exceptions::ValidationFailed, "You must have declared what bag this item belongs to!" unless data_bag
id = raw_data["id"]
"data_bag_item_#{data_bag}_#{id}"
end
|
#pretty_print(pretty_printer) ⇒ Object
209
210
211
|
# File 'lib/chef/data_bag_item.rb', line 209
def pretty_print(pretty_printer)
pretty_printer.pp({ "data_bag_item('#{data_bag}', '#{id}')" => self.to_hash })
end
|
#save(item_id = ) ⇒ Object
Save this Data Bag Item via RESTful API
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/chef/data_bag_item.rb', line 172
def save(item_id = @raw_data["id"])
r = chef_server_rest
begin
if Chef::Config[:why_run]
Chef::Log.warn("In why-run mode, so NOT performing data bag item save.")
else
r.put("data/#{data_bag}/#{item_id}", self)
end
rescue Net::HTTPServerException => e
raise e unless e.response.code == "404"
r.post("data/#{data_bag}", self)
end
self
end
|
#to_hash ⇒ Object
109
110
111
112
113
114
|
# File 'lib/chef/data_bag_item.rb', line 109
def to_hash
result = self.raw_data.dup
result["chef_type"] = "data_bag_item"
result["data_bag"] = self.data_bag.to_s
result
end
|
#to_json(*a) ⇒ Object
Serialize this object as a hash
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/chef/data_bag_item.rb', line 117
def to_json(*a)
result = {
"name" => object_name,
"json_class" => self.class.name,
"chef_type" => "data_bag_item",
"data_bag" => data_bag,
"raw_data" => raw_data,
}
Chef::JSONCompat.to_json(result, *a)
end
|
#to_s ⇒ Object
201
202
203
|
# File 'lib/chef/data_bag_item.rb', line 201
def to_s
"data_bag_item[#{id}]"
end
|
#validate_id!(id_str) ⇒ Object
73
74
75
|
# File 'lib/chef/data_bag_item.rb', line 73
def validate_id!(id_str)
self.class.validate_id!(id_str)
end
|