diff --git a/manifests/manage.pp b/manifests/manage.pp index 1324187a7..82c39bc8d 100644 --- a/manifests/manage.pp +++ b/manifests/manage.pp @@ -10,24 +10,38 @@ # # @param create_resources # A hash of resources to create -# NOTE: functions, such as `template` or `epp`, are not evaluated. +# NOTE: functions, such as `template` or `epp`, are not directly evaluated +# but processed as Puppet code based on epp and erb hash keys. # # @example # class { 'stdlib::manage': -# 'create_resources' => { -# 'file' => { -# '/etc/motd.d/hello' => { -# 'content' => 'I say Hi', -# 'notify' => 'Service[sshd]', +# 'create_resources' => { +# 'file' => { +# '/etc/motd.d/hello' => { +# 'content' => 'I say Hi', +# 'notify' => 'Service[sshd]', +# }, +# '/etc/motd' => { +# 'ensure' => 'file', +# 'epp' => { +# 'template' => 'profile/motd.epp', # } # }, -# 'package' => { -# 'example' => { -# 'ensure' => 'installed', -# 'subscribe' => ['Service[sshd]', 'Exec[something]'], +# '/etc/information' => { +# 'ensure' => 'file', +# 'erb' => { +# 'template' => 'profile/informaiton.erb', # } # } +# }, +# 'package' => { +# 'example' => { +# 'ensure' => 'installed', +# 'subscribe' => ['Service[sshd]', 'Exec[something]'], +# } # } +# } +# } # # @example # stdlib::manage::create_resources: @@ -35,6 +49,15 @@ # '/etc/motd.d/hello': # content: I say Hi # notify: 'Service[sshd]' +# '/etc/motd': +# ensure: 'file' +# epp: +# template: 'profile/motd.epp' +# context: {} +# '/etc/information': +# ensure: 'file' +# erb: +# template: 'profile/information.erb' # package: # example: # ensure: installed @@ -46,7 +69,50 @@ ) { $create_resources.each |$type, $resources| { $resources.each |$title, $attributes| { - create_resources($type, { $title => $attributes }) + case $type { + 'file': { + # sanity checks + # epp, erb and content are exclusive + if 'epp' in $attributes and 'content' in $attributes { + fail("You can not set 'epp' and 'content' for file ${title}") + } + if 'erb' in $attributes and 'content' in $attributes { + fail("You can not set 'erb' and 'content' for file ${title}") + } + if 'erb' in $attributes and 'epp' in $attributes { + fail("You can not set 'erb' and 'epp' for file ${title}") + } + + if 'epp' in $attributes { + if 'template' in $attributes['epp'] { + if 'context' in $attributes['epp'] { + $content = epp($attributes['epp']['template'], $attributes['epp']['context']) + } else { + $content = epp($attributes['epp']['template']) + } + } else { + fail("No template configured for epp for file ${title}") + } + } elsif 'erb' in $attributes { + if 'template' in $attributes['erb'] { + $content = template($attributes['erb']['template']) + } else { + fail("No template configured for erb for file ${title}") + } + } elsif 'content' in $attributes { + $content = $attributes['content'] + } else { + $content = undef + } + file { $title: + * => $attributes - 'erb' - 'epp' - 'content', + content => $content, + } + } + default: { + create_resources($type, { $title => $attributes }) + } + } } } } diff --git a/spec/classes/manage_spec.rb b/spec/classes/manage_spec.rb index a34fc7296..ce78ec2f0 100644 --- a/spec/classes/manage_spec.rb +++ b/spec/classes/manage_spec.rb @@ -25,6 +25,16 @@ '/etc/motd.d/hello' => { 'content' => 'I say Hi', 'notify' => 'Service[sshd]' + }, + '/etc/motd' => { + 'epp' => { + 'template' => 'profile/motd.epp' + } + }, + '/etc/information' => { + 'erb' => { + 'template' => 'profile/information.erb' + } } }, 'package' => { @@ -37,8 +47,17 @@ } end + Puppet::Functions.create_function(:epp) do + return 'I am an epp template' + end + Puppet::Functions.create_function(:template) do + return 'I am an erb template' + end + it { is_expected.to compile } it { is_expected.to contain_file('/etc/motd.d/hello').with_content('I say Hi').with_notify('Service[sshd]') } + it { is_expected.to contain_file('/etc/motd').with_content(%r{I am an epp template}) } + it { is_expected.to contain_file('/etc/information').with_content(%r{I am an erb template}) } it { is_expected.to contain_package('example').with_ensure('installed').that_subscribes_to(['Service[sshd]', 'File[/etc/motd.d]']) } end end