Class: PuppetDB::Connection

Inherits:
Object
  • Object
show all
Includes:
Puppet::Util::Logging
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.



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

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



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

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



24
25
26
27
28
# File 'lib/puppetdb/connection.rb', line 24

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppetdb/connection.rb', line 92

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?

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

  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, grouphosts = true) ⇒ Hash|Array

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

  • grouphosts (Boolean) (defaults to: true)

    whether or not to group the results by the host they belong to

Returns:

  • (Hash|Array)

    a hash of hashes with resources for each node mathing query or array if grouphosts was false



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
# File 'lib/puppetdb/connection.rb', line 60

def resources(nodequery, resquery, http=nil, grouphosts=true)
  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 = {}
  results = query(:resources, q, http)

  if grouphosts
    results.each do |resource|
      unless resources.has_key? resource['certname']
        resources[resource['certname']] = []
      end
      resources[resource['certname']] << resource
    end
  else
    resources = results
  end

  return resources
end