diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-30 14:40:02 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2017-03-31 13:03:08 -0700 |
commit | 5fb94bf7bd7d22fd3a1cff22f5861b0fbf32adb1 (patch) | |
tree | 2af84f37a5de0fff1f167f01bdb068274fe6b153 | |
parent | ac21083e7480c90a79cad36f05da973bc681f110 (diff) | |
download | chef-5fb94bf7bd7d22fd3a1cff22f5861b0fbf32adb1.tar.gz |
apply knife search node fuzzifier to knife ssh
extracts out a common 'fuzz' option to Chef::Search::Query so that anyone can
use it.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r-- | lib/chef/knife/search.rb | 13 | ||||
-rw-r--r-- | lib/chef/knife/ssh.rb | 4 | ||||
-rw-r--r-- | lib/chef/search/query.rb | 18 |
3 files changed, 22 insertions, 13 deletions
diff --git a/lib/chef/knife/search.rb b/lib/chef/knife/search.rb index 7fc76b28c0..654f9ad498 100644 --- a/lib/chef/knife/search.rb +++ b/lib/chef/knife/search.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2009-2016, Chef Software Inc. +# Copyright:: Copyright 2009-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -73,19 +73,18 @@ class Chef def run read_cli_args - fuzzify_query if @type == "node" ui.use_presenter Knife::Core::NodePresenter end q = Chef::Search::Query.new - escaped_query = Addressable::URI.encode_component(@query, Addressable::URI::CharacterClasses::QUERY) result_items = [] result_count = 0 search_args = Hash.new + search_args[:fuzz] = true search_args[:start] = config[:start] if config[:start] search_args[:rows] = config[:rows] if config[:rows] if config[:filter_result] @@ -95,7 +94,7 @@ class Chef end begin - q.search(@type, escaped_query, search_args) do |item| + q.search(@type, @query, search_args) do |item| formatted_item = Hash.new if item.is_a?(Hash) # doing a little magic here to set the correct name @@ -151,12 +150,6 @@ class Chef end end - 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 - # 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: diff --git a/lib/chef/knife/ssh.rb b/lib/chef/knife/ssh.rb index d79565991f..f048c7268d 100644 --- a/lib/chef/knife/ssh.rb +++ b/lib/chef/knife/ssh.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2009-2016, Chef Software Inc. +# Copyright:: Copyright 2009-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -223,7 +223,7 @@ class Chef end @search_count = 0 - query.search(:node, @name_args[0], filter_result: required_attributes) do |item| + query.search(:node, @name_args[0], filter_result: required_attributes, fuzz: true) do |item| @search_count += 1 # we should skip the loop to next iteration if the item # returned by the search is nil diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb index 7357dbf6be..7e70584f30 100644 --- a/lib/chef/search/query.rb +++ b/lib/chef/search/query.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright 2008-2016, Chef Software Inc. +# Copyright:: Copyright 2008-2017, Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -63,6 +63,14 @@ class Chef validate_type(type) args_h = hashify_args(*args) + if args_h.key?(:fuzz) + if type == :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 + response = call_rest_service(type, query: query, **args_h) if block @@ -92,6 +100,14 @@ class Chef private + 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 + 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." + |