module Chef::DataRegion::DataQuery
DataQuery
module
This module borrows its name from {Chef::DSL::DataQuery}, which defines the `data_bag_item` method. It exists for mixing in to {Chef::Recipe}, as occurs with {Chef::DSL::DataQuery}. This is the means by which `data_bag_item` is available in Chef
recipes without qualification.
Public Instance Methods
Fetch the specified item from the specified bag
@param bag [] data bag name @param item [] data bag item name @param secret [] encrypted data bag item secret key @return [Chef::DataBagItem] the fetched data bag item
# File lib/chef/data_region.rb, line 39 def data_bag_item(bag, item, secret = nil) loaded_item = Chef::DataBagItem.load(expand_bag_name(bag), item) encrypted?(loaded_item) ? decrypt(loaded_item) : loaded_item end
Private Instance Methods
Decrypt an encrypted item
@param item [Chef::DataBagItem] a data bag item
# File lib/chef/data_region.rb, line 49 def decrypt(item) secret ||= Chef::EncryptedDataBagItem.load_secret Chef::EncryptedDataBagItem.new(item.raw_data, secret) end
Is the given item encrypted?
@param item [Chef::DataBagItem] a data bag item
# File lib/chef/data_region.rb, line 57 def encrypted?(item) item.raw_data.map do |_, value| value.respond_to?(:key) && value.key?('encrypted_data') end.reduce(false, :|) end
Expand a data bag name if it matches one of the configured patterns.
If the name matches, consult the node attribute specified in the configuration to retrieve the region name, then substitute it into the expansion pattern.
If the name does not match, return the name verbatim.
@param bag_name [String] a data bag name @return [String] the expanded bag name
# File lib/chef/data_region.rb, line 73 def expand_bag_name(bag_name) if Chef::DataRegion.bags.keys.include?(bag_name) lambda do |definition| format( definition[:pattern], attribute: definition[:attribute].reduce(node) do |hash, index| hash.fetch(index) end ) end.call(Chef::DataRegion.bags.fetch(bag_name)) else bag_name end rescue KeyError bag_name rescue NoMethodError raise("Undefined region for data bag '#{bag_name}'") end