Skip to content

Commit d1f8b62

Browse files
authored
Merge pull request #129 from github/kpaulisse-unstructured-facts
Allow YAML facts in "facter -y" format
2 parents 602f179 + 75568bb commit d1f8b62

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

lib/octocatalog-diff/facts/yaml.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ def self.fact_retriever(options = {}, node = '')
2121
fact_file_data = fact_file_string.split(/\n/)
2222
fact_file_data[0] = '---' if fact_file_data[0] =~ /^---/
2323

24-
# Load and return the parsed fact file.
25-
result = YAML.load(fact_file_data.join("\n"))
24+
# Load the parsed fact file.
25+
parsed = YAML.load(fact_file_data.join("\n"))
26+
27+
# This is a handler for a YAML file that has just the facts and none of the
28+
# structure. For example if you saved the output of `facter -y` to a file and
29+
# are passing that in, this will work.
30+
result = if parsed.key?('name') && parsed.key?('values')
31+
parsed
32+
else
33+
{ 'name' => node || parsed['fqdn'] || '', 'values' => parsed }
34+
end
35+
2636
result['name'] = node unless node == ''
2737
result
2838
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
apt_update_last_success: 1458162123
3+
architecture: amd64
4+
datacenter: xyz
5+
fqdn: rspec-node.xyz.github.net
6+
"_timestamp": 2014-12-02 12:56:20.865795 -08:00
7+
ec2_metadata:
8+
ami-id: ami-deadbeef
9+
ami-launch-index: "0"
10+
ami-manifest-path: "(unknown)"
11+
block-device-mapping:
12+
ami: /dev/sda1
13+
ephemeral0: sdb
14+
ephemeral1: sdc
15+
root: /dev/sda1

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,17 @@
3737
expect(result['name']).to eq('override.node')
3838
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
3939
end
40+
41+
it 'should convert unstructured facts into structured facts' do
42+
fact_file = OctocatalogDiff::Spec.fixture_path('facts/unstructured.yaml')
43+
options = {
44+
fact_file_string: File.read(fact_file)
45+
}
46+
result = OctocatalogDiff::Facts::Yaml.fact_retriever(options, 'override.node')
47+
expect(result).to be_a_kind_of(Hash)
48+
expect(result['name']).to eq('override.node')
49+
expect(result['values']['fqdn']).to eq('rspec-node.xyz.github.net')
50+
expect(result['values']['ec2_metadata']['block-device-mapping']['ephemeral0']).to eq('sdb')
51+
end
4052
end
4153
end

0 commit comments

Comments
 (0)