Skip to content

Commit 3118e64

Browse files
author
Jeff McCune
committed
Merge branch 'offlinehacker-feature/master/getparam_function'
* offlinehacker-feature/master/getparam_function: Add getparam function to get defined resource parameters
2 parents d674190 + 20e0e07 commit 3118e64

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ Example:
209209

210210
- *Type*: rvalue
211211

212+
getparam
213+
--------
214+
215+
Takes a resource reference and name of the parameter and returns
216+
value of resource's parameter.
217+
218+
For example:
219+
220+
define example_resource($param) {
221+
}
222+
223+
example_resource { "example_resource_instance":
224+
param => "param_value"
225+
}
226+
227+
getparam(Example_resource["example_resource_instance"], "param")
228+
229+
- *Type*: rvalue
230+
212231
getvar
213232
------
214233
Lookup a variable in a remote namespace.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Test whether a given class or definition is defined
2+
require 'puppet/parser/functions'
3+
4+
Puppet::Parser::Functions.newfunction(:getparam,
5+
:type => :rvalue,
6+
:doc => <<-'ENDOFDOC'
7+
Takes a resource reference and name of the parameter and
8+
returns value of resource's parameter.
9+
10+
*Examples:*
11+
12+
define example_resource($param) {
13+
}
14+
15+
example_resource { "example_resource_instance":
16+
param => "param_value"
17+
}
18+
19+
getparam(Example_resource["example_resource_instance"], "param")
20+
21+
Would return: param_value
22+
ENDOFDOC
23+
) do |vals|
24+
reference, param = vals
25+
raise(ArgumentError, 'Must specify a reference') unless reference
26+
raise(ArgumentError, 'Must specify name of a parameter') unless param and param.instance_of? String
27+
28+
if resource = findresource(reference.to_s)
29+
return resource[param] if resource[param]
30+
end
31+
32+
return ''
33+
end

spec/functions/getparam_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#! /usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
require 'rspec-puppet'
5+
describe 'getparam' do
6+
describe 'when a resource is not specified' do
7+
it do
8+
should run.with_params().and_raise_error(ArgumentError)
9+
should run.with_params('User[dan]').and_raise_error(ArgumentError)
10+
should run.with_params('User[dan]', {}).and_raise_error(ArgumentError)
11+
should run.with_params('User[dan]', '').and_return('')
12+
end
13+
end
14+
describe 'when compared against a resource with no params' do
15+
let :pre_condition do
16+
'user { "dan": }'
17+
end
18+
it do
19+
should run.with_params('User[dan]', 'shell').and_return('')
20+
end
21+
end
22+
23+
describe 'when compared against a resource with params' do
24+
let :pre_condition do
25+
'user { "dan": ensure => present, shell => "/bin/sh", managehome => false}'
26+
end
27+
it do
28+
should run.with_params('User[dan]', 'shell').and_return('/bin/sh')
29+
should run.with_params('User[dan]', '').and_return('')
30+
should run.with_params('User[dan]', 'ensure').and_return('present')
31+
should run.with_params('User[dan]', 'managehome').and_return(false)
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)