-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add facter::structured_data_fact to allow for structured data
- Loading branch information
1 parent
c44e36e
commit 3d9ae52
Showing
7 changed files
with
326 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# @summary Define YAML based external structured data facts. | ||
# | ||
# @param data | ||
# A hash of facts. All keys must be strings. | ||
# | ||
# @param file | ||
# File in which the fact will be placed. The file name must end with '.yaml'. | ||
# | ||
# @param facts_dir | ||
# Directory in which the file will be placed. If not specified, use the | ||
# default facts_d_dir. | ||
# | ||
define facter::structured_data_fact ( | ||
Hash[String[1], Data] $data, | ||
Pattern[/\.yaml*\Z/] $file = 'facts.yaml', | ||
Optional[Stdlib::Absolutepath] $facts_dir = undef, | ||
) { | ||
|
||
include facter | ||
|
||
$facts_dir_path = pick($facts_dir, $facter::facts_d_dir) | ||
if $facts['os']['family'] == 'windows' { | ||
$facts_file_path = "${facts_dir_path}\\${file}" | ||
} else { | ||
$facts_file_path = "${facts_dir_path}/${file}" | ||
} | ||
|
||
file { "structured_data_fact_${file}": | ||
ensure => file, | ||
path => $facts_file_path, | ||
content => template('facter/structured_data_fact.erb'), | ||
owner => $facter::facts_file_owner, | ||
group => $facter::facts_file_group, | ||
mode => $facter::facts_file_mode, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
require 'spec_helper' | ||
|
||
describe 'facter::structured_data_fact' do | ||
context 'on RedHat' do | ||
redhat = { | ||
supported_os: [ | ||
{ | ||
'operatingsystem' => 'RedHat', | ||
'operatingsystemrelease' => ['7'], | ||
}, | ||
], | ||
} | ||
|
||
on_supported_os(redhat).each do |_os, os_facts| | ||
let(:facts) do | ||
os_facts | ||
end | ||
|
||
context 'with file and facts_dir specified' do | ||
let(:title) { 'fact1' } | ||
let(:params) do | ||
{ | ||
:data => { | ||
'my_array' => ['one', 'two', 'three'], | ||
'my_hash' => { 'k' => 'v' }, | ||
}, | ||
:file => 'custom.yaml', | ||
:facts_dir => '/factsdir', | ||
} | ||
end | ||
|
||
it { should contain_class('facter') } | ||
|
||
# These must exist or the coverage report lists these incorrectly as | ||
# untouched resources. These resources are all from the facter class. | ||
it { should contain_file('facts_file') } | ||
it { should contain_file('facts_d_directory') } | ||
it { should contain_exec('mkdir_p-/etc/facter/facts.d') } | ||
|
||
content = <<-END.gsub(/^\s+\|/, '') | ||
|# This file is being maintained by Puppet. | ||
|# DO NOT EDIT | ||
|--- | ||
|my_array: | ||
|- one | ||
|- two | ||
|- three | ||
|my_hash: | ||
| k: v | ||
END | ||
|
||
it { | ||
should contain_file('structured_data_fact_custom.yaml').with({ | ||
'ensure' => 'file', | ||
'path' => '/factsdir/custom.yaml', | ||
'content' => content, | ||
'owner' => 'root', | ||
'group' => 'root', | ||
'mode' => '0644', | ||
}) | ||
} | ||
end | ||
end # end on_supported_os(redhat) | ||
end # context "on RedHat" | ||
|
||
context 'on windows' do | ||
windows = { | ||
supported_os: [ | ||
{ | ||
'operatingsystem' => 'windows', | ||
'operatingsystemrelease' => ['2016'], | ||
}, | ||
], | ||
} | ||
|
||
on_supported_os(windows).each do |_os, os_facts| | ||
let(:facts) do | ||
os_facts | ||
end | ||
|
||
context 'with fact and facts_dir specified' do | ||
let(:title) { 'fact1' } | ||
let(:params) do | ||
{ | ||
:data => { | ||
'my_array' => ['one', 'two', 'three'], | ||
'my_hash' => { 'k' => 'v' }, | ||
}, | ||
:file => 'custom.yaml', | ||
:facts_dir => 'C:\factsdir', | ||
} | ||
end | ||
|
||
# These must exist or the coverage report lists these incorrectly as | ||
# untouched resources. These resources are all from the facter class. | ||
it { should contain_exec('mkdir_p-C:\ProgramData\PuppetLabs\facter\facts.d') } | ||
|
||
content = <<-END.gsub(/^\s+\|/, '') | ||
|# This file is being maintained by Puppet. | ||
|# DO NOT EDIT | ||
|--- | ||
|my_array: | ||
|- one | ||
|- two | ||
|- three | ||
|my_hash: | ||
| k: v | ||
END | ||
|
||
it { | ||
should contain_file('structured_data_fact_custom.yaml').with({ | ||
'ensure' => 'file', | ||
'content' => content, | ||
'path' => 'C:\factsdir\custom.yaml', | ||
'owner' => 'NT AUTHORITY\SYSTEM', | ||
'group' => 'NT AUTHORITY\SYSTEM', | ||
}) | ||
} | ||
end | ||
end # end on_supported_os(windows) | ||
end # context "on windows" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# This file is being maintained by Puppet. | ||
# DO NOT EDIT | ||
<%= @data.to_yaml -%> |