class Chef::DataBagItem

Constants

VALID_ID

Attributes

raw_data[R]

Public Class Methods

chef_server_rest() click to toggle source
# File lib/chef/data_bag_item.rb, line 63
def self.chef_server_rest
  Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
from_hash(h) click to toggle source
# File lib/chef/data_bag_item.rb, line 123
def self.from_hash(h)
  h.delete("chef_type")
  h.delete("json_class")

  item = new
  item.data_bag(h.delete("data_bag")) if h.key?("data_bag")
  if h.key?("raw_data")
    item.raw_data = h["raw_data"]
  else
    item.raw_data = h
  end
  item
end
load(data_bag, name) click to toggle source

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 138
def self.load(data_bag, name)
  if Chef::Config[:solo_legacy_mode]
    bag = Chef::DataBag.load(data_bag)
    raise Exceptions::InvalidDataBagItemID, "Item #{name} not found in data bag #{data_bag}. Other items found: #{bag.keys.join(", ")}" unless bag.include?(name)
    item = bag[name]
  else
    item = Chef::ServerAPI.new(Chef::Config[:chef_server_url]).get("data/#{data_bag}/#{name}")
  end

  if item.kind_of?(DataBagItem)
    item
  else
    item = from_hash(item)
    item.data_bag(data_bag)
    item
  end
end
new(chef_server_rest: nil) click to toggle source

Create a new Chef::DataBagItem

# File lib/chef/data_bag_item.rb, line 53
def initialize(chef_server_rest: nil)
  @data_bag = nil
  @raw_data = Mash.new
  @chef_server_rest = chef_server_rest
end
object_name(data_bag_name, id) click to toggle source
# File lib/chef/data_bag_item.rb, line 100
def self.object_name(data_bag_name, id)
  "data_bag_item_#{data_bag_name}_#{id}"
end
validate_id!(id_str) click to toggle source
# File lib/chef/data_bag_item.rb, line 41
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

Public Instance Methods

==(other) click to toggle source
# File lib/chef/data_bag_item.rb, line 182
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
chef_server_rest() click to toggle source
# File lib/chef/data_bag_item.rb, line 59
def chef_server_rest
  @chef_server_rest ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url])
end
create() click to toggle source

Create this Data Bag Item via RESTful API

# File lib/chef/data_bag_item.rb, line 177
def create
  chef_server_rest.post("data/#{data_bag}", self)
  self
end
data_bag(arg = nil) click to toggle source
# File lib/chef/data_bag_item.rb, line 80
def data_bag(arg = nil)
  set_or_return(
    :data_bag,
    arg,
    :regex => /^[\-[:alnum:]_]+$/
  )
end
destroy(data_bag = self.data_bag(), databag_item = name) click to toggle source
# File lib/chef/data_bag_item.rb, line 156
def destroy(data_bag = self.data_bag(), databag_item = name)
  chef_server_rest.delete("data/#{data_bag}/#{databag_item}")
end
id() click to toggle source
# File lib/chef/data_bag_item.rb, line 202
def id
  @raw_data["id"]
end
inspect() click to toggle source
# File lib/chef/data_bag_item.rb, line 194
def inspect
  "data_bag_item[#{data_bag.inspect}, #{raw_data['id'].inspect}, #{raw_data.inspect}]"
end
name() click to toggle source
# File lib/chef/data_bag_item.rb, line 88
def name
  object_name
end
object_name() click to toggle source
# File lib/chef/data_bag_item.rb, line 92
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) click to toggle source
# File lib/chef/data_bag_item.rb, line 198
def pretty_print(pretty_printer)
  pretty_printer.pp({ "data_bag_item('#{data_bag}', '#{id}')" => to_hash })
end
raw_data=(new_data) click to toggle source
# File lib/chef/data_bag_item.rb, line 71
def raw_data=(new_data)
  new_data = Mash.new(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(item_id = @raw_data["id"]) click to toggle source

Save this Data Bag Item via RESTful API

# File lib/chef/data_bag_item.rb, line 161
def save(item_id = @raw_data["id"])
  r = chef_server_rest
  begin
    if Chef::Config[:why_run]
      Chef::Log.warn("In why-run mode, so NOT performing data bag item save.")
    else
      r.put("data/#{data_bag}/#{item_id}", self)
    end
  rescue Net::HTTPServerException => e
    raise e unless e.response.code == "404"
    r.post("data/#{data_bag}", self)
  end
  self
end
to_hash() click to toggle source
# File lib/chef/data_bag_item.rb, line 104
def to_hash
  result = raw_data.dup
  result["chef_type"] = "data_bag_item"
  result["data_bag"] = data_bag.to_s
  result
end
to_json(*a) click to toggle source

Serialize this object as a hash

# File lib/chef/data_bag_item.rb, line 112
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() click to toggle source

As a string

# File lib/chef/data_bag_item.rb, line 190
def to_s
  "data_bag_item[#{id}]"
end
validate_id!(id_str) click to toggle source
# File lib/chef/data_bag_item.rb, line 67
def validate_id!(id_str)
  self.class.validate_id!(id_str)
end