Skip to content

Commit 7f07bdf

Browse files
authored
Merge pull request #1250 from jcpunk/simplify-manage
Simplify stdlib::manage
2 parents 16c27c8 + 8a1b5b1 commit 7f07bdf

File tree

3 files changed

+24
-35
lines changed

3 files changed

+24
-35
lines changed

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ node default {
6262
}
6363
```
6464

65-
The `stdlib::manage` class provides an interface for generating trivial resource declarations via the `create_resources` parameter.
65+
The `stdlib::manage` class provides an interface for generating trivial resource declarations via the `create_resources` parameter. Depending on your usage, you may want to set `hiera`'s `lookup_options` for the `stdlib::manage::create_resources:` element.
66+
67+
```yaml
68+
---
69+
stdlib::manage::create_resources:
70+
file:
71+
/etc/somefile:
72+
ensure: file
73+
owner: root
74+
group: root
75+
package:
76+
badpackage:
77+
ensure: absent
78+
```
6679
6780
## Reference
6881

manifests/manage.pp

+8-33
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
# resource. There are a number of possible patterns to
66
# generate trivial resource definitions. This is an attempt
77
# 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.
8+
# There is __limited__ support for `before`, `require`, `notify`,
9+
# and `subscribe`.
1110
#
1211
# @param create_resources
1312
# A hash of resources to create
14-
# NOTE: functions, such as `template` or `epp` are not evaluated.
13+
# NOTE: functions, such as `template` or `epp`, are not evaluated.
1514
#
1615
# @example
1716
# class { 'stdlib::manage':
@@ -25,6 +24,7 @@
2524
# 'package' => {
2625
# 'example' => {
2726
# 'ensure' => 'installed',
27+
# 'subscribe' => ['Service[sshd]', 'Exec[something]'],
2828
# }
2929
# }
3030
# }
@@ -38,40 +38,15 @@
3838
# package:
3939
# example:
4040
# ensure: installed
41+
# subscribe:
42+
# - 'Service[sshd]'
43+
# - 'Exec[something]'
4144
class stdlib::manage (
4245
Hash[String, Hash] $create_resources = {}
4346
) {
4447
$create_resources.each |$type, $resources| {
4548
$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 })
49+
create_resources($type, { $title => $attributes })
7550
}
7651
}
7752
}

spec/classes/manage_spec.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'package' => {
3131
'example' => {
3232
'ensure' => 'installed',
33+
'subscribe' => ['Service[sshd]', 'File[/etc/motd.d]'],
3334
}
3435
}
3536
}
@@ -38,6 +39,6 @@
3839

3940
it { is_expected.to compile }
4041
it { is_expected.to contain_file('/etc/motd.d/hello').with_content('I say Hi').with_notify('Service[sshd]') }
41-
it { is_expected.to contain_package('example').with_ensure('installed') }
42+
it { is_expected.to contain_package('example').with_ensure('installed').that_subscribes_to(['Service[sshd]', 'File[/etc/motd.d]']) }
4243
end
4344
end

0 commit comments

Comments
 (0)