Skip to content

Commit

Permalink
Fixes #30078 - add parameter to accept a hostgroup config hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Patel authored and Anand Patel committed Jun 25, 2020
1 parent 7462292 commit 9ea04e1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
22 changes: 19 additions & 3 deletions manifests/plugin/default_hostgroup.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
# Installs foreman_default_hostgroup plugin
class foreman::plugin::default_hostgroup {
foreman::plugin {'default_hostgroup': }
# @summary This class installs the default_hostgroup plugin and optionally manages the configuration file
#
# @param hostgroups An array of hashes of hostgroup names and facts to add to the configuration
#
class foreman::plugin::default_hostgroup (
Array[Hash] $hostgroups = [],
){
if empty($hostgroups) {
$config = undef
$config_file = undef
} else {
$config = template('foreman/default_hostgroup.yaml.erb')
$config_file = "${foreman::plugin_config_dir}/default_hostgroup.yaml"
}

foreman::plugin {'default_hostgroup':
config => $config,
config_file => $config_file,
}
}
41 changes: 41 additions & 0 deletions spec/classes/plugin/default_hostgroup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,45 @@

describe 'foreman::plugin::default_hostgroup' do
include_examples 'basic foreman plugin tests', 'default_hostgroup'

context 'with user provided config hash' do
let(:params) do
{
:hostgroups => [
'Redhat' => {
'osfamily'=> 'RedHat',
'lsbdistcodename' => 'Santiago',
},
'Osx/common' => {
'osfamily' => 'Darwin',
},
'Base' => {
'hostname' => '.*'
}
]
}
end

hostgroups = <<EOF
---
:default_hostgroup:
:facts_map:
'Redhat':
'lsbdistcodename': 'Santiago'
'osfamily': 'RedHat'
'Osx/common':
'osfamily': 'Darwin'
'Base':
'hostname': '.*'
EOF

it { is_expected.to compile.with_all_deps }
it { is_expected.to contain_file('/etc/foreman/plugins/default_hostgroup.yaml').with_content(hostgroups) }
end

context 'no config hash' do
it { is_expected.not_to contain_file('/etc/foreman/plugins/default_hostgroup.yaml') }
end

end

11 changes: 11 additions & 0 deletions templates/default_hostgroup.yaml.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
:default_hostgroup:
:facts_map:
<% @hostgroups.each do |group| -%>
<% group.each do |name, value| -%>
'<%= name %>':
<% value.sort.each do |fact_name, fact_value| -%>
'<%= fact_name %>': '<%= fact_value %>'
<% end -%>
<% end -%>
<% end -%>

0 comments on commit 9ea04e1

Please sign in to comment.