Class: Bolt::PuppetDB::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.



13
14
15
16
17
18
# File 'lib/bolt/puppetdb/client.rb', line 13

def initialize(config)
  @config = config
  @bad_urls = []
  @current_url = nil
  @logger = Logging.logger[self]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/bolt/puppetdb/client.rb', line 11

def config
  @config
end

Instance Method Details

#facts_for_node(certnames) ⇒ Object

This method expects an array of certnames to get facts for



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/bolt/puppetdb/client.rb', line 33

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")
  result = make_query(name_query, 'inventory')

  result&.each_with_object({}) do |node, coll|
    coll[node['certname']] = node['facts']
  end
end

#headersObject



103
104
105
106
107
# File 'lib/bolt/puppetdb/client.rb', line 103

def headers
  headers = { 'Content-Type' => 'application/json' }
  headers['X-Authentication'] = @config.token if @config.token
  headers
end

#http_clientObject



77
78
79
80
81
82
83
84
# File 'lib/bolt/puppetdb/client.rb', line 77

def http_client
  return @http if @http
  @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
end

#make_query(query, path = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/bolt/puppetdb/client.rb', line 46

def make_query(query, path = nil)
  body = JSON.generate(query: query)
  url = "#{uri}/pdb/query/v4"
  url += "/#{path}" if path

  begin
    response = http_client.post(url, body: body, header: headers)
  rescue StandardError => err
    raise Bolt::PuppetDBFailoverError, "Failed to query PuppetDB: #{err}"
  end

  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 => err
  @logger.error("Request to puppetdb at #{@current_url} failed with #{err}.")
  reject_url
  make_query(query, path)
end

#query_certnames(query) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bolt/puppetdb/client.rb', line 20

def query_certnames(query)
  return [] unless query

  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_urlObject



86
87
88
89
# File 'lib/bolt/puppetdb/client.rb', line 86

def reject_url
  @bad_urls << @current_url if @current_url
  @current_url = nil
end

#uriObject



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bolt/puppetdb/client.rb', line 91

def 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 = URI.parse(@current_url)
  uri.port ||= 8081
  uri
end