Module: Chef::DSL::DataQuery

Includes:
EncryptedDataBagItem::CheckEncrypted
Included in:
Recipe, Provider::LWRPBase, Resource
Defined in:
lib/chef/dsl/data_query.rb

Overview

Chef::DSL::DataQuery

Provides DSL for querying data from the chef-server via search or data bag.

Instance Method Summary collapse

Methods included from EncryptedDataBagItem::CheckEncrypted

#encrypted?

Instance Method Details

#data_bag(bag) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/chef/dsl/data_query.rb', line 49

def data_bag(bag)
  DataBag.validate_name!(bag.to_s)
  rbag = DataBag.load(bag)
  rbag.keys
rescue Exception
  Log.error("Failed to list data bag items in data bag: #{bag.inspect}")
  raise
end

#data_bag_item(bag, item, secret = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chef/dsl/data_query.rb', line 58

def data_bag_item(bag, item, secret = nil)
  DataBag.validate_name!(bag.to_s)
  DataBagItem.validate_id!(item)

  item = DataBagItem.load(bag, item)
  if encrypted?(item.raw_data)
    Log.debug("Data bag item looks encrypted: #{bag.inspect} #{item.inspect}")

    # Try to load the data bag item secret, if secret is not provided.
    # Chef::EncryptedDataBagItem.load_secret may throw a variety of errors.
    begin
      secret ||= EncryptedDataBagItem.load_secret
      item = EncryptedDataBagItem.new(item.raw_data, secret)
    rescue Exception
      Log.error("Failed to load secret for encrypted data bag item: #{bag.inspect} #{item.inspect}")
      raise
    end
  end

  item
rescue Exception
  Log.error("Failed to load data bag item: #{bag.inspect} #{item.inspect}")
  raise
end

#search(*args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chef/dsl/data_query.rb', line 34

def search(*args, &block)
  # If you pass a block, or have at least the start argument, do raw result parsing
  #
  # Otherwise, do the iteration for the end user
  if Kernel.block_given? || args.length >= 4
    Chef::Search::Query.new.search(*args, &block)
  else
    results = Array.new
    Chef::Search::Query.new.search(*args) do |o|
      results << o
    end
    results
  end
end