Class: Hiera::Backend::Puppetdb_backend

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

Instance Method Summary collapse

Constructor Details

#initializePuppetdb_backend

Returns a new instance of Puppetdb_backend.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hiera/backend/puppetdb_backend.rb', line 4

def initialize
  require 'puppetdb/connection'
  begin
    require 'puppet'
    # This is needed when we run from hiera cli
    Puppet.initialize_settings unless Puppet[:confdir]
    require 'puppet/util/puppetdb'
    server = Puppet::Util::Puppetdb.server
    port = Puppet::Util::Puppetdb.port
  rescue
    server = 'puppetdb'
    port = 443
  end

  Hiera.debug("Hiera PuppetDB backend starting")

  @puppetdb = PuppetDB::Connection.new(server, port)
end

Instance Method Details

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hiera/backend/puppetdb_backend.rb', line 23

def lookup(key, scope, order_override, resolution_type)
  return nil if key.end_with? "::_nodequery"

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

  if nodequery = Backend.lookup(key + "::_nodequery", nil, scope, order_override, :priority)
    Hiera.debug("Found nodequery #{nodequery.inspect}")

    # Support specifying the query in a few different ways
    if nodequery.is_a? Hash
      query = nodequery['query']
      fact = nodequery['fact']
    elsif nodequery.is_a? Array
      query, fact = *nodequery
    else
      query = nodequery.to_s
    end

    if fact then
      query = @puppetdb.parse_query query, :facts if query.is_a? String
      @puppetdb.facts([fact], query).each_value.collect { |facts| facts[fact] }
    else
      query = @puppetdb.parse_query query, :nodes if query.is_a? String
      @puppetdb.query(:nodes, query).collect { |n| n['name'] }
    end
  end
end