Class: Bolt::PuppetDB::Client
- Inherits:
-
Object
- Object
- Bolt::PuppetDB::Client
- Defined in:
- lib/bolt/puppetdb/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
- #fact_values(certnames = [], facts = []) ⇒ Object
-
#facts_for_node(certnames) ⇒ Object
This method expects an array of certnames to get facts for.
- #headers ⇒ Object
- #http_client ⇒ Object
-
#initialize(config) ⇒ Client
constructor
A new instance of Client.
- #make_query(query, path = nil) ⇒ Object
- #query_certnames(query) ⇒ Object
- #reject_url ⇒ Object
- #uri ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'lib/bolt/puppetdb/client.rb', line 9 def config @config end |
Instance Method Details
#fact_values(certnames = [], facts = []) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bolt/puppetdb/client.rb', line 47 def fact_values(certnames = [], facts = []) return {} if certnames.empty? || facts.empty? certnames.uniq! name_query = certnames.map { |c| ["=", "certname", c] } name_query.insert(0, "or") facts_query = facts.map { |f| ["=", "path", f] } facts_query.insert(0, "or") query = ['and', name_query, facts_query] @logger.debug("Querying certnames") result = make_query(query, 'fact-contents') result.map! { |h| h.delete_if { |k, _v| %w[environment name].include?(k) } } result.group_by { |c| c['certname'] } end |
#facts_for_node(certnames) ⇒ Object
This method expects an array of certnames to get facts for
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/bolt/puppetdb/client.rb', line 32 def facts_for_node(certnames) return {} if certnames.empty? || certnames.nil? certnames.uniq! name_query = certnames.map { |c| ["=", "certname", c] } name_query.insert(0, "or") @logger.debug("Querying certnames") result = make_query(name_query, 'inventory') result&.each_with_object({}) do |node, coll| coll[node['certname']] = node['facts'] end end |
#headers ⇒ Object
131 132 133 134 135 |
# File 'lib/bolt/puppetdb/client.rb', line 131 def headers headers = { 'Content-Type' => 'application/json' } headers['X-Authentication'] = @config.token if @config.token headers end |
#http_client ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/bolt/puppetdb/client.rb', line 98 def http_client return @http if @http # lazy-load expensive gem code require 'httpclient' @logger.trace("Creating HTTP Client") @http = HTTPClient.new @http.ssl_config.set_client_cert_file(@config.cert, @config.key) if @config.cert @http.ssl_config.add_trust_ca(@config.cacert) @http.connect_timeout = @config.connect_timeout if @config.connect_timeout @http.receive_timeout = @config.read_timeout if @config.read_timeout @http end |
#make_query(query, path = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/bolt/puppetdb/client.rb', line 65 def make_query(query, path = nil) body = JSON.generate(query: query) url = "#{uri}/pdb/query/v4" url += "/#{path}" if path begin @logger.debug("Sending PuppetDB query to #{url}") response = http_client.post(url, body: body, header: headers) rescue StandardError => e raise Bolt::PuppetDBFailoverError, "Failed to query PuppetDB: #{e}" end @logger.debug("Got response code #{response.code} from PuppetDB") if response.code != 200 msg = "Failed to query PuppetDB: #{response.body}" if response.code == 400 raise Bolt::PuppetDBError, msg else raise Bolt::PuppetDBFailoverError, msg end end begin JSON.parse(response.body) rescue JSON::ParserError raise Bolt::PuppetDBError, "Unable to parse response as JSON: #{response.body}" end rescue Bolt::PuppetDBFailoverError => e @logger.error("Request to puppetdb at #{@current_url} failed with #{e}.") reject_url make_query(query, path) end |
#query_certnames(query) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bolt/puppetdb/client.rb', line 18 def query_certnames(query) return [] unless query @logger.debug("Querying certnames") results = make_query(query) if results&.first && !results.first&.key?('certname') fields = results.first&.keys raise Bolt::PuppetDBError, "Query results did not contain a 'certname' field: got #{fields.join(', ')}" end results&.map { |result| result['certname'] }&.uniq end |
#reject_url ⇒ Object
112 113 114 115 |
# File 'lib/bolt/puppetdb/client.rb', line 112 def reject_url @bad_urls << @current_url if @current_url @current_url = nil end |
#uri ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/bolt/puppetdb/client.rb', line 117 def uri require 'addressable/uri' @current_url ||= (@config.server_urls - @bad_urls).first unless @current_url msg = "Failed to connect to all PuppetDB server_urls: #{@config.server_urls.to_a.join(', ')}." raise Bolt::PuppetDBError, msg end uri = Addressable::URI.parse(@current_url) uri.port ||= 8081 uri end |