summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2017-03-31 13:10:58 -0700
committerGitHub <noreply@github.com>2017-03-31 13:10:58 -0700
commit4c9a6def08a1bd3611ed8eb2c3a1d47760365041 (patch)
tree22c003cfeb8338a85e87d0d73ccdcbbbb55fa839
parentac21083e7480c90a79cad36f05da973bc681f110 (diff)
parent56ce47e3fa9de14f84c4a2959149239125630d56 (diff)
downloadchef-4c9a6def08a1bd3611ed8eb2c3a1d47760365041.tar.gz
Merge pull request #5977 from chef/lcg/ssh-fuzzifier
apply knife search node fuzzifier to knife ssh
-rw-r--r--RELEASE_NOTES.md7
-rw-r--r--lib/chef/knife/search.rb13
-rw-r--r--lib/chef/knife/ssh.rb4
-rw-r--r--lib/chef/search/query.rb18
-rw-r--r--spec/unit/search/query_spec.rb23
5 files changed, 51 insertions, 14 deletions
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index a1e5ceab45..3f3d8c44bb 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -157,3 +157,10 @@ This can be used by any other resource by just overriding the name property and
Notifications to resources with empty strings as their name is also supported via either the bare resource name (`apt_update` --
matches what the user types in the DSL) or with empty brackets (`apt_update[]` -- matches the resource notification pattern).
+### The knife ssh command applies the same fuzzifier as knife search node
+
+A bare name to knife search node will search for the name in `tags`, `roles`, `fqdn`, `addresses`, `policy_name` or `policy_group` fields and will
+match when given partial strings (available since Chef 11). The `knife ssh` search term has been similarly extended so that the
+search API matches in both cases. The node search fuzzifier has also been extracted out to a `fuzz` option to Chef::Search::Query for re-use
+elsewhere.
+
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..6f494819ba 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[: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." +
diff --git a/spec/unit/search/query_spec.rb b/spec/unit/search/query_spec.rb
index 0837410b3c..95221870d5 100644
--- a/spec/unit/search/query_spec.rb
+++ b/spec/unit/search/query_spec.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");
@@ -237,6 +237,27 @@ describe Chef::Search::Query do
end
end
+ it "fuzzifies node searches when fuzz is set" do
+ expect(rest).to receive(:get).with(
+ "search/node?q=tags:*free.messi*%20OR%20roles:*free.messi*%20OR%20fqdn:*free.messi*%20OR%20addresses:*free.messi*%20OR%20policy_name:*free.messi*%20OR%20policy_group:*free.messi*&start=0"
+ ).and_return(response)
+ query.search(:node, "free.messi", fuzz: true)
+ end
+
+ it "does not fuzzify node searches when fuzz is not set" do
+ expect(rest).to receive(:get).with(
+ "search/node?q=free.messi&start=0"
+ ).and_return(response)
+ query.search(:node, "free.messi")
+ end
+
+ it "does not fuzzify client searches" do
+ expect(rest).to receive(:get).with(
+ "search/client?q=messi&start=0"
+ ).and_return(response)
+ query.search(:client, "messi", fuzz: true)
+ end
+
context "when :filter_result is provided as a result" do
include_context "filtered search" do
let(:filter_key) { :filter_result }