class Chef::Search::Query

Constants

QUERY_PARAM_VALUE

Attributes

config[R]

Public Class Methods

new(url = nil, config: Chef::Config) click to toggle source
# File lib/chef/search/query.rb, line 32
def initialize(url = nil, config: Chef::Config)
  @config = config
  @url = url
end

Public Instance Methods

rest() click to toggle source
# File lib/chef/search/query.rb, line 37
def rest
  @rest ||= Chef::ServerAPI.new(@url || @config[:chef_server_url])
end

Private Instance Methods

call_rest_service(type, query: "*:*", rows: nil, start: 0, filter_result: nil) click to toggle source
# File lib/chef/search/query.rb, line 151
def call_rest_service(type, query: "*:*", rows: nil, start: 0, filter_result: nil)
  query_string = create_query_string(type, query, rows, start)

  if filter_result
    response = rest.post(query_string, filter_result)
    # response returns rows in the format of
    # { "url" => url_to_node, "data" => filter_result_hash }
    response["rows"].map! { |row| row["data"] }
  else
    response = rest.get(query_string)
    response["rows"].map! do |row|
      case type.to_s
      when "node"
        Chef::Node.from_hash(row)
      when "role"
        Chef::Role.from_hash(row)
      when "environment"
        Chef::Environment.from_hash(row)
      when "client"
        Chef::ApiClient.from_hash(row)
      else
        Chef::DataBagItem.from_hash(row)
      end
    end
  end

  response
end
create_query_string(type, query, rows, start) click to toggle source
# File lib/chef/search/query.rb, line 144
def create_query_string(type, query, rows, start)
  qstr = "search/#{type}?q=#{escape_value(query)}"
  qstr += "&start=#{escape_value(start)}" if start
  qstr += "&rows=#{escape_value(rows)}" if rows
  qstr
end
escape_value(s) click to toggle source
# File lib/chef/search/query.rb, line 140
def escape_value(s)
  s && Addressable::URI.encode_component(s.to_s, QUERY_PARAM_VALUE)
end
fuzzify_node_query(query) click to toggle source
# File lib/chef/search/query.rb, line 107
def fuzzify_node_query(query)
  if query !~ /:/
    "tags:*#{query}* OR roles:*#{query}* OR fqdn:*#{query}* OR addresses:*#{query}* OR policy_name:*#{query}* OR policy_group:*#{query}*"
  else
    query
  end
end
hashify_args(*args) click to toggle source
# File lib/chef/search/query.rb, line 124
def hashify_args(*args)
  return Hash.new if args.empty?
  return args.first if args.first.is_a?(Hash)

  args_h = Hash.new
  # If we have 4 arguments, the first is the now-removed sort option, so
  # just ignore it.
  args.pop(0) if args.length == 4
  args_h[:start] = args[0] if args[0]
  args_h[:rows] = args[1]
  args_h[:filter_result] = args[2]
  args_h
end
validate_type(t) click to toggle source
# File lib/chef/search/query.rb, line 115
def validate_type(t)
  unless t.kind_of?(String) || t.kind_of?(Symbol)
    msg = "Invalid search object type #{t.inspect} (#{t.class}), must be a String or Symbol." +
      "Usage: search(:node, QUERY[, OPTIONAL_ARGS])" +
      "        `knife search environment QUERY (options)`"
    raise Chef::Exceptions::InvalidSearchQuery, msg
  end
end