Class: PuppetDB::Client
- Inherits:
-
Object
- Object
- PuppetDB::Client
- Includes:
- HTTParty
- Defined in:
- lib/puppetdb/client.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
-
#use_ssl ⇒ Object
readonly
Returns the value of attribute use_ssl.
Instance Method Summary collapse
- #command(command, payload, version) ⇒ Object
- #debug(msg) ⇒ Object
- #hash_get(hash, key) ⇒ Object
- #hash_includes?(hash, *sought_keys) ⇒ Boolean
-
#initialize(settings, query_api_version = 4, command_api_version = 1) ⇒ Client
constructor
A new instance of Client.
- #raise_if_error(response) ⇒ Object
- #request(endpoint, query, opts = {}) ⇒ Object
Constructor Details
#initialize(settings, query_api_version = 4, command_api_version = 1) ⇒ Client
Returns a new instance of Client.
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 |
# File 'lib/puppetdb/client.rb', line 48 def initialize(settings, query_api_version = 4, command_api_version = 1) @query_api_version = query_api_version @command_api_version = command_api_version server = hash_get(settings, 'server') pem = hash_get(settings, 'pem') scheme = URI.parse(server).scheme unless %w[http https].include? scheme error_msg = 'Configuration error: :server must specify a protocol of either http or https' raise error_msg end @use_ssl = scheme == 'https' if @use_ssl && pem unless hash_includes?(pem, 'key', 'cert', 'ca_file') error_msg = 'Configuration error: https:// specified but pem is missing or incomplete. It requires cert, key, and ca_file.' raise error_msg end self.class. = { pem: pem } self.class.connection_adapter(FixSSLConnectionAdapter) end self.class.base_uri(server) end |
Instance Attribute Details
#logger=(value) ⇒ Object (writeonly)
Sets the attribute logger
24 25 26 |
# File 'lib/puppetdb/client.rb', line 24 def logger=(value) @logger = value end |
#use_ssl ⇒ Object (readonly)
Returns the value of attribute use_ssl.
23 24 25 |
# File 'lib/puppetdb/client.rb', line 23 def use_ssl @use_ssl end |
Instance Method Details
#command(command, payload, version) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/puppetdb/client.rb', line 111 def command(command, payload, version) path = "/pdb/cmd/v#{@command_api_version}" query = { 'command' => command, 'version' => version, 'certname' => payload['certname'] } debug("#{path} #{query} #{payload}") ret = self.class.post( path, query: query, body: payload.to_json, headers: { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } ) raise_if_error(ret) Response.new(ret.parsed_response) end |
#debug(msg) ⇒ Object
44 45 46 |
# File 'lib/puppetdb/client.rb', line 44 def debug(msg) @logger.debug(msg) if @logger end |
#hash_get(hash, key) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/puppetdb/client.rb', line 26 def hash_get(hash, key) untouched = hash[key] return untouched if untouched sym = hash[key.to_sym] return sym if sym str = hash[key.to_s] return str if str nil end |
#hash_includes?(hash, *sought_keys) ⇒ Boolean
39 40 41 42 |
# File 'lib/puppetdb/client.rb', line 39 def hash_includes?(hash, *sought_keys) sought_keys.each { |x| return false unless hash.include?(x) } true end |
#raise_if_error(response) ⇒ Object
76 77 78 |
# File 'lib/puppetdb/client.rb', line 76 def raise_if_error(response) raise APIError, response if response.code.to_s =~ %r{^[4|5]} end |
#request(endpoint, query, opts = {}) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/puppetdb/client.rb', line 80 def request(endpoint, query, opts = {}) path = "/pdb/query/v#{@query_api_version}" if endpoint == '' # PQL json_query = query else path += "/#{endpoint}" query = PuppetDB::Query.maybe_promote(query) json_query = query.build end filtered_opts = { 'query' => json_query } opts.each do |k, v| if k == :counts_filter filtered_opts['counts-filter'] = JSON.dump(v) else filtered_opts[k.to_s.sub('_', '-')] = v end end debug("#{path} #{json_query} #{opts}") ret = self.class.get(path, body: filtered_opts) raise_if_error(ret) total = ret.headers['X-Records'] total = ret.parsed_response.length if total.nil? Response.new(ret.parsed_response, total) end |