Class: Blender::Discovery::Chef

Inherits:
Object
  • Object
show all
Defined in:
lib/blender/discoveries/chef.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Chef

Returns a new instance of Chef.



25
26
27
# File 'lib/blender/discoveries/chef.rb', line 25

def initialize(options = {})
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



23
24
25
# File 'lib/blender/discoveries/chef.rb', line 23

def options
  @options
end

Instance Method Details

#search(opts = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/blender/discoveries/chef.rb', line 29

def search(opts = {})
  attr = options[:attribute] || 'fqdn'
  case opts
  when String
    search_term = opts
  when Hash
    search_term = opts[:search_term]
    attr = opts[:attribute] if opts.key?(:attribute)
  else
    raise ArgumentError, "Invalid argument type #{opts.class}"
  end
  search_term ||= '*:*'
  ::Chef::Config.from_file(options[:config_file]) if options[:config_file]
  ::Chef::Config[:node_name] = options[:node_name] if options[:node_name]
  ::Chef::Config[:client_key] = options[:client_key] if options[:client_key]
  ::Chef::Config[:chef_server_url] = options[:chef_server_url] if options[:chef_server_url]
  q = ::Chef::Search::Query.new
  res = q.search(:node, search_term, filter_result: {attribute: attr.split('.')})
  res.first.collect{|node_data| node_data['attribute']}
end