diff options
author | Claire McQuin <claire@getchef.com> | 2014-12-19 12:45:51 -0800 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-12-19 12:45:51 -0800 |
commit | 8261791ced25227781f0d0ad3f44ef77c889d53b (patch) | |
tree | 8d53acd234d25cb559d61937a27757f9bf78f18a | |
parent | fe8ca9ea64205288d3cc0fa2ea6b80c953f8ea4c (diff) | |
download | chef-mcquin/bugfix/search-filtering.tar.gz |
Build configurable search string.mcquin/bugfix/search-filtering
-rw-r--r-- | lib/chef/search/query.rb | 10 | ||||
-rw-r--r-- | spec/functional/knife/ssh_spec.rb | 2 | ||||
-rw-r--r-- | spec/unit/search/query_spec.rb | 10 |
3 files changed, 15 insertions, 7 deletions
diff --git a/lib/chef/search/query.rb b/lib/chef/search/query.rb index b68749f196..8656e810db 100644 --- a/lib/chef/search/query.rb +++ b/lib/chef/search/query.rb @@ -124,8 +124,16 @@ WARNDEP s && URI.escape(s.to_s) end + def create_query_string(type, query, rows, start, sort) + qstr = "search/#{type}?q=#{escape(query)}" + qstr += "&sort=#{escape(sort)}" if sort + qstr += "&start=#{escape(start)}" if start + qstr += "&rows=#{escape(rows)}" if rows + qstr + end + def call_rest_service(type, query:'*:*', rows:nil, start:0, sort:'X_CHEF_id_CHEF_X asc', filter_result:nil) - query_string = "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}#{"&rows=" + escape(rows) if rows}" + query_string = create_query_string(type, query, rows, start, sort) if filter_result response = rest.post_rest(query_string, filter_result) diff --git a/spec/functional/knife/ssh_spec.rb b/spec/functional/knife/ssh_spec.rb index eaef4d7e1c..5b8ad6f368 100644 --- a/spec/functional/knife/ssh_spec.rb +++ b/spec/functional/knife/ssh_spec.rb @@ -260,7 +260,7 @@ describe Chef::Knife::Ssh do Chef::Config[:client_key] = nil Chef::Config[:chef_server_url] = 'http://localhost:9000' - @api.get("/search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=", 200) { + @api.get("/search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0", 200) { %({"total":1, "start":0, "rows":[{"name":"i-xxxxxxxx", "json_class":"Chef::Node", "automatic":{"fqdn":"the.fqdn", "ec2":{"public_hostname":"the_public_hostname"}},"recipes":[]}]}) } end diff --git a/spec/unit/search/query_spec.rb b/spec/unit/search/query_spec.rb index e426ddd25d..2fb197b183 100644 --- a/spec/unit/search/query_spec.rb +++ b/spec/unit/search/query_spec.rb @@ -24,7 +24,7 @@ describe Chef::Search::Query do let(:query) { Chef::Search::Query.new } shared_context "filtered search" do - let(:query_string) { "search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=" } + let(:query_string) { "search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0" } let(:server_url) { "https://api.opscode.com/organizations/opscode/nodes" } let(:args) { { filter_key => filter_hash } } let(:filter_hash) { @@ -147,22 +147,22 @@ describe Chef::Search::Query do end it "queries for every object of a type by default" do - expect(rest).to receive(:get_rest).with("search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=").and_return(response) + expect(rest).to receive(:get_rest).with("search/node?q=*:*&sort=X_CHEF_id_CHEF_X%20asc&start=0").and_return(response) query.search(:node) end it "allows a custom query" do - expect(rest).to receive(:get_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0&rows=").and_return(response) + expect(rest).to receive(:get_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=0").and_return(response) query.search(:node, "platform:rhel") end it "lets you set a sort order" do - expect(rest).to receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=0&rows=").and_return(response) + expect(rest).to receive(:get_rest).with("search/node?q=platform:rhel&sort=id%20desc&start=0").and_return(response) query.search(:node, "platform:rhel", sort: "id desc") end it "lets you set a starting object" do - expect(rest).to receive(:get_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=2&rows=").and_return(response) + expect(rest).to receive(:get_rest).with("search/node?q=platform:rhel&sort=X_CHEF_id_CHEF_X%20asc&start=2").and_return(response) query.search(:node, "platform:rhel", start: 2) end |