Class: Chef::DataBagItem

Inherits:
Object show all
Includes:
Mixin::FromFile, Mixin::ParamsValidate
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

Class Method Summary collapse

Instance Method Summary collapse

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



61
62
63
64
65
66
67
# File 'lib/chef/data_bag_item.rb', line 61

def initialize(couchdb=nil)
  @couchdb_rev = nil
  @couchdb_id = nil
  @data_bag = nil
  @raw_data = Mash.new
  @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.



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

def method_missing(method_symbol, *args, &block) 
  self.raw_data.send(method_symbol, *args, &block)
end

Instance Attribute Details

#couchdb_idObject

Returns the value of attribute couchdb_id.



58
59
60
# File 'lib/chef/data_bag_item.rb', line 58

def couchdb_id
  @couchdb_id
end

#couchdb_revObject

Returns the value of attribute couchdb_rev.



58
59
60
# File 'lib/chef/data_bag_item.rb', line 58

def couchdb_rev
  @couchdb_rev
end

#raw_dataObject

Returns the value of attribute raw_data.



58
59
60
# File 'lib/chef/data_bag_item.rb', line 58

def raw_data
  @raw_data
end

Class Method Details

.cdb_load(data_bag, name) ⇒ Object

Load a Data Bag Item by name from CouchDB



163
164
165
166
# File 'lib/chef/data_bag_item.rb', line 163

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

.create_design_documentObject

Set up our CouchDB design document



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

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

.json_create(o) ⇒ Object

Create a Chef::DataBagItem from JSON



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chef/data_bag_item.rb', line 138

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"] 
    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



169
170
171
172
# File 'lib/chef/data_bag_item.rb', line 169

def self.load(data_bag, name)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.get_rest("data/#{data_bag}/#{name}")
end

.object_name(data_bag_name, id) ⇒ Object



112
113
114
# File 'lib/chef/data_bag_item.rb', line 112

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

Instance Method Details

#cdb_destroyObject

Remove this Data Bag Item from CouchDB



175
176
177
178
# File 'lib/chef/data_bag_item.rb', line 175

def cdb_destroy
  removed = @couchdb.delete("data_bag_item", object_name, @couchdb_rev)
  removed
end

#cdb_saveObject

Save this Data Bag Item to CouchDB



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

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

#createObject

Create this Data Bag Item via RESTful API



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

def create
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.post_rest("data/#{data_bag}", @raw_data) 
  self
end

#data_bag(arg = nil) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/chef/data_bag_item.rb', line 86

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
183
# File 'lib/chef/data_bag_item.rb', line 180

def destroy(data_bag=data_bag, databag_item=name)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.delete_rest("data/#{data_bag}/#{databag_item}")
end

#nameObject



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

def name
  object_name
end

#object_nameObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/chef/data_bag_item.rb', line 98

def object_name
  if raw_data.has_key?('id')
    id = raw_data['id']
  else
    raise ArgumentError, "You must have an 'id' or :id key in the raw data"
  end
 
  data_bag_name = self.data_bag
  unless data_bag_name
    raise ArgumentError, "You must have declared what bag this item belongs to!"
  end
  "data_bag_item_#{data_bag_name}_#{id}"
end

#save(item_id = ) ⇒ Object

Save this Data Bag Item via RESTful API



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

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

#to_hashObject



116
117
118
119
120
121
122
# File 'lib/chef/data_bag_item.rb', line 116

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



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

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



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

def to_s
  "data_bag_item[#{@name}]"
end