Class: Chef::DataBagItem

Inherits:
Object show all
Extended by:
Forwardable
Includes:
IndexQueue::Indexable, Mixin::FromFile, Mixin::ParamsValidate
Defined in:
lib/chef/data_bag_item.rb

Constant Summary collapse

VALID_ID =
/^[\-[:alnum:]_]+$/
DESIGN_DOCUMENT =
{
  "version" => 1,
  "language" => "javascript",
  "views" => {
    "all" => {
      "map" => <<-EOJS
      function(doc) {
        if (doc.chef_type == "data_bag_item") {
          emit(doc.name, doc);
        }
      }
      EOJS
    },
    "all_id" => {
      "map" => <<-EOJS
      function(doc) {
        if (doc.chef_type == "data_bag_item") {
          emit(doc.name, doc.name);
        }
      }
      EOJS
    }
  }
}

Instance Attribute Summary collapse

Attributes included from IndexQueue::Indexable

#index_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexQueue::Indexable

#add_to_index, #delete_from_index, included, #index_object_type, #with_indexer_metadata

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Methods included from Mixin::FromFile

#class_from_file, #from_file

Constructor Details

#initialize(couchdb = nil) ⇒ DataBagItem

Create a new Chef::DataBagItem



81
82
83
84
85
86
87
# File 'lib/chef/data_bag_item.rb', line 81

def initialize(couchdb=nil)
  @couchdb_rev = nil
  @couchdb_id = nil
  @data_bag = nil
  @raw_data = Mash.new
  @couchdb = couchdb || Chef::CouchDB.new
end

Instance Attribute Details

#couchdbObject

Returns the value of attribute couchdb.



77
78
79
# File 'lib/chef/data_bag_item.rb', line 77

def couchdb
  @couchdb
end

#couchdb_idObject

Returns the value of attribute couchdb_id.



77
78
79
# File 'lib/chef/data_bag_item.rb', line 77

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev.



77
78
79
# File 'lib/chef/data_bag_item.rb', line 77

def couchdb_rev
  @couchdb_rev
end

#raw_dataObject

Returns the value of attribute raw_data.



78
79
80
# File 'lib/chef/data_bag_item.rb', line 78

def raw_data
  @raw_data
end

Class Method Details

.cdb_load(data_bag, name, couchdb = nil) ⇒ Object

Load a Data Bag Item by name from CouchDB



186
187
188
# File 'lib/chef/data_bag_item.rb', line 186

def self.cdb_load(data_bag, name, couchdb=nil)
  (couchdb || Chef::CouchDB.new).load("data_bag_item", object_name(data_bag, name))
end

.chef_server_restObject



93
94
95
# File 'lib/chef/data_bag_item.rb', line 93

def self.chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end

.create_design_document(couchdb = nil) ⇒ Object

Set up our CouchDB design document



242
243
244
# File 'lib/chef/data_bag_item.rb', line 242

def self.create_design_document(couchdb=nil)
  (couchdb || Chef::CouchDB.new).create_design_document("data_bag_items", DESIGN_DOCUMENT)
end

.from_hash(h) ⇒ Object



158
159
160
161
162
# File 'lib/chef/data_bag_item.rb', line 158

def self.from_hash(h)
  item = new
  item.raw_data = h
  item
end

.json_create(o) ⇒ Object

Create a Chef::DataBagItem from JSON



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/chef/data_bag_item.rb', line 165

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")
  if o.has_key?("_rev")
    bag_item.couchdb_rev = o["_rev"]
    o.delete("_rev")
  end
  if o.has_key?("_id")
    bag_item.couchdb_id = o["_id"]
    bag_item.index_id = bag_item.couchdb_id
    o.delete("_id")
  end
  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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/chef/data_bag_item.rb', line 191

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



133
134
135
# File 'lib/chef/data_bag_item.rb', line 133

def self.object_name(data_bag_name, id)
  "data_bag_item_#{data_bag_name}_#{id}"
end

.validate_id!(id_str) ⇒ Object



