Class: PuppetDB::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetdb/connection.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Connection.



8
9
10
11
12
13
# File 'lib/puppetdb/connection.rb', line 8

def initialize(host='puppetdb', port=443, use_ssl=true)
  @host = host
  @port = port
  @use_ssl = use_ssl
  @parser = PuppetDB::Parser.new
end

Instance Method Details

#facts(facts, nodequery, http = nil) ⇒ Hash

Get the listed facts for all nodes matching query return it as a hash of hashes

Parameters:

  • facts (Array)

    the list of facts to fetch

  • nodequery (Array)

    the query to find the nodes to fetch facts for

Returns:

  • (Hash)

    a hash of hashes with facts for each node mathing query



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/puppetdb/connection.rb', line 32

def facts(facts, nodequery, http = nil)
  if facts.empty?
    q = ['in', 'certname', ['extract', 'certname', ['select-facts', nodequery]]]
  else
    q = ['and', ['in', 'certname', ['extract', 'certname', ['select-facts', nodequery]]], ['or', *facts.collect { |f| ['=', 'name', f]}]]
  end
  facts = {}
  query(:facts, q, http).each do |fact|
    if facts.include? fact['certname']
      facts[fact['certname']][fact['name']] = fact['value']
    else
      facts[fact['certname']] = { fact['name'] => fact['value'] }
    end
  end
  facts
end

#parse_query(query, endpoint = :nodes) ⇒ Array

Parse a query string into a PuppetDB query

Parameters:

  • query (String)

    the query string to parse

  • endpoint (Symbol) (defaults to: :nodes)

    the endpoint for which the query should be evaluated

Returns:

  • (Array)

    the PuppetDB query



20
21
22
23
24
# File 'lib/puppetdb/connection.rb', line 20

def parse_query(query, endpoint = :nodes)
  if query = @parser.scan_str(query)
    query.optimize.evaluate endpoint
  end
end

#query(endpoint, query = nil, http = nil, version = :v3) ⇒ 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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppetdb/connection.rb', line 80

def query(endpoint, query = nil, http = nil, version = :v3)
  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 = "/#{version}/#{endpoint}"
  uri += URI.escape "?query=#{query.to_json}" unless query.nil? || query.empty?

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

#resources(nodequery, resquery, http = nil) ⇒ Hash

Get the listed resources for all nodes matching query return it as a hash of hashes

Parameters:

  • resquery (Array)

    a resources query for what resources to fetch

  • nodequery (Array)

    the query to find the nodes to fetch resources for, optionally empty

Returns:

  • (Hash)

    a hash of hashes with resources for each node mathing query



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/puppetdb/connection.rb', line 55

def resources(nodequery, resquery, http=nil)
  if resquery and ! resquery.empty?
    if nodequery and ! nodequery.empty?
      q = ['and', resquery, nodequery]
    else
      q = resquery
    end
  else
    raise RuntimeError, "PuppetDB resources query error: at least one argument must be non empty; arguments were: nodequery: #{nodequery.inspect} and requery: #{resquery.inspect}"
  end
  resources = {}
  query(:resources, q, http).each do |resource|
    unless resources.has_key? resource['certname']
      resources[resource['certname']] = []
    end
    resources[resource['certname']] << resource
  end
  return resources
end