From a0c824ce7f770a032b72c97108a5c852a2cf3f39 Mon Sep 17 00:00:00 2001 From: Louis Charreau Date: Sun, 17 Nov 2024 09:20:25 +0100 Subject: [PATCH 1/2] Separates nested facts by generating unique names for each nested fact while preserving their connection to the parent fact. --- lib/puppet/functions/query_facts.rb | 2 +- lib/puppetdb/parser_helper.rb | 30 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 12 deletions(-) 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..5ef1d81 100644 --- a/lib/puppetdb/parser_helper.rb +++ b/lib/puppetdb/parser_helper.rb @@ -52,18 +52,26 @@ 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 + + # Return nil to skip the default assignment (handled by nested results) + next ret end if ret.include? fact['certname'] From dbe467f295517d2e452b92ae4458f65ab87c25cb Mon Sep 17 00:00:00 2001 From: Louis Charreau Date: Fri, 29 Nov 2024 11:09:25 +0100 Subject: [PATCH 2/2] Remove unnecessary 'next ret' for handling nested facts, simplify fact processing logic --- lib/puppetdb/parser_helper.rb | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/puppetdb/parser_helper.rb b/lib/puppetdb/parser_helper.rb index 5ef1d81..146dc1a 100644 --- a/lib/puppetdb/parser_helper.rb +++ b/lib/puppetdb/parser_helper.rb @@ -70,15 +70,17 @@ def facts_hash(fact_hash, facts) end end - # Return nil to skip the default assignment (handled by nested results) - next ret + 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