|
| 1 | +# @summary A simple place to define trivial resources |
| 2 | +# |
| 3 | +# Sometimes your systems require a single simple resource. |
| 4 | +# It can feel unnecessary to create a module for a single |
| 5 | +# resource. There are a number of possible patterns to |
| 6 | +# generate trivial resource definitions. This is an attempt |
| 7 | +# to create a single clear method for uncomplicated resources. |
| 8 | +# There is limited support for `before`, `require`, `notify`, |
| 9 | +# and `subscribe`. However, the target resources must be defined |
| 10 | +# before this module is run. |
| 11 | +# |
| 12 | +# @param create_resources |
| 13 | +# A hash of resources to create |
| 14 | +# NOTE: functions, such as `template` or `epp` are not evaluated. |
| 15 | +# |
| 16 | +# @example |
| 17 | +# class { 'stdlib::manage': |
| 18 | +# 'create_resources' => { |
| 19 | +# 'file' => { |
| 20 | +# '/etc/motd.d/hello' => { |
| 21 | +# 'content' => 'I say Hi', |
| 22 | +# 'notify' => 'Service[sshd]', |
| 23 | +# } |
| 24 | +# }, |
| 25 | +# 'package' => { |
| 26 | +# 'example' => { |
| 27 | +# 'ensure' => 'installed', |
| 28 | +# } |
| 29 | +# } |
| 30 | +# } |
| 31 | +# |
| 32 | +# @example |
| 33 | +# stdlib::manage::create_resources: |
| 34 | +# file: |
| 35 | +# '/etc/motd.d/hello': |
| 36 | +# content: I say Hi |
| 37 | +# notify: 'Service[sshd]' |
| 38 | +# package: |
| 39 | +# example: |
| 40 | +# ensure: installed |
| 41 | +class stdlib::manage ( |
| 42 | + Hash[String, Hash] $create_resources = {} |
| 43 | +) { |
| 44 | + $create_resources.each |$type, $resources| { |
| 45 | + $resources.each |$title, $attributes| { |
| 46 | + $filtered_attributes = $attributes.filter |$key, $value| { |
| 47 | + $key !~ /(before|require|notify|subscribe)/ |
| 48 | + } |
| 49 | + |
| 50 | + if $attributes['before'] { |
| 51 | + $_before = stdlib::str2resource($attributes['before']) |
| 52 | + } else { |
| 53 | + $_before = undef |
| 54 | + } |
| 55 | + |
| 56 | + if $attributes['require'] { |
| 57 | + $_require = stdlib::str2resource($attributes['require']) |
| 58 | + } else { |
| 59 | + $_require = undef |
| 60 | + } |
| 61 | + |
| 62 | + if $attributes['notify'] { |
| 63 | + $_notify = stdlib::str2resource($attributes['notify']) |
| 64 | + } else { |
| 65 | + $_notify = undef |
| 66 | + } |
| 67 | + |
| 68 | + if $attributes['subscribe'] { |
| 69 | + $_subscribe = stdlib::str2resource($attributes['subscribe']) |
| 70 | + } else { |
| 71 | + $_subscribe = undef |
| 72 | + } |
| 73 | + |
| 74 | + create_resources($type, { $title => $filtered_attributes }, { 'before' => $_before, 'require' => $_require, 'notify' => $_notify, 'subscribe' => $_subscribe }) |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments