Update stdlib and concat to 6.1.0 both
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / getparam.rb
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 => <<-'DOC'
7     @summary
8       Returns the value of a resource's parameter.
9
10     @return
11       value of a resource's parameter.
12
13     Takes a resource reference and name of the parameter and
14     returns value of resource's parameter. Note that user defined
15     resource types are evaluated lazily.
16
17     @example Example Usage:
18
19       # define a resource type with a parameter
20       define example_resource($param) {
21       }
22
23       # declare an instance of that type
24       example_resource { "example_resource_instance":
25           param => "'the value we are getting in this example''"
26       }
27
28       # Because of order of evaluation, a second definition is needed
29       # that will be evaluated after the first resource has been declared
30       #
31       define example_get_param {
32         # This will notice the value of the parameter
33         notice(getparam(Example_resource["example_resource_instance"], "param"))
34       }
35
36       # Declare an instance of the second resource type - this will call notice
37       example_get_param { 'show_notify': }
38
39     Would notice: 'the value we are getting in this example'
40
41     > **Note** that since Puppet 4.0.0 it is possible to get a parameter value by using its data type
42     and the [ ] operator. The example below is equivalent to a call to getparam():
43       ```Example_resource['example_resource_instance']['param']``
44
45   DOC
46                                      ) do |vals|
47   reference, param = vals
48   raise(ArgumentError, 'Must specify a reference') unless reference
49   raise(ArgumentError, 'Must specify name of a parameter') unless param && param.instance_of?(String)
50
51   return '' if param.empty?
52
53   resource = findresource(reference.to_s)
54   if resource
55     return resource[param] unless resource[param].nil?
56   end
57
58   return ''
59 end