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
22
23
24
25
26
# 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'
    PuppetDB::Connection.check_version
    uri = URI(Puppet::Util::Puppetdb.config.server_urls.first)
    host = uri.host
    port = uri.port
    ssl = uri.scheme == 'https'
  rescue
    host = 'puppetdb'
    port = 443
    ssl = true
  end

  Hiera.debug('Hiera PuppetDB backend starting')

  @puppetdb = PuppetDB::Connection.new(host, port, ssl)
  @parser = PuppetDB::Parser.new
end

Instance Method Details

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hiera/backend/puppetdb_backend.rb', line 28

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
      query = @parser.facts_query query, [fact]
      @puppetdb.query(:facts, query).collect { |f| f['value'] }.sort
    else
      query = @parser.parse query, :nodes if query.is_a? String
      @puppetdb.query(:nodes, query).collect { |n| n['name'] }
    end
  end
end