Class: PuppetDB::Connection
- Inherits:
-
Object
- Object
- PuppetDB::Connection
- Defined in:
- lib/puppetdb/connection.rb
Instance Method Summary collapse
-
#facts(facts, nodequery, http = nil) ⇒ Hash
Get the listed facts for all nodes matching query return it as a hash of hashes.
-
#initialize(host = 'puppetdb', port = 443, use_ssl = true) ⇒ Connection
constructor
A new instance of Connection.
-
#parse_query(query, endpoint = :nodes) ⇒ Array
Parse a query string into a PuppetDB query.
-
#query(endpoint, query = nil, http = nil) ⇒ Array
Execute a PuppetDB query.
Constructor Details
permalink #initialize(host = 'puppetdb', port = 443, use_ssl = true) ⇒ Connection
Returns a new instance of Connection.
9 10 11 12 13 14 |
# File 'lib/puppetdb/connection.rb', line 9 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
permalink #facts(facts, nodequery, http = nil) ⇒ Hash
Get the listed facts for all nodes matching query return it as a hash of hashes
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/puppetdb/connection.rb', line 31 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'] then facts[fact['certname']][fact['name']] = fact['value'] else facts[fact['certname']] = {fact['name'] => fact['value']} end end facts end |
permalink #parse_query(query, endpoint = :nodes) ⇒ Array
Parse a query string into a PuppetDB query
21 22 23 |
# File 'lib/puppetdb/connection.rb', line 21 def parse_query(query, endpoint=:nodes) @parser.scan_str(query).optimize.evaluate endpoint end |
permalink #query(endpoint, query = nil, http = nil) ⇒ Array
Execute a PuppetDB query
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/puppetdb/connection.rb', line 53 def query(endpoint, query=nil, http=nil) unless http then require 'puppet/network/http_pool' http = Puppet::Network::HttpPool.http_instance(@host, @port, @use_ssl) end headers = { "Accept" => "application/json" } uri = "/v2/#{endpoint.to_s}" uri += URI.escape "?query=#{query.to_json}" unless query.nil? or query.empty? resp = http.get(uri, headers) raise Puppet::Error, "PuppetDB query error: [#{resp.code}] #{resp.msg}, query: #{query.to_json}" unless resp.kind_of?(Net::HTTPSuccess) return JSON.parse(resp.body) end |