Module: Chef::Sugar::DataBag

Extended by:
DataBag
Included in:
DataBag
Defined in:
lib/chef/sugar/data_bag.rb

Instance Method Summary collapse

Instance Method Details

#encrypted_data_bag_item(bag, id) ⇒ Hash

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

Parameters:

  • bag (String)

    the name of the encrypted data bag

  • id (String)

    the id of the encrypted data bag

Returns:

  • (Hash)


33
34
35
36
# File 'lib/chef/sugar/data_bag.rb', line 33

def encrypted_data_bag_item(bag, id)
  Chef::Log.debug "Loading encrypted data bag item #{bag}/#{id}"
  Chef::EncryptedDataBagItem.load(bag, id)
end

#encrypted_data_bag_item_for_environment(node, bag, id) ⇒ Hash

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": {...},
}

Parameters:

  • node (Node)

    the current Chef node

  • bag (String)

    the name of the encrypted data bag

  • id (String)

    the id of the encrypted data bag

Returns:

  • (Hash)


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/chef/sugar/data_bag.rb', line 59

def encrypted_data_bag_item_for_environment(node, bag, id)
  data = encrypted_data_bag_item(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