Skip to content

Commit 1e9db53

Browse files
author
Jeff McCune
committed
Merge branch 'bodepd-ensure_resource_attempt_2'
* bodepd-ensure_resource_attempt_2: Explicitly load functions used by ensure_resource Revert "Revert "Merge pull request #86 from bodepd/ensure_resource""
2 parents 1e09833 + 5d99cdf commit 1e9db53

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Test whether a given class or definition is defined
2+
require 'puppet/parser/functions'
3+
4+
Puppet::Parser::Functions.newfunction(:defined_with_params,
5+
:type => :rvalue,
6+
:doc => <<-'ENDOFDOC'
7+
Takes a resource reference and an optional hash of attributes.
8+
9+
Returns true if a resource with the specified attributes has already been added
10+
to the catalog, and false otherwise.
11+
12+
user { 'dan':
13+
ensure => present,
14+
}
15+
16+
if ! defined_with_params(User[dan], {'ensure' => 'present' }) {
17+
user { 'dan': ensure => present, }
18+
}
19+
ENDOFDOC
20+
) do |vals|
21+
reference, params = vals
22+
raise(ArgumentError, 'Must specify a reference') unless reference
23+
if (! params) || params == ''
24+
params = {}
25+
end
26+
ret = false
27+
if resource = findresource(reference.to_s)
28+
matches = params.collect do |key, value|
29+
resource[key] == value
30+
end
31+
ret = params.empty? || !matches.include?(false)
32+
end
33+
Puppet.debug("Resource #{reference} was not determined to be defined")
34+
ret
35+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Test whether a given class or definition is defined
2+
require 'puppet/parser/functions'
3+
4+
Puppet::Parser::Functions.newfunction(:ensure_resource,
5+
:type => :statement,
6+
:doc => <<-'ENDOFDOC'
7+
Takes a resource type, title, and a list of attributes that describe a
8+
resource.
9+
10+
user { 'dan':
11+
ensure => present,
12+
}
13+
14+
This example only creates the resource if it does not already exist:
15+
16+
ensure_resource('user, 'dan', {'ensure' => 'present' })
17+
18+
If the resource already exists but does not match the specified parameters,
19+
this function will attempt to recreate the resource leading to a duplicate
20+
resource definition error.
21+
22+
ENDOFDOC
23+
) do |vals|
24+
type, title, params = vals
25+
raise(ArgumentError, 'Must specify a type') unless type
26+
raise(ArgumentError, 'Must specify a title') unless title
27+
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 }])
34+
end
35+
end
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
require 'rspec-puppet'
5+
describe 'defined_with_params' do
6+
describe 'when a resource is not specified' do
7+
it { should run.with_params().and_raise_error(ArgumentError) }
8+
end
9+
describe 'when compared against a resource with no attributes' do
10+
let :pre_condition do
11+
'user { "dan": }'
12+
end
13+
it do
14+
should run.with_params('User[dan]', {}).and_return(true)
15+
should run.with_params('User[bob]', {}).and_return(false)
16+
should run.with_params('User[dan]', {'foo' => 'bar'}).and_return(false)
17+
end
18+
end
19+
20+
describe 'when compared against a resource with attributes' do
21+
let :pre_condition do
22+
'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}'
23+
end
24+
it do
25+
should run.with_params('User[dan]', {}).and_return(true)
26+
should run.with_params('User[dan]', '').and_return(true)
27+
should run.with_params('User[dan]', {'ensure' => 'present'}
28+
).and_return(true)
29+
should run.with_params('User[dan]',
30+
{'ensure' => 'present', 'managehome' => false}
31+
).and_return(true)
32+
should run.with_params('User[dan]',
33+
{'ensure' => 'absent', 'managehome' => false}
34+
).and_return(false)
35+
end
36+
end
37+
end
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
require 'rspec-puppet'
5+
describe 'ensure_resource' do
6+
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
11+
end
12+
describe 'when compared against a resource with no attributes' do
13+
let :pre_condition do
14+
'user { "dan": }'
15+
end
16+
it do
17+
should run.with_params('user', 'dan', {})
18+
compiler.catalog.resource('User[dan]').to_s.should == 'User[dan]'
19+
end
20+
end
21+
22+
describe 'when compared against a resource with attributes' do
23+
let :pre_condition do
24+
'user { "dan": ensure => present, shell => "/bin/csh", managehome => false}'
25+
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)
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)