Class: Hiera::Backend::Json_backend

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

Instance Method Summary collapse

Constructor Details

#initializeJson_backend

Returns a new instance of Json_backend.



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

def initialize
    require 'json'

    Hiera.debug("Hiera JSON 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
# File 'lib/hiera/backend/json_backend.rb', line 10

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

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

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

        jsonfile = Backend.datafile(:json, scope, source, "json") || next

        data = JSON.parse(File.read(jsonfile))

        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
        case resolution_type
        when :array
            answer << Backend.parse_answer(data[key], scope)
        else
            answer = Backend.parse_answer(data[key], scope)
            break
        end
    end

    return answer
end