Class: Hiera::Backend::Yaml_backend

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

Instance Method Summary collapse

Constructor Details

#initializeYaml_backend

Returns a new instance of Yaml_backend.



4
5
6
7
8
# File 'lib/hiera/backend/yaml_backend.rb', line 4

def initialize
    require 'yaml'

    Hiera.debug("Hiera YAML backend starting")
end

Instance Method Details

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



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

def lookup(key, scope, order_override, resolution_type)
    answer = Backend.empty_answer(resolution_type)

    Hiera.debug("Looking up #{key} in YAML backend")

    Backend.datasources(scope, order_override) do |source|
        Hiera.debug("Looking for data source #{source}")

        yamlfile = Backend.datafile(:yaml, scope, source, "yaml") || next

        data = YAML.load_file(yamlfile)

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

        # 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)
        case resolution_type
        when :array
            raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
            answer << new_answer
        when :hash
            raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
            answer = new_answer.merge answer
        else
            answer = new_answer
            break
        end
    end

    return answer
end