Class: Hiera::Backend::Cfn_metadata_backend

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

Instance Method Summary collapse

Constructor Details

#initializeCfn_metadata_backend

Returns a new instance of Cfn_metadata_backend.



6
7
8
9
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
# File 'lib/hiera/backend/cfn_metadata_backend.rb', line 6

def initialize()
  require 'aws-sdk-core'
  require 'hiera-cfn-metadata'

  @config = Config[:cfn_metadata]
  stack = @config[:stack] || ENV['CFN_STACK']
  resource = @config[:resource] || ENV['CFN_RESOURCE']

  begin
    cfn = Aws::CloudFormation::Client.new(
      region: @config[:region] || ENV['AWS_REGION'],
      credentials: Aws::InstanceIdentityCredentials.new(),
    )

    resp = cfn.describe_stack_resource({
      stack_name: stack,
      logical_resource_id: resource
    })
    raise resp.error if !resp.successful?

     = resp.stack_resource_detail.

    @datasources = JSON.parse()
    p @datasources

  rescue Exception => e
    @datasources = nil
    Hiera.warn("[hiera-cfn-metadata] Skipping backend. Configuration error: #{e}")
  end
end

Instance Method Details

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



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

def lookup(key, scope, order_override, resolution_type)
  answer = nil
  return answer if @datasources.nil?

  Hiera.debug("[hiera-cfn-metadata] Looking up #{key}")

  Backend.datasources(scope, order_override) do |source|
    Hiera.debug("[hiera-cfn-metadata] Looking for data source #{source}")

    data = @datasources[source]
    next if data.nil?
    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-cfn-metadata] 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-cfn-metadata] 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)
    else
      answer = new_answer
      break
    end
  end

  return answer
end