Sha256: 8099329ac2a34db3be048057e456068b4c47158033e99952d7f97c290dac9db4

Contents?: true

Size: 1.88 KB

Versions: 95

Compression:

Stored size: 1.88 KB

Contents

class Hiera
  module Backend
    class Json_backend
      def initialize(cache=nil)
        require 'json'

        Hiera.debug("Hiera JSON backend starting")

        @cache = cache || Filecache.new
      end

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

        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

          next unless File.exist?(jsonfile)

          data = @cache.read_file(jsonfile, Hash) do |data|
            JSON.parse(data)
          end

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

          # 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
    end
  end
end

Version data entries

95 entries across 95 versions & 2 rubygems

Version Path
hiera-3.12.0 lib/hiera/backend/json_backend.rb
hiera-3.11.0 lib/hiera/backend/json_backend.rb
hiera-3.10.0 lib/hiera/backend/json_backend.rb
hiera-3.9.0 lib/hiera/backend/json_backend.rb
hiera-3.8.0 lib/hiera/backend/json_backend.rb
hiera-3.7.0 lib/hiera/backend/json_backend.rb
hiera-3.6.0 lib/hiera/backend/json_backend.rb
hiera-3.4.6 lib/hiera/backend/json_backend.rb
hiera-3.5.0 lib/hiera/backend/json_backend.rb
bolt-0.24.0 vendored/hiera/lib/hiera/backend/json_backend.rb
bolt-0.23.0 vendored/hiera/lib/hiera/backend/json_backend.rb
hiera-3.4.5 lib/hiera/backend/json_backend.rb
bolt-0.22.0 vendored/hiera/lib/hiera/backend/json_backend.rb
bolt-0.21.8 vendored/hiera/lib/hiera/backend/json_backend.rb
hiera-3.4.4 lib/hiera/backend/json_backend.rb
bolt-0.21.7 vendored/hiera/lib/hiera/backend/json_backend.rb
bolt-0.21.6 vendored/hiera/lib/hiera/backend/json_backend.rb
bolt-0.21.5 vendored/hiera/lib/hiera/backend/json_backend.rb
bolt-0.21.4 vendored/hiera/lib/hiera/backend/json_backend.rb
bolt-0.21.3 vendored/hiera/lib/hiera/backend/json_backend.rb