File tree Expand file tree Collapse file tree 3 files changed +86
-0
lines changed
lib/puppet/parser/functions Expand file tree Collapse file tree 3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,25 @@ Example:
209
209
210
210
- * Type* : rvalue
211
211
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
+
212
231
getvar
213
232
------
214
233
Lookup a variable in a remote namespace.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments