Skip to content

Commit 5e4a66b

Browse files
authored
Merge pull request #1422 from tuxmea/manage_file_templates
Allow usage of file templates with stdlib::manage
2 parents 76e9ddc + bd6f29f commit 5e4a66b

File tree

2 files changed

+96
-11
lines changed

2 files changed

+96
-11
lines changed

Diff for: manifests/manage.pp

+77-11
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,54 @@
1010
#
1111
# @param create_resources
1212
# A hash of resources to create
13-
# NOTE: functions, such as `template` or `epp`, are not evaluated.
13+
# NOTE: functions, such as `template` or `epp`, are not directly evaluated
14+
# but processed as Puppet code based on epp and erb hash keys.
1415
#
1516
# @example
1617
# class { 'stdlib::manage':
17-
# 'create_resources' => {
18-
# 'file' => {
19-
# '/etc/motd.d/hello' => {
20-
# 'content' => 'I say Hi',
21-
# 'notify' => 'Service[sshd]',
18+
# 'create_resources' => {
19+
# 'file' => {
20+
# '/etc/motd.d/hello' => {
21+
# 'content' => 'I say Hi',
22+
# 'notify' => 'Service[sshd]',
23+
# },
24+
# '/etc/motd' => {
25+
# 'ensure' => 'file',
26+
# 'epp' => {
27+
# 'template' => 'profile/motd.epp',
2228
# }
2329
# },
24-
# 'package' => {
25-
# 'example' => {
26-
# 'ensure' => 'installed',
27-
# 'subscribe' => ['Service[sshd]', 'Exec[something]'],
30+
# '/etc/information' => {
31+
# 'ensure' => 'file',
32+
# 'erb' => {
33+
# 'template' => 'profile/informaiton.erb',
2834
# }
2935
# }
36+
# },
37+
# 'package' => {
38+
# 'example' => {
39+
# 'ensure' => 'installed',
40+
# 'subscribe' => ['Service[sshd]', 'Exec[something]'],
41+
# }
3042
# }
43+
# }
44+
# }
3145
#
3246
# @example
3347
# stdlib::manage::create_resources:
3448
# file:
3549
# '/etc/motd.d/hello':
3650
# content: I say Hi
3751
# notify: 'Service[sshd]'
52+
# '/etc/motd':
53+
# ensure: 'file'
54+
# epp:
55+
# template: 'profile/motd.epp'
56+
# context: {}
57+
# '/etc/information':
58+
# ensure: 'file'
59+
# erb:
60+
# template: 'profile/information.erb'
3861
# package:
3962
# example:
4063
# ensure: installed
@@ -46,7 +69,50 @@
4669
) {
4770
$create_resources.each |$type, $resources| {
4871
$resources.each |$title, $attributes| {
49-
create_resources($type, { $title => $attributes })
72+
case $type {
73+
'file': {
74+
# sanity checks
75+
# epp, erb and content are exclusive
76+
if 'epp' in $attributes and 'content' in $attributes {
77+
fail("You can not set 'epp' and 'content' for file ${title}")
78+
}
79+
if 'erb' in $attributes and 'content' in $attributes {
80+
fail("You can not set 'erb' and 'content' for file ${title}")
81+
}
82+
if 'erb' in $attributes and 'epp' in $attributes {
83+
fail("You can not set 'erb' and 'epp' for file ${title}")
84+
}
85+
86+
if 'epp' in $attributes {
87+
if 'template' in $attributes['epp'] {
88+
if 'context' in $attributes['epp'] {
89+
$content = epp($attributes['epp']['template'], $attributes['epp']['context'])
90+
} else {
91+
$content = epp($attributes['epp']['template'])
92+
}
93+
} else {
94+
fail("No template configured for epp for file ${title}")
95+
}
96+
} elsif 'erb' in $attributes {
97+
if 'template' in $attributes['erb'] {
98+
$content = template($attributes['erb']['template'])
99+
} else {
100+
fail("No template configured for erb for file ${title}")
101+
}
102+
} elsif 'content' in $attributes {
103+
$content = $attributes['content']
104+
} else {
105+
$content = undef
106+
}
107+
file { $title:
108+
* => $attributes - 'erb' - 'epp' - 'content',
109+
content => $content,
110+
}
111+
}
112+
default: {
113+
create_resources($type, { $title => $attributes })
114+
}
115+
}
50116
}
51117
}
52118
}

Diff for: spec/classes/manage_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@
2525
'/etc/motd.d/hello' => {
2626
'content' => 'I say Hi',
2727
'notify' => 'Service[sshd]'
28+
},
29+
'/etc/motd' => {
30+
'epp' => {
31+
'template' => 'profile/motd.epp'
32+
}
33+
},
34+
'/etc/information' => {
35+
'erb' => {
36+
'template' => 'profile/information.erb'
37+
}
2838
}
2939
},
3040
'package' => {
@@ -37,8 +47,17 @@
3747
}
3848
end
3949

50+
Puppet::Functions.create_function(:epp) do
51+
return 'I am an epp template'
52+
end
53+
Puppet::Functions.create_function(:template) do
54+
return 'I am an erb template'
55+
end
56+
4057
it { is_expected.to compile }
4158
it { is_expected.to contain_file('/etc/motd.d/hello').with_content('I say Hi').with_notify('Service[sshd]') }
59+
it { is_expected.to contain_file('/etc/motd').with_content(%r{I am an epp template}) }
60+
it { is_expected.to contain_file('/etc/information').with_content(%r{I am an erb template}) }
4261
it { is_expected.to contain_package('example').with_ensure('installed').that_subscribes_to(['Service[sshd]', 'File[/etc/motd.d]']) }
4362
end
4463
end

0 commit comments

Comments
 (0)