Skip to content

Commit

Permalink
Merge pull request #240 from chef/tm/efficencies
Browse files Browse the repository at this point in the history
Optimise queries when finding nodes
  • Loading branch information
thommay committed Nov 7, 2016
2 parents cb0efb3 + 5b3e325 commit 01dd0e6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
18 changes: 9 additions & 9 deletions lib/chef-vault/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,18 @@ def clients(search_or_client, action = :add)
else
results_returned = false
query = Chef::Search::Query.new
query.search(:node, search_or_client) do |node|
query.search(:node, search_or_client, filter_result: { name: ["name"] }, rows: 100000) do |node|
results_returned = true
case action
when :add
begin
client_key = load_actor(node.name, "clients")
client_key = load_actor(node["name"], "clients")
add_client(client_key)
rescue ChefVault::Exceptions::ClientNotFound
ChefVault::Log.warn "node '#{node.name}' has no private key; skipping"
ChefVault::Log.warn "node '#{node['name']}' has no private key; skipping"
end
when :delete
delete_client_or_node(node)
delete_client_or_node(node["name"])
else
raise ChefVault::Exceptions::KeysActionNotValid,
"#{action} is not a valid action"
Expand Down Expand Up @@ -416,7 +416,7 @@ def node_exists?(nodename)
# the node does not exist if a search for the node with that
# name returns no results
query = Chef::Search::Query.new
numresults = query.search(:node, "name:#{nodename}")[2]
numresults = query.search(:node, "name:#{nodename}", filter_result: { name: ["name"] }, rows: 0)[2]
return false unless numresults > 0
# if the node search does return results, predicate node
# existence on the existence of a like-named client
Expand Down Expand Up @@ -448,7 +448,7 @@ def handle_client_action(api_client, action)
client = load_actor(api_client.name, "clients")
add_client(client)
when :delete
delete_client_or_node(api_client)
delete_client_or_node(api_client.name)
end
end

Expand All @@ -460,10 +460,10 @@ def add_client(client)
end

# removes a client to the vault item keys
# @param client_or_node [Chef::ApiClient, Chef::Node] the API client or node to remove
# @param client_or_node [String] the name of the API client or node to remove
# @return [void]
def delete_client_or_node(client_or_node)
client = load_actor(client_or_node.name, "clients")
def delete_client_or_node(name)
client = load_actor(name, "clients")
keys.delete(client)
end
end
Expand Down
10 changes: 4 additions & 6 deletions spec/chef-vault/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
end

describe "#refresh" do
let(:node) { { "name" => "testnode" } }

it "saves only the keys" do
keys = double("keys",
Expand All @@ -184,7 +185,6 @@

item = ChefVault::Item.new("foo", "bar")

node = double("node", name: "testnode")
query = double("query")
allow(Chef::Search::Query).to receive(:new).and_return(query)
allow(query).to receive(:search).and_yield(node)
Expand All @@ -202,14 +202,13 @@

describe "#clients" do
context "when search returns a node with a valid client backing it and one without a valid client" do
let(:node_with_valid_client) { double("chef node valid") }
let(:node_without_valid_client) { double("chef node no valid client") }
let(:node_with_valid_client) { { "name" => "foo" } }
let(:node_without_valid_client) { { "name" => "bar" } }
let(:query_result) { double("chef search results") }
let(:client_key) { double("chef key") }

before do
# node with valid client proper loads client key
allow(node_with_valid_client).to receive(:name).and_return("foo")
allow(item).to receive(:load_actor).with("foo", "clients").and_return(client_key)
privkey = OpenSSL::PKey::RSA.new(1024)
pubkey = privkey.public_key
Expand All @@ -218,12 +217,11 @@
allow(client_key).to receive(:type).and_return("clients")

# node without client throws relevant error on key load
allow(node_without_valid_client).to receive(:name).and_return("bar")
allow(item).to receive(:load_actor).with("bar", "clients").and_raise(ChefVault::Exceptions::ClientNotFound)

allow(query_result)
.to receive(:search)
.with(Symbol, String)
.with(Symbol, String, Hash)
.and_yield(node_with_valid_client).and_yield(node_without_valid_client)
allow(Chef::Search::Query)
.to receive(:new)
Expand Down

0 comments on commit 01dd0e6

Please sign in to comment.