Puppet Function: servicenow_cmdb_integration::getvar
- Defined in:
-
lib/puppet/functions/servicenow_cmdb_integration/getvar.rb
- Function type:
- Ruby 4.x API
Overview
servicenow_cmdb_integration::getvar(Struct[{var=>String[1],topscope_vars_only=>Optional[Boolean]}] $options, Puppet::LookupContext $context) ⇒ Any
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/puppet/functions/servicenow_cmdb_integration/getvar.rb', line 1
Puppet::Functions.create_function(:'servicenow_cmdb_integration::getvar') do
dispatch :getvar do
param 'Struct[{var=>String[1],topscope_vars_only=>Optional[Boolean]}]', :options
param 'Puppet::LookupContext', :context
end
def getvar(options, context)
var = options['var']
topscope_vars_only = true
if options.key?('topscope_vars_only')
topscope_vars_only = options['topscope_vars_only']
end
unless (matches = var.match(%r{^((::)?(\w+::)*\w+)(.*)\z}))
raise ArgumentError, "the var '#{var}' does not start with a valid variable name"
end
rem_sequence = matches[4]
if rem_sequence[0] == '.'
rem_sequence = rem_sequence[1..-1]
else
unless rem_sequence.empty?
raise ArgumentError, "first character after var name in var must be a '.' - got #{rem_sequence[0]}"
end
end
var_name = matches[1]
if topscope_vars_only && var_name.include?('::')
msg = <<-MESSAGE
attempting to consult non-topscope variable "#{var_name}". This is
prohibited by the topscope_vars_only=true option, which dictates that variables
consulted must be in topscope. If you would like to consult "#{var_name}" anyway, you
must set topscope_vars_only=false under the options key in this backend's Hiera
config
MESSAGE
raise ArgumentError, msg
end
catch(:undefined_variable) do
var_value = closure_scope.lookupvar(var_name)
final_value = call_function('get', var_value, rem_sequence, {})
unless final_value.is_a?(Hash)
msg = "[servicenow_cmdb_integration::getvar] '#{var}'' resolves to a non-Hash value so returning an empty Hash instead"
context.explain { msg }
final_value = {}
end
return final_value
end
{}
end
end
|