class Chef::Search::Query

Attributes

config[R]

Public Class Methods

new(url = nil, config: Chef::Config) click to toggle source
# File lib/chef/search/query.rb, line 34
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 39
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 149
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 142
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 137
def escape_value(s)
  query_param_value = Addressable::URI::CharacterClasses::QUERY + "\\&\\;"
  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 109
def fuzzify_node_query(query)
  if !/:/.match?(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 126
def hashify_args(*args)
  return {} if args.empty?
  return args.first if args.first.is_a?(Hash)

  args_h = {}
  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 117
def validate_type(t)
  unless t.is_a?(String) || t.is_a?(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