1 # Test whether a given class or definition is defined
2 require 'puppet/parser/functions'
4 Puppet::Parser::Functions.newfunction(:getparam,
7 Takes a resource reference and name of the parameter and
8 returns value of resource's parameter. Note that user defined
9 resource types are evaluated lazily.
13 # define a resource type with a parameter
14 define example_resource($param) {
17 # declare an instance of that type
18 example_resource { "example_resource_instance":
19 param => "'the value we are getting in this example''"
22 # Because of order of evaluation, a second definition is needed
23 # that will be evaluated after the first resource has been declared
25 define example_get_param {
26 # This will notice the value of the parameter
27 notice(getparam(Example_resource["example_resource_instance"], "param"))
30 # Declare an instance of the second resource type - this will call notice
31 example_get_param { 'show_notify': }
33 Would notice: 'the value we are getting in this example'
35 Note that since Puppet 4.0.0 it is possible to get a parameter value by using its data type
36 and the [ ] operator. The example below is equivalent to a call to getparam():
38 Example_resource['example_resource_instance']['param']
42 reference, param = vals
43 raise(ArgumentError, 'Must specify a reference') unless reference
44 raise(ArgumentError, 'Must specify name of a parameter') unless param && param.instance_of?(String)
46 return '' if param.empty?
48 resource = findresource(reference.to_s)
50 return resource[param] unless resource[param].nil?