68
69
70
71
72
# File 'lib/chef/data_bag_item.rb', line 68

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



246
247
248
249
250
251
# File 'lib/chef/data_bag_item.rb', line 246

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

#cdb_destroyObject

Remove this Data Bag Item from CouchDB



209
210
211
212
# File 'lib/chef/data_bag_item.rb', line 209

def cdb_destroy
  Chef::Log.debug "Destroying data bag item: #{self.inspect}"
  @couchdb.delete("data_bag_item", object_name, @couchdb_rev)
end

#cdb_saveObject

Save this Data Bag Item to CouchDB



219
220
221
# File 'lib/chef/data_bag_item.rb', line 219

def cdb_save
  @couchdb_rev = @couchdb.store("data_bag_item", object_name, self)["rev"]
end

#chef_server_restObject



89
90
91
# File 'lib/chef/data_bag_item.rb', line 89

def chef_server_rest
  Chef::REST.new(Chef::Config[:chef_server_url])
end

#createObject

Create this Data Bag Item via RESTful API



236
237
238
239
# File 'lib/chef/data_bag_item.rb', line 236

def create
  chef_server_rest.post_rest("data/#{data_bag}", self)
  self
end

#data_bag(arg = nil) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/chef/data_bag_item.rb', line 113

def data_bag(arg=nil)
  set_or_return(
    :data_bag,
    arg,
    :regex => /^[\-[:alnum:]_]+$/
  )
end

#destroy(data_bag = data_bag, databag_item = name) ⇒ Object



214
215
216
# File 'lib/chef/data_bag_item.rb', line 214

def destroy(data_bag=data_bag, databag_item=name)
  chef_server_rest.delete_rest("data/#{data_bag}/#{databag_item}")
end

#idObject



266
267
268
# File 'lib/chef/data_bag_item.rb', line 266

def id
  @raw_data['id']
end

#inspectObject



258
259
260
# File 'lib/chef/data_bag_item.rb', line 258

def inspect
  "data_bag_item[#{data_bag.inspect}, #{raw_data['id'].inspect}, #{raw_data.inspect}]"
end

#nameObject



121
122
123
# File 'lib/chef/data_bag_item.rb', line 121

def name
  object_name
end

#object_nameObject



125
126
127
128
129
130
131
# File 'lib/chef/data_bag_item.rb', line 125

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



262
263
264
# File 'lib/chef/data_bag_item.rb', line 262

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



224
225
226
227
228
229
230
231
232
233
# File 'lib/chef/data_bag_item.rb', line 224

def save(item_id=@raw_data['id'])
  r = chef_server_rest
  begin
    r.put_rest("data/#{data_bag}/#{item_id}", self)
  rescue Net::HTTPServerException => e
    raise e unless e.response.code == "404"
    r.post_rest("data/#{data_bag}", self)
  end
  self
end

#to_hashObject



137
138
139
140
141
142
143
# File 'lib/chef/data_bag_item.rb', line 137

def to_hash
  result = self.raw_data
  result["chef_type"] = "data_bag_item"
  result["data_bag"] = self.data_bag
  result["_rev"] = @couchdb_rev if @couchdb_rev
  result
end

#to_json(*a) ⇒ Object

Serialize this object as a hash



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/chef/data_bag_item.rb', line 146

def to_json(*a)
  result = {
    "name" => self.object_name,
    "json_class" => self.class.name,
    "chef_type" => "data_bag_item",
    "data_bag" => self.data_bag,
    "raw_data" => self.raw_data
  }
  result["_rev"] = @couchdb_rev if @couchdb_rev
  result.to_json(*a)
end

#to_sObject

As a string



254
255
256
# File 'lib/chef/data_bag_item.rb', line 254

def to_s
  "data_bag_item[#{id}]"
end

#validate_id!(id_str) ⇒ Object



101
102
103
# File 'lib/chef/data_bag_item.rb', line 101

def validate_id!(id_str)
  self.class.validate_id!(id_str)
end