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 b114dba
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
19 changes: 16 additions & 3 deletions manifests/plugin/default_hostgroup.pp
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
# 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 hash of hostgroup names and facts to add to the configuration
#
class foreman::plugin::default_hostgroup (
Hash[String, Hash[String, Variant[String, Integer]]] $hostgroups = {},
){
if $hostgroups {
$config = template('foreman/default_hostgroup.yaml.erb')
} else {
$config = undef
}

foreman::plugin {'default_hostgroup':
config => $config,
}
}
34 changes: 34 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,38 @@

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

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

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

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

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

end

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

0 comments on commit b114dba

Please sign in to comment.