Class: Hiera::Backend::Redis_backend

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

Constant Summary collapse

VERSION =
'2.0.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRedis_backend

Returns a new instance of Redis_backend.



8
9
10
11
12
13
14
15
16
# File 'lib/hiera/backend/redis_backend.rb', line 8

def initialize
  require 'redis'
  Hiera.debug("Hiera Redis backend #{VERSION} starting")
  @options = { separator: ':', soft_connection_failure: false }.merge(Config[:redis] || {})
  case options[:deserialize]
  when :json then require 'json'
  when :yaml then require 'yaml'
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/hiera/backend/redis_backend.rb', line 6

def options
  @options
end

Instance Method Details

#deserialize(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hiera/backend/redis_backend.rb', line 18

def deserialize(value)
  return value unless value.is_a?(String)

  case options[:deserialize]
  when :json then JSON.parse(value)
  when :yaml then YAML.load(value)
  else
    Hiera.warn("Invalid configuration for :deserialize; found #{options[:deserialize]}")
    value
  end
rescue => e
  Hiera.warn("Error de-serializing data: #{e.class}: #{e.message}")
  value
end

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



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

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

  Backend.datasources(scope, order_override) do |source|
    redis_key = (source.split('/') << key).join(options[:separator])
    data = read_value(redis_key)
    next if data.nil?

    found = true
    new_answer = Backend.parse_answer(data, scope, {}, context)

    case resolution_type.is_a?(Hash) ? :hash : resolution_type
    when :array
      check_type(key, new_answer, Array, String)
      answer ||= []
      answer << new_answer
    when :hash
      check_type(key, new_answer, Hash)
      answer ||= {}
      answer = Backend.merge_answer(new_answer, answer, resolution_type)
    else
      answer = new_answer
      break
    end
  end

  throw :no_such_key unless found
  answer
rescue Redis::CannotConnectError, Errno::ENOENT => e
  Hiera.warn("Cannot connect to redis server at #{redis.id}")
  raise e unless options[:soft_connection_failure]
end