Module: Puppet::DataProviders::HieraInterpolate

Included in:
HieraConfig, JsonDataProvider, YamlDataProvider
Defined in:
lib/puppet/data_providers/hiera_interpolate.rb

Overview

Add support for Hiera-like interpolation expressions. The expressions may contain keys that uses dot-notation to further navigate into hashes and arrays

Instance Method Summary collapse

Instance Method Details

#interpolate(subject, lookup_invocation, allow_methods) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/puppet/data_providers/hiera_interpolate.rb', line 7

def interpolate(subject, lookup_invocation, allow_methods)
  return subject unless subject.is_a?(String) && !subject.index('%{').nil?

  lookup_invocation.with(:interpolate, subject) do
    subject.gsub(/%\{([^\}]+)\}/) do |match|
      method_key, key = get_method_and_data($1, allow_methods)
      is_alias = method_key == 'alias'

      # Alias is only permitted if the entire string is equal to the interpolate expression
      raise Puppet::DataBinding::LookupError, "'alias' interpolation is only permitted if the expression is equal to the entire string" if is_alias && subject != match

      segments = key.split('.')
      value = interpolate_method(method_key).call(segments[0], lookup_invocation)
      value = qualified_lookup(segments.drop(1), value) if segments.size > 1
      value = lookup_invocation.check(key) { interpolate(value, lookup_invocation, allow_methods) } if value.is_a?(String)

      # break gsub and return value immediately if this was an alias substitution. The value might be something other than a String
      return value if is_alias

      value || ''
    end
  end
end