Class: PuppetDB::Connection
- Inherits:
-
Object
- Object
- PuppetDB::Connection
- Includes:
- Puppet::Util::Logging
- 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, version = :v3) ⇒ Array
Execute a PuppetDB query.
-
#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.
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
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
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
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
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 |