Class: Hiera::Backend::Eyaml_backend

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil) ⇒ Eyaml_backend

Returns a new instance of Eyaml_backend.



14
15
16
17
18
19
20
# File 'lib/hiera/backend/eyaml_backend.rb', line 14

def initialize(cache = nil)
  debug('Hiera eYAML backend starting')

  @decrypted_cache = {}
  @cache     = cache || Filecache.new
  @extension = Config[:eyaml][:extension] || 'eyaml'
end

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



12
13
14
# File 'lib/hiera/backend/eyaml_backend.rb', line 12

def extension
  @extension
end

Instance Method Details

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



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hiera/backend/eyaml_backend.rb', line 22

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

  parse_options(scope)

  debug("Looking up #{key} in eYAML backend")

  Backend.datasources(scope, order_override) do |source|
    debug("Looking for data source #{source}")
    eyaml_file = Backend.datafile(:eyaml, scope, source, extension) || next

    next unless File.exist?(eyaml_file)

    data = @cache.read(eyaml_file, Hash) do |data|
      YAML.load(data) || {}
    end

    next if data.empty?
    next unless data.include?(key)

    # 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.
    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 = parse_answer(data[key], scope)
    case resolution_type
    when :array
      unless new_answer.is_a? Array or new_answer.is_a? String
        raise Exception,
              "Hiera type mismatch: expected Array and got #{new_answer.class}"
      end

      answer ||= []
      answer << new_answer
    when :hash
      unless new_answer.is_a? Hash
        raise Exception,
              "Hiera type mismatch: expected Hash and got #{new_answer.class}"
      end

      answer ||= {}
      answer = Backend.merge_answer(new_answer, answer)
    else
      answer = new_answer
      break
    end
  end

  answer
end