Class: Hiera::Backend::Yeaml_backend

Inherits:
Yaml_backend
  • Object
show all
Defined in:
lib/hiera/backend/yeaml_backend.rb

Instance Method Summary collapse

Instance Method Details

#lookup(key, scope, order_override, resolution_type, context) ⇒ Object



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
# File 'lib/hiera/backend/yeaml_backend.rb', line 6

def lookup(key, scope, order_override, resolution_type, context)
  answer = nil
  found = false

  Hiera.debug("Looking up #{key} in YEAML backend")
  Backend.datasourcefiles(:yeaml, scope, Config[:yeaml][:extension] || "eyaml", order_override) do |source, yamlfile|
#          debugger if scope['env']== "dev-on-premises" && ['cluster', 'env','lookup_options'].include?(key)
    data = @cache.read_file(yamlfile, Hash) do |data|
      YAML.load(data) || {}
    end

    next if data.empty?
    next unless data.include?(key)
    found = true

    # Extra logging that we found the key. This can be outputted
    # multiple times if the resolution type is array or hash but that
    # should be expected as the logging will then tell the user ALL the
    # places where the key is found.
    Hiera.debug("Found #{key} in #{source}")

    # for array resolution we just append to the array whatever
    # we find, we then goes onto the next file and keep adding to
    # the array
    #
    # for priority searches we break after the first found data item
    new_answer = Backend.parse_answer(data[key], scope, {}, context)
    case resolution_type.is_a?(Hash) ? :hash : resolution_type
    when :array
      raise Exception, "Hiera type mismatch for key '#{key}': expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
      answer ||= []
      answer << new_answer
    when :hash
      raise Exception, "Hiera type mismatch for key '#{key}': expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
      answer ||= {}
      answer = Backend.merge_answer(new_answer, answer, resolution_type)
    else
      answer = new_answer
      break
    end
  end
  throw :no_such_key unless found
  return answer
end