Skip to content

Improvement of Nested Fact Handling in facts_hash #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/puppet/functions/query_facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 25 additions & 15 deletions lib/puppetdb/parser_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down