Skip to content

Commit 4e68d1a

Browse files
committed
Add stdlib::manage to utilize the stdlib::str2resource function
1 parent 0830311 commit 4e68d1a

File tree

5 files changed

+128
-5
lines changed

5 files changed

+128
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ If you are authoring a module that depends on stdlib, be sure to [specify depend
4040

4141
Most of stdlib's features are automatically loaded by Puppet. To use standardized run stages in Puppet, declare this class in your manifest with `include stdlib`.
4242

43-
When declared, stdlib declares all other classes in the module. The only other class currently included in the module is `stdlib::stages`.
43+
When declared, stdlib declares all other classes in the module. This currently consists of `stdlib::manage` and `stdlib::stages`.
4444

4545
The `stdlib::stages` class declares various run stages for deploying infrastructure, language runtimes, and application layers. The high level stages are (in order):
4646

@@ -62,6 +62,8 @@ node default {
6262
}
6363
```
6464

65+
The `stdlib::manage` class provides an interface for generating trivial resource declarations via the `create_resources` parameter.
66+
6567
## Reference
6668

6769
For information on the classes and types, see the [REFERENCE.md](https://github.com/puppetlabs/puppetlabs-stdlib/blob/main/REFERENCE.md).

lib/puppet/functions/stdlib/str2resource.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# @return Puppet::Resource
2020
dispatch :str2resource do
2121
param 'String', :res_string
22-
#return_type 'Puppet::Resource'
22+
# return_type 'Puppet::Resource'
2323
return_type 'Any'
2424
end
2525

@@ -29,9 +29,9 @@ def str2resource(res_string)
2929
resource = closure_scope.findresource(type_name, title)
3030

3131
if resource.nil?
32-
raise(Puppet::ParseError, "stdlib::str2resource(): could not find #{type_name}[#{title}], this is parse order dependant and values should not be quoted")
32+
raise(Puppet::ParseError, "stdlib::str2resource(): could not find #{type_name}[#{title}], this is parse order dependent and values should not be quoted")
3333
end
3434

35-
return resource
35+
resource
3636
end
3737
end

manifests/init.pp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
# declared in order to use the standardized run stages.
66
#
77
# Declares all other classes in the stdlib module. Currently, this consists
8-
# of stdlib::stages.
8+
# of stdlib::stages and stdlib::manage.
99
#
1010
class stdlib {
11+
include stdlib::manage
1112
include stdlib::stages
1213
}

manifests/manage.pp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

spec/classes/manage_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'stdlib::manage' do
6+
on_supported_os.each do |os, os_facts|
7+
context "on #{os}" do
8+
let(:facts) { os_facts }
9+
10+
it { is_expected.to compile }
11+
end
12+
end
13+
14+
describe 'with resources to create' do
15+
let :pre_condition do
16+
<<-PRECOND
17+
file { '/etc/motd.d' : }
18+
service { 'sshd' : }
19+
PRECOND
20+
end
21+
let :params do
22+
{
23+
'create_resources' => {
24+
'file' => {
25+
'/etc/motd.d/hello' => {
26+
'content' => 'I say Hi',
27+
'notify' => 'Service[sshd]',
28+
}
29+
},
30+
'package' => {
31+
'example' => {
32+
'ensure' => 'installed',
33+
}
34+
}
35+
}
36+
}
37+
end
38+
39+
it { is_expected.to compile }
40+
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+
end
43+
end

0 commit comments

Comments
 (0)