Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / getvar.rb
1 #
2 # getvar.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:getvar, :type => :rvalue, :doc => <<-'DOC') do |args|
6     Lookup a variable in a given namespace.
7     Returns undef if variable does not exist.
8
9     For example:
10
11         $foo = getvar('site::data::foo')
12         # Equivalent to $foo = $site::data::foo
13
14     This is useful if the namespace itself is stored in a string:
15
16         $datalocation = 'site::data'
17         $bar = getvar("${datalocation}::bar")
18         # Equivalent to $bar = $site::data::bar
19
20     Note: from Puppet 6.0.0, the compatible function with the same name in Puppet core
21     will be used instead of this function. The new function also has support for
22     digging into a structured value. See the built-in
23     [`getvar`](https://puppet.com/docs/puppet/latest/function.html#getvar) function
24     DOC
25
26     unless args.length == 1
27       raise Puppet::ParseError, "getvar(): wrong number of arguments (#{args.length}; must be 1)"
28     end
29
30     begin
31       result = nil
32       catch(:undefined_variable) do
33         result = lookupvar((args[0]).to_s)
34       end
35
36       # avoid relying on inconsistent behaviour around ruby return values from catch
37       result
38     rescue Puppet::ParseError # rubocop:disable Lint/HandleExceptions : Eat the exception if strict_variables = true is set
39     end
40   end
41 end