Class: Chef::Search::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/search/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, config: Chef::Config) ⇒ Query

Returns a new instance of Query.



34
35
36
37
# File 'lib/chef/search/query.rb', line 34

def initialize(url = nil, config: Chef::Config)
  @config = config
  @url = url
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/chef/search/query.rb', line 32

def config
  @config
end

Instance Method Details

#restObject



39
40
41
# File 'lib/chef/search/query.rb', line 39

def rest
  @rest ||= Chef::ServerAPI.new(@url || @config[:chef_server_url])
end

#search(type, query = "*:*", *args, &block) ⇒ Object

New search input, designed to be backwards compatible with the old method signature 'type' and 'query' are the same as before, args now will accept either a Hash of search arguments with symbols as the keys (ie :sort, :start, :rows) and a :filter_result option.

:filter_result should be in the format of another Hash with the structure of: { :returned_name1 => ["path", "to", "variable"], :returned_name2 => ["shorter", "path"] } a real world example might be something like: { :ip_address => ["ipaddress"], :ruby_version => ["languages", "ruby", "version"] } this will bring back 2 variables 'ip_address' and 'ruby_version' with whatever value was found an example of the returned json may be: "ruby_version": "1.9.3"



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/chef/search/query.rb', line 63

def search(type, query = "*:*", *args, &block)
  validate_type(type)

  args_h = hashify_args(*args)
  if args_h[:fuzz]
    if type.to_sym == :node
      query = fuzzify_node_query(query)
    end
    # FIXME: can i haz proper ruby-2.x named parameters someday plz?
    args_h = args_h.reject { |k, v| k == :fuzz }
  end

  # Set default rows parameter to 1000. This is the default in
  # Chef Server, but we set it explicitly here so that we can
  # confidently advance our start parameter.
  args_h[:rows] ||= 1000

  response = call_rest_service(type, query: query, **args_h)

  if block
    response["rows"].each { |row| yield(row) if row }
    #
    # args_h[:rows] and args_h[:start] are the page size and
    # start position requested of the search index backing the
    # search API.
    #
    # The response may contain fewer rows than arg_h[:rows] if
    # the page of index results included deleted nodes which
    # have been filtered from the returned data. In this case,
    # we still want to start the next page at start +
    # args_h[:rows] to avoid asking the search backend for
    # overlapping pages (which could result in duplicates).
    #
    next_start = response["start"] + args_h[:rows]
    unless next_start >= response["total"]
      args_h[:start] = next_start
      search(type, query, args_h, &block)
    end
    true
  else
    [ response["rows"], response["start"], response["total"] ]
  end
end