Skip to content

Commit 6701b4f

Browse files
authored
Merge pull request #242 from kpaulisse/kpaulisse-json-facts
Handle JSON facts structured as name/values
2 parents 0ba9924 + a39f561 commit 6701b4f

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

lib/octocatalog-diff/facts/json.rb

+13-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,19 @@ class JSON
1414
# @return [Hash] Facts
1515
def self.fact_retriever(options = {}, node = '')
1616
facts = ::JSON.parse(options.fetch(:fact_file_string))
17-
node = facts.fetch('fqdn', 'unknown.node') if node.empty?
18-
{ 'name' => node, 'values' => facts }
17+
18+
if facts.keys.include?('name') && facts.keys.include?('values') && facts['values'].is_a?(Hash)
19+
# If you saved the output of something like
20+
# `puppet facts find $(hostname)` the structure will already be a
21+
# {'name' => <fqdn>, 'values' => <hash of facts>}. We do nothing
22+
# here because we don't want to double-encode.
23+
else
24+
facts = { 'name' => node, 'values' => facts }
25+
end
26+
27+
facts['name'] = node unless node.empty?
28+
facts['name'] = facts['values'].fetch('fqdn', 'unknown.node') if facts['name'].empty?
29+
facts
1930
end
2031
end
2132
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name":"rspec-node.abc.github.net",
3+
"values":{
4+
"apt_update_last_success":1458162123,
5+
"architecture":"amd64",
6+
"datacenter":"xyz",
7+
"fqdn":"rspec-node.xyz.github.net",
8+
"_timestamp":"2014-12-02 14:56:20 -0600"
9+
}
10+
}

spec/octocatalog-diff/tests/facts/json_spec.rb

+25
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,30 @@
3939
expect(result['name']).to eq('override.node')
4040
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
4141
end
42+
43+
it 'should fill in a missing name from hash with name/values' do
44+
fact_file = OctocatalogDiff::Spec.fixture_path('facts/encoded-facts.json')
45+
data = JSON.parse(File.read(fact_file))
46+
data['name'] = ''
47+
options = {
48+
fact_file_string: JSON.generate(data)
49+
}
50+
result = OctocatalogDiff::Facts::JSON.fact_retriever(options)
51+
expect(result).to be_a_kind_of(Hash)
52+
expect(result['name']).to eq('rspec-node.xyz.github.net')
53+
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
54+
end
55+
56+
it 'should not double-encode hash already containing name and values' do
57+
fact_file = OctocatalogDiff::Spec.fixture_path('facts/encoded-facts.json')
58+
options = {
59+
fact_file_string: File.read(fact_file)
60+
}
61+
result = OctocatalogDiff::Facts::JSON.fact_retriever(options)
62+
expect(result).to be_a_kind_of(Hash)
63+
expect(result.keys).to match_array(%w(name values))
64+
expect(result['name']).to eq('rspec-node.abc.github.net')
65+
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
66+
end
4267
end
4368
end

0 commit comments

Comments
 (0)