Class: Hiera::Backend::Jerakiaserver_backend
- Inherits:
-
Object
- Object
- Hiera::Backend::Jerakiaserver_backend
- Defined in:
- lib/hiera/backend/jerakiaserver_backend.rb
Instance Method Summary collapse
-
#initialize ⇒ Jerakiaserver_backend
constructor
A new instance of Jerakiaserver_backend.
- #lookup(key, scope, order_override, resolution_type) ⇒ Object
- #scope_valid?(identifier, metadata) ⇒ Boolean
- #send_scope(identifier, scope) ⇒ Object
- #store_scope(identifier, uuid, scope) ⇒ Object
Constructor Details
#initialize ⇒ Jerakiaserver_backend
Returns a new instance of Jerakiaserver_backend.
9 10 11 12 13 |
# File 'lib/hiera/backend/jerakiaserver_backend.rb', line 9 def initialize() @config = Hiera::Config[:jerakia] || {} @jerakia = ::Jerakia::Client.new(@config) @scope_cache = {} end |
Instance Method Details
#lookup(key, scope, order_override, resolution_type) ⇒ 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/hiera/backend/jerakiaserver_backend.rb', line 33 def lookup(key, scope, order_override, resolution_type) # Jerakia doesn't do anything with lookup_options, this behaviour is achieved # using schemas, therefore we always return nil here for the key return nil if key == 'lookup_options' lookup_type = :first merge_type = :none case resolution_type when :array lookup_type = :cascade merge_type = :array when :hash lookup_type = :cascade merge_type = :hash end namespace = [] if key.include?('::') lookup_key = key.split('::') key = lookup_key.pop namespace = lookup_key.join('/') end ={} if scope.is_a?(Hash) =scope.reject { |k, v| v.is_a?(Puppet::Resource) } else = scope.real.to_hash.reject { |k, v| v.is_a?(Puppet::Resource) } end if ['trusted'] identifier = ['trusted']['certname'] else identifier = ['fqdn'] end send_scope(identifier, ) unless scope_valid?(identifier, ) = { :namespace => namespace, :scope => 'server', :lookup_type => lookup_type.to_s, :merge => merge_type.to_s, :scope_opts => { 'identifier' => identifier, 'realm' => 'puppet' } } begin lookup = @jerakia.lookup(key, ) rescue Jerakia::Client::ScopeNotFoundError => e send_scope(identifier, ) lookup = @jerakia.lookup(key, ) end if lookup.is_a?(Hash) raise Puppet::Error, "Jerakia data lookup failed #{lookup['message']}" unless lookup['status'] = 'ok' payload = lookup['payload'] case resolution_type when :array return [] if payload.nil? return payload when :hash return {} if payload.nil? return payload else return payload end else raise Puppet::Error, "Jerakia data lookup failed Expected a hash but got a #{lookup.class}" end end |
#scope_valid?(identifier, metadata) ⇒ Boolean
27 28 29 30 31 |
# File 'lib/hiera/backend/jerakiaserver_backend.rb', line 27 def scope_valid?(identifier, ) return false unless @scope_cache.include?(identifier) return false unless @scope_cache[identifier][:scope] == return true end |
#send_scope(identifier, scope) ⇒ Object
22 23 24 25 |
# File 'lib/hiera/backend/jerakiaserver_backend.rb', line 22 def send_scope(identifier, scope) returndata = @jerakia.send_scope('puppet', identifier, scope) store_scope(identifier, returndata['uuid'], scope) end |
#store_scope(identifier, uuid, scope) ⇒ Object
15 16 17 18 19 20 |
# File 'lib/hiera/backend/jerakiaserver_backend.rb', line 15 def store_scope(identifier, uuid, scope) @scope_cache[identifier] = { :uuid => uuid, :scope => scope } end |