Class: Chef::Knife::Search
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::Search
- Defined in:
- lib/chef/knife/search.rb
Instance Attribute Summary
Attributes inherited from Chef::Knife
Instance Method Summary collapse
-
#create_result_filter(filter_string) ⇒ Object
This method turns a set of key value pairs in a string into the appropriate data structure that the chef-server search api is expecting.
- #create_result_filter_from_attributes(filter_array) ⇒ Object
- #fuzzify_query ⇒ Object
- #read_cli_args ⇒ Object
- #run ⇒ Object
Methods included from Core::NodeFormattingOptions
Methods included from Core::MultiAttributeReturnOption
Methods inherited from Chef::Knife
#api_key, #apply_computed_config, category, chef_config_dir, common_name, #config_file_settings, config_loader, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username
Methods included from Mixin::ConvertToClassName
#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename
Methods included from Mixin::PathSanity
Constructor Details
This class inherits a constructor from Chef::Knife
Instance Method Details
#create_result_filter(filter_string) ⇒ Object
This method turns a set of key value pairs in a string into the appropriate data structure that the chef-server search api is expecting. expected input is in the form of: -f “return_var1=path.to.attribute, return_var2=shorter.path”
a more concrete example might be: -f “env=chef_environment, ruby_platform=languages.ruby.platform”
The end result is a hash where the key is a symbol in the hash (the return variable) and the path is an array with the path elements as strings (in order) See lib/chef/search/query.rb for more examples of this.
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/chef/knife/search.rb', line 178 def create_result_filter(filter_string) final_filter = Hash.new filter_string.delete!(" ") filters = filter_string.split(",") filters.each do |f| return_id, attr_path = f.split("=") final_filter[return_id.to_sym] = attr_path.split(".") end return final_filter end |
#create_result_filter_from_attributes(filter_array) ⇒ Object
189 190 191 192 193 194 195 196 197 |
# File 'lib/chef/knife/search.rb', line 189 def create_result_filter_from_attributes(filter_array) final_filter = Hash.new filter_array.each do |f| final_filter[f] = f.split(".") end # adding magic filter so we can actually pull the name as before final_filter["__display_name"] = [ "name" ] return final_filter end |
#fuzzify_query ⇒ Object
161 162 163 164 165 |
# File 'lib/chef/knife/search.rb', line 161 def fuzzify_query if @query !~ /:/ @query = "tags:*#{@query}* OR roles:*#{@query}* OR fqdn:*#{@query}* OR addresses:*#{@query}* OR policy_name:*#{@query}* OR policy_group:*#{@query}*" end end |
#read_cli_args ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/chef/knife/search.rb', line 136 def read_cli_args if config[:query] if @name_args[1] ui.error "Please specify query as an argument or an option via -q, not both" ui.msg opt_parser exit 1 end @type = name_args[0] @query = config[:query] else case name_args.size when 0 ui.error "No query specified" ui.msg opt_parser exit 1 when 1 @type = "node" @query = name_args[0] when 2 @type = name_args[0] @query = name_args[1] end end end |
#run ⇒ Object
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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/chef/knife/search.rb', line 79 def run read_cli_args fuzzify_query if @type == "node" ui.use_presenter Knife::Core::NodePresenter end q = Chef::Search::Query.new escaped_query = URI.escape(@query, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) result_items = [] result_count = 0 search_args = Hash.new search_args[:sort] = config[:sort] if config[:sort] search_args[:start] = config[:start] if config[:start] search_args[:rows] = config[:rows] if config[:rows] if config[:filter_result] search_args[:filter_result] = create_result_filter(config[:filter_result]) elsif (not ui.config[:attribute].nil?) && (not ui.config[:attribute].empty?) search_args[:filter_result] = create_result_filter_from_attributes(ui.config[:attribute]) end begin q.search(@type, escaped_query, search_args) do |item| formatted_item = Hash.new if item.is_a?(Hash) # doing a little magic here to set the correct name formatted_item[item["__display_name"]] = item.reject { |k| k == "__display_name" } else formatted_item = format_for_display(item) end result_items << formatted_item result_count += 1 end rescue Net::HTTPServerException => e msg = Chef::JSONCompat.from_json(e.response.body)["error"].first ui.error("knife search failed: #{msg}") exit 1 end if ui.interchange? output({ :results => result_count, :rows => result_items }) else ui.log "#{result_count} items found" ui.log("\n") result_items.each do |item| output(item) unless config[:id_only] ui.msg("\n") end end end end |