Update puppetlabs/stdlib module
[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     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.
10
11     *Examples:*
12
13         # define a resource type with a parameter
14         define example_resource($param) {
15         }
16
17         # declare an instance of that type
18         example_resource { "example_resource_instance":
19             param => "'the value we are getting in this example''"
20         }
21
22         # Because of order of evaluation, a second definition is needed
23         # that will be evaluated after the first resource has been declared
24         #
25         define example_get_param {
26           # This will notice the value of the parameter
27           notice(getparam(Example_resource["example_resource_instance"], "param"))
28         }
29
30         # Declare an instance of the second resource type - this will call notice
31         example_get_param { 'show_notify': }
32
33     Would notice: 'the value we are getting in this example'
34
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():
37
38         Example_resource['example_resource_instance']['param']
39
40   DOC
41                                      ) do |vals|
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)
45
46   return '' if param.empty?
47
48   resource = findresource(reference.to_s)
49   if resource
50     return resource[param] unless resource[param].nil?
51   end
52
53   return ''
54 end