Class: Chef::DataBagItem
- Inherits:
-
Object
- Object
- Chef::DataBagItem
- Defined in:
- lib/chef/data_bag_item.rb
Constant Summary collapse
- 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
-
#couchdb ⇒ Object
Returns the value of attribute couchdb.
-
#couchdb_id ⇒ Object
Returns the value of attribute couchdb_id.
-
#couchdb_rev ⇒ Object
Returns the value of attribute couchdb_rev.
-
#raw_data ⇒ Object
Returns the value of attribute raw_data.
Attributes included from IndexQueue::Indexable
Class Method Summary collapse
-
.cdb_load(data_bag, name, couchdb = nil) ⇒ Object
Load a Data Bag Item by name from CouchDB.
- .chef_server_rest ⇒ Object
-
.create_design_document(couchdb = nil) ⇒ Object
Set up our CouchDB design document.
-
.json_create(o) ⇒ Object
Create a Chef::DataBagItem from JSON.
-
.load(data_bag, name) ⇒ Object
Load a Data Bag Item by name via RESTful API.
- .object_name(data_bag_name, id) ⇒ Object
Instance Method Summary collapse
-
#cdb_destroy ⇒ Object
Remove this Data Bag Item from CouchDB.
-
#cdb_save ⇒ Object
Save this Data Bag Item to CouchDB.
- #chef_server_rest ⇒ Object
-
#create ⇒ Object
Create this Data Bag Item via RESTful API.
- #data_bag(arg = nil) ⇒ Object
- #destroy(data_bag = data_bag, databag_item = name) ⇒ Object
- #id ⇒ Object
-
#initialize(couchdb = nil) ⇒ DataBagItem
constructor
Create a new Chef::DataBagItem.
-
#method_missing(method_symbol, *args, &block) ⇒ Object
The Data Bag Item behaves like a hash - we pass all that stuff along to @raw_data.
- #name ⇒ Object
- #object_name ⇒ Object
- #pretty_print(pretty_printer) ⇒ Object
-
#save(item_id = ) ⇒ Object
Save this Data Bag Item via RESTful API.
- #to_hash ⇒ Object
-
#to_json(*a) ⇒ Object
Serialize this object as a hash.
-
#to_s ⇒ Object
As a string.
Methods included from IndexQueue::Indexable
#add_to_index, #delete_from_index, included, #index_object_type, #with_indexer_metadata
Methods included from Mixin::ParamsValidate
Methods included from Mixin::FromFile
Constructor Details
#initialize(couchdb = nil) ⇒ DataBagItem
Create a new Chef::DataBagItem
66 67 68 69 70 71 72 |
# File 'lib/chef/data_bag_item.rb', line 66 def initialize(couchdb=nil) @couchdb_rev = nil @couchdb_id = nil @data_bag = nil @raw_data = Mash.new @couchdb = couchdb || Chef::CouchDB.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *args, &block) ⇒ Object
The Data Bag Item behaves like a hash - we pass all that stuff along to @raw_data.
160 161 162 |
# File 'lib/chef/data_bag_item.rb', line 160 def method_missing(method_symbol, *args, &block) self.raw_data.send(method_symbol, *args, &block) end |
Instance Attribute Details
#couchdb ⇒ Object
Returns the value of attribute couchdb.
62 63 64 |
# File 'lib/chef/data_bag_item.rb', line 62 def couchdb @couchdb end |
#couchdb_id ⇒ Object
Returns the value of attribute couchdb_id.
62 63 64 |
# File 'lib/chef/data_bag_item.rb', line 62 def couchdb_id @couchdb_id end |
#couchdb_rev ⇒ Object
Returns the value of attribute couchdb_rev.
62 63 64 |
# File 'lib/chef/data_bag_item.rb', line 62 def couchdb_rev @couchdb_rev end |
#raw_data ⇒ Object
Returns the value of attribute raw_data.
63 64 65 |
# File 'lib/chef/data_bag_item.rb', line 63 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
165 166 167 |
# File 'lib/chef/data_bag_item.rb', line 165 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_rest ⇒ Object
78 79 80 |
# File 'lib/chef/data_bag_item.rb', line 78 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
208 209 210 |
# File 'lib/chef/data_bag_item.rb', line 208 def self.create_design_document(couchdb=nil) (couchdb || Chef::CouchDB.new).create_design_document("data_bag_items", DESIGN_DOCUMENT) end |
.json_create(o) ⇒ Object
Create a Chef::DataBagItem from JSON
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/chef/data_bag_item.rb', line 139 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 RESTful API
170 171 172 |
# File 'lib/chef/data_bag_item.rb', line 170 def self.load(data_bag, name) Chef::REST.new(Chef::Config[:chef_server_url]).get_rest("data/#{data_bag}/#{name}") end |
.object_name(data_bag_name, id) ⇒ Object
113 114 115 |
# File 'lib/chef/data_bag_item.rb', line 113 def self.object_name(data_bag_name, id) "data_bag_item_#{data_bag_name}_#{id}" end |
Instance Method Details
#cdb_destroy ⇒ Object
Remove this Data Bag Item from CouchDB
175 176 177 178 |
# File 'lib/chef/data_bag_item.rb', line 175 def cdb_destroy Chef::Log.debug "destroying data bag item: #{self.inspect}" @couchdb.delete("data_bag_item", object_name, @couchdb_rev) end |
#cdb_save ⇒ Object
Save this Data Bag Item to CouchDB
185 186 187 |
# File 'lib/chef/data_bag_item.rb', line 185 def cdb_save @couchdb_rev = @couchdb.store("data_bag_item", object_name, self)["rev"] end |
#chef_server_rest ⇒ Object
74 75 76 |
# File 'lib/chef/data_bag_item.rb', line 74 def chef_server_rest Chef::REST.new(Chef::Config[:chef_server_url]) end |
#create ⇒ Object
Create this Data Bag Item via RESTful API
202 203 204 205 |
# File 'lib/chef/data_bag_item.rb', line 202 def create chef_server_rest.post_rest("data/#{data_bag}", @raw_data) self end |
#data_bag(arg = nil) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/chef/data_bag_item.rb', line 93 def data_bag(arg=nil) set_or_return( :data_bag, arg, :regex => /^[\-[:alnum:]_]+$/ ) end |
#destroy(data_bag = data_bag, databag_item = name) ⇒ Object
180 181 182 |
# File 'lib/chef/data_bag_item.rb', line 180 def destroy(data_bag=data_bag, databag_item=name) chef_server_rest.delete_rest("data/#{data_bag}/#{databag_item}") end |
#id ⇒ Object
221 222 223 |
# File 'lib/chef/data_bag_item.rb', line 221 def id @raw_data['id'] end |
#name ⇒ Object
101 102 103 |
# File 'lib/chef/data_bag_item.rb', line 101 def name object_name end |
#object_name ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/chef/data_bag_item.rb', line 105 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
217 218 219 |
# File 'lib/chef/data_bag_item.rb', line 217 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
190 191 192 193 194 195 196 197 198 199 |
# File 'lib/chef/data_bag_item.rb', line 190 def save(item_id=@raw_data['id']) r = chef_server_rest begin r.put_rest("data/#{data_bag}/#{item_id}", @raw_data) rescue Net::HTTPServerException => e raise e unless e.response.code == "404" r.post_rest("data/#{data_bag}", @raw_data) end self end |
#to_hash ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/chef/data_bag_item.rb', line 117 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
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/chef/data_bag_item.rb', line 126 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_s ⇒ Object
As a string
213 214 215 |
# File 'lib/chef/data_bag_item.rb', line 213 def to_s "data_bag_item[#{id}]" end |