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
54
55
56
57
58
|
# File 'lib/chef/data_bag_item.rb', line 54
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.
40
41
42
|
# File 'lib/chef/data_bag_item.rb', line 40
def chef_server_rest
@chef_server_rest
end
|
#raw_data ⇒ Object
Returns the value of attribute raw_data.
51
52
53
|
# File 'lib/chef/data_bag_item.rb', line 51
def raw_data
@raw_data
end
|
Class Method Details
.chef_server_rest ⇒ Object
64
65
66
|
# File 'lib/chef/data_bag_item.rb', line 64
def self.chef_server_rest
Chef::REST.new(Chef::Config[:chef_server_url])
end
|
.from_hash(h) ⇒ Object
127
128
129
130
131
|
# File 'lib/chef/data_bag_item.rb', line 127
def self.from_hash(h)
item = new
item.raw_data = h
item
end
|
.json_create(o) ⇒ Object
Create a Chef::DataBagItem from JSON
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/chef/data_bag_item.rb', line 134
def self.json_create(o)
bag_item = new
bag_item.data_bag(o["data_bag"])
o.delete("data_bag")
o.delete("chef_type")
o.delete("json_class")
o.delete("name")
bag_item.raw_data = Mash.new(o["raw_data"])
bag_item
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/chef/data_bag_item.rb', line 147
def self.load(data_bag, name)
if Chef::Config[:solo]
bag = Chef::DataBag.load(data_bag)
item = bag[name]
else
item = Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("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
104
105
106
|
# File 'lib/chef/data_bag_item.rb', line 104
def self.object_name(data_bag_name, id)
"data_bag_item_#{data_bag_name}_#{id}"
end
|
.validate_id!(id_str) ⇒ Object
42
43
44
45
46
|
# File 'lib/chef/data_bag_item.rb', line 42
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
190
191
192
193
194
195
|
# File 'lib/chef/data_bag_item.rb', line 190
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
185
186
187
188
|
# File 'lib/chef/data_bag_item.rb', line 185
def create
chef_server_rest.post_rest("data/#{data_bag}", self)
self
end
|
#data_bag(arg = nil) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/chef/data_bag_item.rb', line 84
def data_bag(arg=nil)
set_or_return(
:data_bag,
arg,
:regex => /^[\-[:alnum:]_]+$/
)
end
|
#destroy(data_bag = data_bag(), databag_item = name) ⇒ Object
164
165
166
|
# File 'lib/chef/data_bag_item.rb', line 164
def destroy(data_bag=data_bag(), databag_item=name)
chef_server_rest.delete_rest("data/#{data_bag}/#{databag_item}")
end
|
#id ⇒ Object
210
211
212
|
# File 'lib/chef/data_bag_item.rb', line 210
def id
@raw_data['id']
end
|
#inspect ⇒ Object
202
203
204
|
# File 'lib/chef/data_bag_item.rb', line 202
def inspect
"data_bag_item[#{data_bag.inspect}, #{raw_data['id'].inspect}, #{raw_data.inspect}]"
end
|
#name ⇒ Object
92
93
94
|
# File 'lib/chef/data_bag_item.rb', line 92
def name
object_name
end
|
#object_name ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/chef/data_bag_item.rb', line 96
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
206
207
208
|
# File 'lib/chef/data_bag_item.rb', line 206
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/chef/data_bag_item.rb', line 169
def save(item_id=@raw_data['id'])
r = chef_server_rest
begin
if Chef::Config[:why_run]
Chef::Log.warn("In whyrun mode, so NOT performing data bag item save.")
else
r.put_rest("data/#{data_bag}/#{item_id}", self)
end
rescue Net::HTTPServerException => e
raise e unless e.response.code == "404"
r.post_rest("data/#{data_bag}", self)
end
self
end
|
#to_hash ⇒ Object
108
109
110
111
112
113
|
# File 'lib/chef/data_bag_item.rb', line 108
def to_hash
result = self.raw_data
result["chef_type"] = "data_bag_item"
result["data_bag"] = self.data_bag
result
end
|
#to_json(*a) ⇒ Object
Serialize this object as a hash
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/chef/data_bag_item.rb', line 116
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
198
199
200
|
# File 'lib/chef/data_bag_item.rb', line 198
def to_s
"data_bag_item[#{id}]"
end
|
#validate_id!(id_str) ⇒ Object
72
73
74
|
# File 'lib/chef/data_bag_item.rb', line 72
def validate_id!(id_str)
self.class.validate_id!(id_str)
end
|