|
10 | 10 | #
|
11 | 11 | # @param create_resources
|
12 | 12 | # 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. |
14 | 15 | #
|
15 | 16 | # @example
|
16 | 17 | # 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', |
22 | 28 | # }
|
23 | 29 | # },
|
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', |
28 | 34 | # }
|
29 | 35 | # }
|
| 36 | +# }, |
| 37 | +# 'package' => { |
| 38 | +# 'example' => { |
| 39 | +# 'ensure' => 'installed', |
| 40 | +# 'subscribe' => ['Service[sshd]', 'Exec[something]'], |
| 41 | +# } |
30 | 42 | # }
|
| 43 | +# } |
| 44 | +# } |
31 | 45 | #
|
32 | 46 | # @example
|
33 | 47 | # stdlib::manage::create_resources:
|
34 | 48 | # file:
|
35 | 49 | # '/etc/motd.d/hello':
|
36 | 50 | # content: I say Hi
|
37 | 51 | # 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' |
38 | 61 | # package:
|
39 | 62 | # example:
|
40 | 63 | # ensure: installed
|
|
46 | 69 | ) {
|
47 | 70 | $create_resources.each |$type, $resources| {
|
48 | 71 | $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 | + } |
50 | 116 | }
|
51 | 117 | }
|
52 | 118 | }
|
0 commit comments