Load a Data Bag Item by name from CouchDB
# 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
# File lib/chef/data_bag_item.rb, line 93 def self.chef_server_rest Chef::REST.new(Chef::Config[:chef_server_url]) end
Set up our CouchDB design document
# File lib/chef/data_bag_item.rb, line 246 def self.create_design_document(couchdb=nil) (couchdb || Chef::CouchDB.new).create_design_document("data_bag_items", DESIGN_DOCUMENT) end
# File lib/chef/data_bag_item.rb, line 158 def self.from_hash(h) item = new item.raw_data = h item end
Create a Chef::DataBagItem from JSON
# 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 a Data Bag Item by name via either the RESTful API or local data_bag_path if run in solo mode
# 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
Create a new Chef::DataBagItem
# 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
# File lib/chef/data_bag_item.rb, line 250 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
Remove this Data Bag Item from CouchDB
# 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
Save this Data Bag Item to CouchDB
# File lib/chef/data_bag_item.rb, line 219 def cdb_save @couchdb_rev = @couchdb.store("data_bag_item", object_name, self)["rev"] end
# File lib/chef/data_bag_item.rb, line 89 def chef_server_rest Chef::REST.new(Chef::Config[:chef_server_url]) end
Create this Data Bag Item via RESTful API
# File lib/chef/data_bag_item.rb, line 240 def create chef_server_rest.post_rest("data/#{data_bag}", self) self end
# File lib/chef/data_bag_item.rb, line 113 def data_bag(arg=nil) set_or_return( :data_bag, arg, :regex => /^[\-[:alnum:]_]+$/ ) end
# 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
# File lib/chef/data_bag_item.rb, line 262 def inspect "data_bag_item[#{data_bag.inspect}, #{raw_data['id'].inspect}, #{raw_data.inspect}]" end
# 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
# File lib/chef/data_bag_item.rb, line 266 def pretty_print(pretty_printer) pretty_printer.pp({"data_bag_item('#{data_bag}', '#{id}')" => self.to_hash}) end
# File lib/chef/data_bag_item.rb, line 105 def raw_data=(new_data) unless new_data.respond_to?(:[]) && new_data.respond_to?(:keys) raise Exceptions::ValidationFailed, "Data Bag Items must contain a Hash or Mash!" end validate_id!(new_data["id"]) @raw_data = new_data end
Save this Data Bag Item via RESTful API
# File lib/chef/data_bag_item.rb, line 224 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
# 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
Serialize this object as a hash
# 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
Generated with the Darkfish Rdoc Generator 2.