Skip to content

Commit 3e59bbd

Browse files
committed
Merge pull request #152 from AlexCline/feature/master/allow_arrays_in_ensure_resource
(#20548) Allow an array of resource titles to be passed into the ensure_...
2 parents 0c68ff6 + d38bce0 commit 3e59bbd

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

README.markdown

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ If the resource already exists but does not match the specified parameters,
243243
this function will attempt to recreate the resource leading to a duplicate
244244
resource definition error.
245245

246+
An array of resources can also be passed in and each will be created with
247+
the type and parameters specified if it doesn't already exist.
248+
249+
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
250+
246251

247252

248253
- *Type*: statement

lib/puppet/parser/functions/ensure_resource.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,27 @@
1919
this function will attempt to recreate the resource leading to a duplicate
2020
resource definition error.
2121
22+
An array of resources can also be passed in and each will be created with
23+
the type and parameters specified if it doesn't already exist.
24+
25+
ensure_resource('user', ['dan','alex'], {'ensure' => 'present'})
26+
2227
ENDOFDOC
2328
) do |vals|
2429
type, title, params = vals
2530
raise(ArgumentError, 'Must specify a type') unless type
2631
raise(ArgumentError, 'Must specify a title') unless title
2732
params ||= {}
28-
Puppet::Parser::Functions.function(:defined_with_params)
29-
if function_defined_with_params(["#{type}[#{title}]", params])
30-
Puppet.debug("Resource #{type}[#{title}] not created b/c it already exists")
31-
else
32-
Puppet::Parser::Functions.function(:create_resources)
33-
function_create_resources([type.capitalize, { title => params }])
33+
34+
items = [title].flatten
35+
36+
items.each do |item|
37+
Puppet::Parser::Functions.function(:defined_with_params)
38+
if function_defined_with_params(["#{type}[#{item}]", params])
39+
Puppet.debug("Resource #{type}[#{item}] not created because it already exists")
40+
else
41+
Puppet::Parser::Functions.function(:create_resources)
42+
function_create_resources([type.capitalize, { item => params }])
43+
end
3444
end
3545
end

spec/functions/ensure_resource_spec.rb

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
require 'rspec-puppet'
55
describe 'ensure_resource' do
66
describe 'when a type or title is not specified' do
7-
it do
8-
should run.with_params().and_raise_error(ArgumentError)
9-
should run.with_params(['type']).and_raise_error(ArgumentError)
10-
end
7+
it { should run.with_params().and_raise_error(ArgumentError) }
8+
it { should run.with_params(['type']).and_raise_error(ArgumentError) }
119
end
10+
1211
describe 'when compared against a resource with no attributes' do
1312
let :pre_condition do
1413
'user { "dan": }'
1514
end
16-
it do
17-
should run.with_params('user', 'dan', {})
15+
it "should contain the the ensured resources" do
16+
subject.should run.with_params('user', 'dan', {})
1817
compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]'
1918
end
2019
end
@@ -23,18 +22,43 @@
2322
let :pre_condition do
2423
'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}'
2524
end
26-
it do
27-
# these first three should not fail
28-
should run.with_params('User', 'dan', {})
29-
should run.with_params('User', 'dan', '')
30-
should run.with_params('User', 'dan', {'ensure' => 'present'})
31-
should run.with_params('User', 'dan',
32-
{'ensure' => 'present', 'managehome' => false}
33-
)
34-
# test that this fails
35-
should run.with_params('User', 'dan',
36-
{'ensure' => 'absent', 'managehome' => false}
37-
).and_raise_error(Puppet::Error)
25+
# these first three should not fail
26+
it { should run.with_params('User', 'dan', {}) }
27+
it { should run.with_params('User', 'dan', '') }
28+
it { should run.with_params('User', 'dan', {'ensure' => 'present'}) }
29+
it { should run.with_params('User', 'dan', {'ensure' => 'present', 'managehome' => false}) }
30+
# test that this fails
31+
it { should run.with_params('User', 'dan', {'ensure' => 'absent', 'managehome' => false}).and_raise_error(Puppet::Error) }
32+
end
33+
34+
describe 'when an array of new resources are passed in' do
35+
it "should contain the ensured resources" do
36+
subject.should run.with_params('User', ['dan', 'alex'], {})
37+
compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]'
38+
compiler.catalog.resource('User[alex]').to_s.should == 'User[alex]'
39+
end
40+
end
41+
42+
describe 'when an array of existing resources is compared against existing resources' do
43+
let :pre_condition do
44+
'user { "dan": ensure => present; "alex": ensure => present }'
45+
end
46+
it "should return the existing resources" do
47+
subject.should run.with_params('User', ['dan', 'alex'], {})
48+
compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]'
49+
compiler.catalog.resource('User[alex]').to_s.should == 'User[alex]'
50+
end
51+
end
52+
53+
describe 'when compared against existing resources with attributes' do
54+
let :pre_condition do
55+
'user { "dan": ensure => present; "alex": ensure => present }'
3856
end
57+
# These should not fail
58+
it { should run.with_params('User', ['dan', 'alex'], {}) }
59+
it { should run.with_params('User', ['dan', 'alex'], '') }
60+
it { should run.with_params('User', ['dan', 'alex'], {'ensure' => 'present'}) }
61+
# This should fail
62+
it { should run.with_params('User', ['dan', 'alex'], {'ensure' => 'absent'}).and_raise_error(Puppet::Error) }
3963
end
4064
end

0 commit comments

Comments
 (0)