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.



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



30
31
32
# File 'lib/chef/search/query.rb', line 30

def config
  @config
end

#restObject

Returns the value of attribute rest.



29
30
31
# File 'lib/chef/search/query.rb', line 29

def rest
  @rest
end

Instance Method Details

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

Backwards compatability for cookbooks. This can be removed in Chef > 12.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/search/query.rb', line 43

def partial_search(type, query = "*:*", *args, &block)
  Chef::Log.warn(<<-WARNDEP)
DEPRECATED: The 'partial_search' API is deprecated and will be removed in
future releases. Please use 'search' with a :filter_result argument to get
partial search data.
WARNDEP

  if !args.empty? && args.first.is_a?(Hash)
    # partial_search uses :keys instead of :filter_result for
    # result filtering.
    args_h = args.first.dup
    args_h[:filter_result] = args_h[:keys]
    args_h.delete(:keys)

    search(type, query, args_h, &block)
  else
    search(type, query, *args, &block)
  end
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”



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/chef/search/query.rb', line 83

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

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

  if block
    response["rows"].each { |row| block.call(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] || response["rows"].length)
    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