Class: PuppetDB::Connection

Inherits:
Object
  • Object
show all
Includes:
Puppet::Util::Logging
Defined in:
lib/puppetdb/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'puppetdb', port = 443, use_ssl = true) ⇒ Connection

Returns a new instance of Connection.



12
13
14
15
16
# File 'lib/puppetdb/connection.rb', line 12

def initialize(host = 'puppetdb', port = 443, use_ssl = true)
  @host = host
  @port = port
  @use_ssl = use_ssl
end

Class Method Details

.check_versionObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppetdb/connection.rb', line 18

def self.check_version
  begin
    require 'puppet/util/puppetdb'
    unless Puppet::Util::Puppetdb.config.respond_to?('server_urls')
      Puppet.warning "It looks like you are using a PuppetDB version < 3.0.\nThis version of puppetdbquery requires at least PuppetDB 3.0 to work.\nDowngrade to puppetdbquery 1.x to use it with PuppetDB 2.x.\n"
    end
  rescue LoadError
  end
end

Instance Method Details

#query(endpoint, query = nil, http = nil, version = :v4) ⇒ Array

Execute a PuppetDB query

Parameters:

  • endpoint (Symbol)

    :resources, :facts or :nodes

  • query (Array) (defaults to: nil)

    query to execute

Returns:

  • (Array)

    the results of the query



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppetdb/connection.rb', line 37

def query(endpoint, query = nil, http = nil, version = :v4)
  require 'json'

  unless http
    require 'puppet/network/http_pool'
    http = Puppet::Network::HttpPool.http_instance(@host, @port, @use_ssl)
  end
  headers = { 'Accept' => 'application/json' }

  uri = "/pdb/query/#{version}/#{endpoint}"
  uri += URI.escape "?query=#{query.to_json}" unless query.nil? || query.empty?

  debug("PuppetDB query: #{query.to_json}")

  resp = http.get(uri, headers)
  fail "PuppetDB query error: [#{resp.code}] #{resp.msg}, query: #{query.to_json}" unless resp.is_a?(Net::HTTPSuccess)
  JSON.parse(resp.body)
end