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
- #export(filename, opts = {}) ⇒ Object
- #hash_includes?(hash, *sought_keys) ⇒ Boolean
- #import(filename) ⇒ Object
-
#initialize(settings = {}, query_api_version = 4, command_api_version = 1, admin_api_version = 1) ⇒ Client
constructor
A new instance of Client.
- #raise_if_error(response) ⇒ Object
- #request(endpoint, query, opts = {}) ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(settings = {}, query_api_version = 4, command_api_version = 1, admin_api_version = 1) ⇒ Client
Returns a new instance of Client.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/puppetdb/client.rb', line 31 def initialize(settings = {}, query_api_version = 4, command_api_version = 1, admin_api_version = 1) config = Config.new(settings, load_files: true) @query_api_version = query_api_version @command_api_version = command_api_version @admin_api_version = admin_api_version @servers = config.server_urls pem = config.pem token = config.token @servers.each do |server| scheme = URI.parse(server).scheme @use_ssl ||= scheme == 'https' unless %w[http https].include? scheme error_msg = "Configuration error: server_url '#{server}' must specify a protocol of either http or https" raise error_msg end end return unless @use_ssl unless hash_includes?(pem, :cacert, :cert, :key) || (pem[:cert].nil? && pem[:key].nil?) error_msg = 'Configuration error: https:// specified, but configuration is incomplete. It requires cacert and either cert and key, or a valid token.' raise error_msg end self.class. = pem self.class.headers('X-Authentication' => token) if token self.class.connection_adapter(FixSSLConnectionAdapter) end |
Instance Attribute Details
#logger=(value) ⇒ Object (writeonly)
Sets the attribute logger
20 21 22 |
# File 'lib/puppetdb/client.rb', line 20 def logger=(value) @logger = value end |
#use_ssl ⇒ Object (readonly)
Returns the value of attribute use_ssl.
19 20 21 |
# File 'lib/puppetdb/client.rb', line 19 def use_ssl @use_ssl end |
Instance Method Details
#command(command, payload, version) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/puppetdb/client.rb', line 121 def command(command, payload, version) path = "/pdb/cmd/v#{@command_api_version}" query = { 'command' => command, 'version' => version, 'certname' => payload['certname'] } debug("#{path} #{query} #{payload}") self.class.base_uri(@servers.first) 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
27 28 29 |
# File 'lib/puppetdb/client.rb', line 27 def debug(msg) @logger.debug(msg) if @logger end |
#export(filename, opts = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/puppetdb/client.rb', line 147 def export(filename, opts = {}) self.class.base_uri(@servers.first) anonymization_profile = opts.delete(:anonymization_profile) || 'none' path = "/pdb/admin/v#{@admin_api_version}/archive?anonymization_profile=#{anonymization_profile}" # Enforce stream_body to avoid using memory params = opts.merge(stream_body: true) File.open(filename, 'w') do |file| self.class.get(path, params) do |fragment| if [301, 302].include?(fragment.code) debug 'Skip streaming write for redirect' elsif fragment.code == 200 file.write(fragment) else raise StandardError, "Non-success status code while streaming #{fragment.code}" end end end end |
#hash_includes?(hash, *sought_keys) ⇒ Boolean
22 23 24 25 |
# File 'lib/puppetdb/client.rb', line 22 def hash_includes?(hash, *sought_keys) sought_keys.each { |x| return false unless hash.include?(x) } true end |
#import(filename) ⇒ Object
168 169 170 171 172 |
# File 'lib/puppetdb/client.rb', line 168 def import(filename) self.class.base_uri(@servers.first) path = "/pdb/admin/v#{@admin_api_version}/archive" self.class.post(path, body: { archive: File.open(filename) }) end |
#raise_if_error(response) ⇒ Object
62 63 64 65 66 |
# File 'lib/puppetdb/client.rb', line 62 def raise_if_error(response) raise UnauthorizedError, response if response.code == 401 raise ForbiddenError, response if response.code == 403 raise APIError, response if response.code.to_s =~ %r{^[4|5]} end |
#request(endpoint, query, opts = {}) ⇒ Object
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/puppetdb/client.rb', line 68 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 query_mode = opts.delete(:query_mode) || :first 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}") if query_mode == :first self.class.base_uri(@servers.first) 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) elsif query_mode == :failover ret = nil @servers.each do |server| self.class.base_uri(server) ret = self.class.get(path, body: filtered_opts) if ret.code < 400 total = ret.headers['X-Records'] total = ret.parsed_response.length if total.nil? return Response.new(ret.parsed_response, total) else debug("query on '#{server}' failed with #{ret.code}") end end raise APIError, ret else raise ArgumentError, "Query mode '#{query_mode}' is not supported (try :first or :failover)." end end |
#status ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/puppetdb/client.rb', line 174 def status status_endpoint = '/status/v1/services' status_map = {} @servers.each do |server| self.class.base_uri(server) ret = self.class.get(status_endpoint) status_map[server] = if ret.code >= 400 { error: "Unable to build JSON object from server: #{server}" } else ret.parsed_response end end status_map end |