Skip to content

Allow usage of file templates with stdlib::manage #1422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 77 additions & 11 deletions manifests/manage.pp
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,54 @@
#
# @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:
# file:
# '/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
Expand All @@ -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 })
}
}
}
}
}
19 changes: 19 additions & 0 deletions spec/classes/manage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' => {
Expand All @@ -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
Loading