Class: Hiera::Interpolate

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/interpolate.rb

Constant Summary collapse

INTERPOLATION =
/%\{([^\}]*)\}/
METHOD_INTERPOLATION =
/%\{(scope|hiera|literal|alias)\(['"]([^"']*)["']\)\}/

Class Method Summary collapse

Class Method Details

.alias_interpolate(data, key, scope, extra_data, context) ⇒ Object



93
94
95
# File 'lib/hiera/interpolate.rb', line 93

def alias_interpolate(data, key, scope, extra_data, context)
  Hiera::Backend.lookup(key, nil, scope, context[:order_override], :priority, context)
end

.do_interpolation(data, scope, extra_data, context) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hiera/interpolate.rb', line 44

def do_interpolation(data, scope, extra_data, context)
  if data.is_a?(String) && (match = data.match(INTERPOLATION))
    interpolation_variable = match[1]
    context[:recurse_guard].check(interpolation_variable) do
      interpolate_method, key = get_interpolation_method_and_key(data)
      interpolated_data = send(interpolate_method, data, key, scope, extra_data, context)

      # Halt recursion if we encounter a literal.
      return interpolated_data if interpolate_method == :literal_interpolate

      do_interpolation(interpolated_data, scope, extra_data, context)
    end
  else
    data
  end
end

.get_interpolation_method_and_key(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hiera/interpolate.rb', line 62

def get_interpolation_method_and_key(data)
  if (match = data.match(METHOD_INTERPOLATION))
    case match[1]
    when 'hiera' then [:hiera_interpolate, match[2]]
    when 'scope' then [:scope_interpolate, match[2]]
    when 'literal' then [:literal_interpolate, match[2]]
    when 'alias' then [:alias_interpolate, match[2]]
    end
  elsif (match = data.match(INTERPOLATION))
    [:scope_interpolate, match[1]]
  end
end

.hiera_interpolate(data, key, scope, extra_data, context) ⇒ Object



83
84
85
# File 'lib/hiera/interpolate.rb', line 83

def hiera_interpolate(data, key, scope, extra_data, context)
  Hiera::Backend.lookup(key, nil, scope, context[:order_override], :priority, context)
end

.interpolate(data, scope, extra_data, context) ⇒ Object



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
# File 'lib/hiera/interpolate.rb', line 12

def interpolate(data, scope, extra_data, context)
  if data.is_a?(String)
    # Wrapping do_interpolation in a gsub block ensures we process
    # each interpolation site in isolation using separate recursion guards.
    context ||= {}
    new_context = context.clone
    new_context[:recurse_guard] ||= Hiera::RecursiveGuard.new
    data.gsub(INTERPOLATION) do |match|
      interp_val = do_interpolation(match, scope, extra_data, new_context)

      # Get interp method in case we are aliasing
      if data.is_a?(String) && (match = data.match(INTERPOLATION))
        interpolate_method, key = get_interpolation_method_and_key(data)
      else
        interpolate_method = nil
      end

      if ( (interpolate_method == :alias_interpolate) and (!interp_val.is_a?(String)) )
        if data.match("^#{INTERPOLATION}$")
          return interp_val
        else
          raise Hiera::InterpolationInvalidValue, "Cannot call alias in the string context"
        end
      else
        interp_val
      end
    end
  else
    data
  end
end

.literal_interpolate(data, key, scope, extra_data, context) ⇒ Object



88
89
90
# File 'lib/hiera/interpolate.rb', line 88

def literal_interpolate(data, key, scope, extra_data, context)
  key
end

.scope_interpolate(data, key, scope, extra_data, context) ⇒ Object



76
77
78
79
80
# File 'lib/hiera/interpolate.rb', line 76

def scope_interpolate(data, key, scope, extra_data, context)
  segments = key.split('.')
  catch(:no_such_key) { return Hiera::Backend.qualified_lookup(segments, scope) }
  catch(:no_such_key) { Hiera::Backend.qualified_lookup(segments, extra_data) }
end