Skip to content

Handle JSON facts structured as name/values #242

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

Merged
merged 1 commit into from
Feb 16, 2021
Merged
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
15 changes: 13 additions & 2 deletions lib/octocatalog-diff/facts/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ class JSON
# @return [Hash] Facts
def self.fact_retriever(options = {}, node = '')
facts = ::JSON.parse(options.fetch(:fact_file_string))
node = facts.fetch('fqdn', 'unknown.node') if node.empty?
{ 'name' => node, 'values' => facts }

if facts.keys.include?('name') && facts.keys.include?('values') && facts['values'].is_a?(Hash)
# If you saved the output of something like
# `puppet facts find $(hostname)` the structure will already be a
# {'name' => <fqdn>, 'values' => <hash of facts>}. We do nothing
# here because we don't want to double-encode.
else
facts = { 'name' => node, 'values' => facts }
end

facts['name'] = node unless node.empty?
facts['name'] = facts['values'].fetch('fqdn', 'unknown.node') if facts['name'].empty?
facts
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/octocatalog-diff/fixtures/facts/encoded-facts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name":"rspec-node.abc.github.net",
"values":{
"apt_update_last_success":1458162123,
"architecture":"amd64",
"datacenter":"xyz",
"fqdn":"rspec-node.xyz.github.net",
"_timestamp":"2014-12-02 14:56:20 -0600"
}
}
25 changes: 25 additions & 0 deletions spec/octocatalog-diff/tests/facts/json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,30 @@
expect(result['name']).to eq('override.node')
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
end

it 'should fill in a missing name from hash with name/values' do
fact_file = OctocatalogDiff::Spec.fixture_path('facts/encoded-facts.json')
data = JSON.parse(File.read(fact_file))
data['name'] = ''
options = {
fact_file_string: JSON.generate(data)
}
result = OctocatalogDiff::Facts::JSON.fact_retriever(options)
expect(result).to be_a_kind_of(Hash)
expect(result['name']).to eq('rspec-node.xyz.github.net')
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
end

it 'should not double-encode hash already containing name and values' do
fact_file = OctocatalogDiff::Spec.fixture_path('facts/encoded-facts.json')
options = {
fact_file_string: File.read(fact_file)
}
result = OctocatalogDiff::Facts::JSON.fact_retriever(options)
expect(result).to be_a_kind_of(Hash)
expect(result.keys).to match_array(%w(name values))
expect(result['name']).to eq('rspec-node.abc.github.net')
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
end
end
end