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
- #debug(msg) ⇒ Object
- #hash_get(hash, key) ⇒ Object
- #hash_includes?(hash, *sought_keys) ⇒ Boolean
-
#initialize(settings, version = 3) ⇒ Client
constructor
A new instance of Client.
- #raise_if_error(response) ⇒ Object
- #request(endpoint, query, opts = {}) ⇒ Object
Constructor Details
#initialize(settings, version = 3) ⇒ Client
Returns a new instance of Client.
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/puppetdb/client.rb', line 50 def initialize(settings, version=3) @version = version server = hash_get(settings, 'server') pem = hash_get(settings, 'pem') scheme = URI.parse(server).scheme unless ['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 unless pem && 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 + '/v' + version.to_s()) 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
#debug(msg) ⇒ Object
44 45 46 47 48 |
# File 'lib/puppetdb/client.rb', line 44 def debug(msg) if @logger @logger.debug(msg) end 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
77 78 79 80 81 |
# File 'lib/puppetdb/client.rb', line 77 def raise_if_error(response) if response.code.to_s() =~ /^[4|5]/ raise APIError.new(response) end end |
#request(endpoint, query, opts = {}) ⇒ Object
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 83 def request(endpoint, query, opts={}) query = PuppetDB::Query.maybe_promote(query) json_query = query.build() path = "/" + endpoint 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, :query => filtered_opts) raise_if_error(ret) total = ret.headers['X-Records'] if total.nil? total = ret.parsed_response.length end Response.new(ret.parsed_response, total) end |