diff --git a/lib/puppet/functions/query_facts.rb b/lib/puppet/functions/query_facts.rb index eceb1db..66a32fa 100644 --- a/lib/puppet/functions/query_facts.rb +++ b/lib/puppet/functions/query_facts.rb @@ -32,7 +32,7 @@ def query_facts(query, facts) uri = URI(Puppet::Util::Puppetdb.config.server_urls.first) puppetdb = PuppetDB::Connection.new(uri.host, uri.port, uri.scheme == 'https') parser = PuppetDB::Parser.new - query = parser.facts_query query, facts_for_query if query.is_a? String + query = parser.facts_query query, facts_for_query.uniq if query.is_a? String parser.facts_hash(puppetdb.query(:facts, query, :extract => [:certname, :name, :value]), facts) end end diff --git a/lib/puppetdb/parser_helper.rb b/lib/puppetdb/parser_helper.rb index 7a1f43e..146dc1a 100644 --- a/lib/puppetdb/parser_helper.rb +++ b/lib/puppetdb/parser_helper.rb @@ -52,25 +52,35 @@ def facts_hash(fact_hash, facts) [fact['name'], fact['value']] else - # Find the set of keys where the first value is the fact name - nested_keys = facts.select do |x| - x.is_a?(Array) && x.first == fact['name'] - end.flatten + # Group each set of keys individually and create unique names + nested_keys_groups = facts.select { |x| x.is_a?(Array) && x.first == fact['name'] } + nested_results = nested_keys_groups.map do |nested_keys| + [ + nested_keys.join("_"), + extract_nested_fact([fact], nested_keys[1..-1]).first + ] + end - # Join all the key names together with an underscore to give - # us a unique name, and then send all the keys but the fact - # name (which was already queried out) to extract_nested_fact - [ - nested_keys.join("_"), - extract_nested_fact([fact], nested_keys[1..-1]).first - ] + # Return the nested results as an array of hashes to be added individually + nested_results.each do |nested_name, nested_value| + if ret.include? fact['certname'] + ret[fact['certname']][nested_name] = nested_value + else + ret[fact['certname']] = { nested_name => nested_value } + end + end + + ret end - if ret.include? fact['certname'] - ret[fact['certname']][name] = value - else - ret[fact['certname']] = { name => value } + if name && value + if ret.include? fact['certname'] + ret[fact['certname']][name] = value + else + ret[fact['certname']] = { name => value } + end end + ret end end