module Chef::Sugar::DataBag

Public Instance Methods

data_bag_item_for_environment(node, bag, id) click to toggle source

This algorithm attempts to find the data bag entry for the current node's Chef environment. If there are no environment-specific values, the “default” bucket is used. The data bag must follow the schema:

{
  "default": {...},
  "environment_name": {...},
  "other_environment": {...},
}

@param [Node] node

the current Chef node

@param [String] bag

the name of the data bag

@param [String] id

the id of the data bag

@return [Hash]

# File lib/chef/sugar/data_bag.rb, line 116
def data_bag_item_for_environment(node, bag, id)
  data = Chef::DataBagItem.load(bag, id)

  if data[node.chef_environment]
    Chef::Log.debug "Using #{node.chef_environment} as the key"
    data[node.chef_environment]
  else
    Chef::Log.debug "#{node.chef_environment} key does not exist, using `default`"
    data['default']
  end
end
encrypted_data_bag_item(bag, id, secret = nil) click to toggle source

Helper method for loading an encrypted data bag item in a similar syntax/recipe DSL method.

@param [String] bag

the name of the encrypted data bag

@param [String] id

the id of the encrypted data bag

@param [String] secret

the encrypted data bag secret raw value

@return [Hash]

# File lib/chef/sugar/data_bag.rb, line 49
def encrypted_data_bag_item(bag, id, secret = nil)
  Chef::Log.debug "Loading encrypted data bag item #{bag}/#{id}"

  if secret.nil? && Chef::Config[:encrypted_data_bag_secret].nil?
    raise EncryptedDataBagSecretNotGiven.new
  end

  secret ||= File.read(Chef::Config[:encrypted_data_bag_secret]).strip
  Chef::EncryptedDataBagItem.load(bag, id, secret)
end
encrypted_data_bag_item_for_environment(node, bag, id, secret = nil) click to toggle source

This algorithm attempts to find the data bag entry for the current node's Chef environment. If there are no environment-specific values, the “default” bucket is used. The data bag must follow the schema:

{
  "default": {...},
  "environment_name": {...},
  "other_environment": {...},
}

@param [Node] node

the current Chef node

@param [String] bag

the name of the encrypted data bag

@param [String] id

the id of the encrypted data bag

@param [String] secret

the encrypted data bag secret (default's to the +Chef::Config+ value)

@return [Hash]

# File lib/chef/sugar/data_bag.rb, line 83
def encrypted_data_bag_item_for_environment(node, bag, id, secret = nil)
  data = encrypted_data_bag_item(bag, id, secret)

  if data[node.chef_environment]
    Chef::Log.debug "Using #{node.chef_environment} as the key"
    data[node.chef_environment]
  else
    Chef::Log.debug "#{node.chef_environment} key does not exist, using `default`"
    data['default']
  end
end