Skip to content

Commit d49a4a2

Browse files
committed
Merge pull request acidprime#5 from raphink/dev/fact_search
Allow multiple facts in filter
2 parents d1c8ab6 + 5fa20bc commit d49a4a2

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/puppet/catalog-diff/searchfacts.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
module Puppet::CatalogDiff
55
class SearchFacts
66

7-
def initialize(args)
8-
@args = args
7+
def initialize(facts)
8+
@facts = Hash[facts.split(',').map { |f| f.split('=') }]
99
end
1010

1111
def find_nodes(options = {})
1212
# Pull all nodes from the yaml cache
1313
# Then validate they are active nodes against the rest of puppetdb api
14-
yaml_cache = find_nodes_local(*@args.split("="))
1514
if options[:use_puppetdb]
1615
active_nodes = find_nodes_puppetdb()
1716
else
@@ -21,13 +20,14 @@ def find_nodes(options = {})
2120
raise "No active nodes were returned from your fact search"
2221
end
2322
if options[:filter_local]
23+
yaml_cache = find_nodes_local()
2424
yaml_cache.select { |node| active_nodes.include?(node) }
2525
else
2626
active_nodes
2727
end
2828
end
2929

30-
def find_nodes_local(fact,value)
30+
def find_nodes_local
3131
Puppet[:clientyamldir] = Puppet[:yamldir]
3232
if Puppet::Node.respond_to? :terminus_class
3333
Puppet::Node.terminus_class = :yaml
@@ -36,15 +36,18 @@ def find_nodes_local(fact,value)
3636
Puppet::Node.indirection.terminus_class = :yaml
3737
nodes = Puppet::Node.indirection.search("*")
3838
end
39-
unless filtered = nodes.select {|n| n.parameters[fact] == value }.map{ |n| n.name }
39+
unless filtered = nodes.select {|n|
40+
@facts.select { |f, v| n[f] == v }.size == @facts.size
41+
}.map{ |n| n.name }
4042
raise "No matching nodes found using yaml terminus"
4143
end
4244
filtered
4345
end
4446

4547

4648
def find_nodes_rest(server)
47-
endpoint = "/v2/facts_search/search?facts.#{@args}"
49+
query = @facts.map { |k, v| "facts.#{k}=#{v}" }.join('&')
50+
endpoint = "/v2/facts_search/search?#{query}"
4851

4952
begin
5053
connection = Puppet::Network::HttpPool.http_instance(server,'8140')
@@ -63,9 +66,9 @@ def find_nodes_rest(server)
6366

6467
def find_nodes_puppetdb()
6568
connection = Puppet::Network::HttpPool.http_instance(Puppet::Util::Puppetdb.server,'8081')
66-
fact_query = @args.split("=")
67-
#json_query = URI.escape(["=", ["fact", fact_query[0]], fact_query[1]].to_json)
68-
json_query = URI.escape(["=", ["node","active"], true].to_json)
69+
base_query = ["and", ["=", ["node","active"], true]]
70+
query = base_query.concat(@facts.map { |k, v| ["=", ["fact", k], v] })
71+
json_query = URI.escape(query.to_json)
6972
unless filtered = PSON.load(connection.request_get("/v2/nodes/?query=#{json_query}", {"Accept" => 'application/json'}).body)
7073
raise "Error parsing json output of puppet search"
7174
end

0 commit comments

Comments
 (0)