Class: Puppet::Provider::SensuAPI
- Inherits:
-
Puppet::Provider
- Object
- Puppet::Provider
- Puppet::Provider::SensuAPI
- Defined in:
- lib/puppet/provider/sensu_api.rb
Class Attribute Summary collapse
-
.access_token ⇒ Object
Returns the value of attribute access_token.
-
.password ⇒ Object
Returns the value of attribute password.
-
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
-
.url ⇒ Object
Returns the value of attribute url.
-
.username ⇒ Object
Returns the value of attribute username.
-
.validate_namespaces ⇒ Object
Returns the value of attribute validate_namespaces.
Class Method Summary collapse
- .api_request(path, data = nil, opts = {}) ⇒ Object
- .auth(username, password) ⇒ Object
- .auth_test(url = nil, username, password) ⇒ Object
- .auth_token ⇒ Object
- .get_bonsai_asset(name, opts = {}) ⇒ Object
- .get_bonsai_latest_version(namespace, name, opts = {}) ⇒ Object
- .namespaces(opts = {}) ⇒ Object
- .type_properties ⇒ Object
- .update_access_token ⇒ Object
- .version ⇒ Object
- .version_cmp(v) ⇒ Object
Instance Method Summary collapse
- #api_request(*args) ⇒ Object
- #auth_test(*args) ⇒ Object
- #convert_boolean_property_value(value) ⇒ Object
- #get_bonsai_asset(name) ⇒ Object
- #get_entity(entity, namespace, opts = {}) ⇒ Object
- #namespaces ⇒ Object
- #type_properties ⇒ Object
- #update_access_token ⇒ Object
- #validate_namespaces ⇒ Object
- #version ⇒ Object
- #version_cmp(*args) ⇒ Object
Class Attribute Details
.access_token ⇒ Object
Returns the value of attribute access_token.
12 13 14 |
# File 'lib/puppet/provider/sensu_api.rb', line 12 def access_token @access_token end |
.password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/puppet/provider/sensu_api.rb', line 11 def password @password end |
.refresh_token ⇒ Object
Returns the value of attribute refresh_token.
13 14 15 |
# File 'lib/puppet/provider/sensu_api.rb', line 13 def refresh_token @refresh_token end |
.url ⇒ Object
Returns the value of attribute url.
9 10 11 |
# File 'lib/puppet/provider/sensu_api.rb', line 9 def url @url end |
.username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/puppet/provider/sensu_api.rb', line 10 def username @username end |
.validate_namespaces ⇒ Object
Returns the value of attribute validate_namespaces.
14 15 16 |
# File 'lib/puppet/provider/sensu_api.rb', line 14 def validate_namespaces @validate_namespaces end |
Class Method Details
.api_request(path, data = nil, 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 120 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/puppet/provider/sensu_api.rb', line 68 def self.api_request(path, data = nil, opts = {}) api_group = opts[:api_group] || 'core' api_version = opts[:api_version] || 'v2' namespace = opts[:namespace] || nil url = opts[:url] || @url username = opts[:username] || @username password = opts[:password] || @password method = opts[:method] || 'get' failonfail = opts[:failonfail].nil? ? true : opts[:failonfail] if opts[:http_proxy] proxy = URI.parse(opts[:http_proxy]) proxy_addr = proxy.host proxy_port = proxy.port proxy_user = proxy.user proxy_password = proxy.password else proxy_addr = nil proxy_port = nil proxy_user = nil proxy_password = nil end no_proxy = opts[:no_proxy] if opts[:use_token] == false token = nil else token = @access_token end if path =~ %r{^/} uri = URI(URI.join(url, path)) elsif namespace uri = URI(URI.join(url, "/api/#{api_group}/#{api_version}/namespaces/#{namespace}/#{path}")) else uri = URI(URI.join(url, "/api/#{api_group}/#{api_version}/#{path}")) end if method == 'get' && !data.nil? uri.query = URI.encode_www_form(data) end Puppet.debug("method=#{method} url=#{uri.to_s} path=#{path}") http = Net::HTTP.new(uri.host, uri.port, proxy_addr, proxy_port, proxy_user, proxy_password, no_proxy) http.use_ssl = (uri.scheme == 'https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl? if method == 'get' request = Net::HTTP::Get.new(uri.path) elsif method == 'post' request = Net::HTTP::Post.new(uri.path) elsif method == 'put' request = Net::HTTP::Put.new(uri.path) elsif method == 'patch' request = Net::HTTP::Patch.new(uri.path) elsif method == 'delete' request = Net::HTTP::Delete.new(uri.path) end # Add data for POST, PUT, and PATCH if ['post','put','patch'].include?(method) Puppet.debug("BODY: #{data.to_json}") request.body = data.to_json unless data.nil? end # Add headers request.add_field("Accept", "application/json") if defined?(request) && !request.nil? if method == 'patch' request.add_field("Content-Type", "application/merge-patch+json") if defined?(request) && !request.nil? else request.add_field("Content-Type", "application/json") if defined?(request) && !request.nil? end # Add either token or basic auth if token.nil? && username && password && opts[:auth] != false Puppet.debug("Sensu API: Using basic auth of #{username}:#{password}") request.basic_auth(username, password) if defined?(request) && !request.nil? else Puppet.debug("Sensu API: Using token #{token}") request.add_field("Authorization", "Bearer #{token}") if defined?(request) && !request.nil? end # Make request if method == 'post-form' || method == 'put-form' encoded_form = URI.encode_www_form(data) headers = { content_type: "application/x-www-form-urlencoded", authorization: "Bearer #{token}" } end if method == 'post-form' response = http.request_post(uri.path, encoded_form, headers) elsif method == 'put-form' response = http.request_put(uri.path, encoded_form, headers) else response = http.request(request) end Puppet.debug("RESPONSE: #{response.code}\n#{response.body}") return response if opts[:return_response] # Handle expired auth token and retry if response.kind_of?(Net::HTTPUnauthorized) && opts[:retry] != false update_access_token opts[:retry] = false return api_request(path, data, opts) end if response.kind_of?(Net::HTTPNotFound) if failonfail raise Puppet::Error, "Resource not found at URL #{uri.to_s}: #{response.class}" else return {} end end unless response.kind_of?(Net::HTTPSuccess) raise Puppet::Error, "Unable to make API request at #{uri.to_s}: #{response.class}" end if Puppet::Provider::Sensuctl.valid_json?(response.body) data = JSON.parse(response.body) Puppet.debug("BODY: #{JSON.pretty_generate(data)}") return data else Puppet.debug("BODY: Not valid JSON") return {} end rescue Exception => e if failonfail raise else Puppet.err "Unable to connect to #{uri.to_s}: #{e.}" return {} end rescue Puppet::Error => e if failonfail raise else Puppet.err "Unable to connect to #{uri.to_s}: #{e.}" return {} end end |
.auth(username, password) ⇒ Object
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/puppet/provider/sensu_api.rb', line 197 def self.auth(username, password) opts = { username: username, password: password, use_token: false, return_response: true, } response = api_request('/auth', nil, opts) if response.kind_of?(Net::HTTPSuccess) data = JSON.parse(response.body) @access_token = data['access_token'] @refresh_token = data['refresh_token'] return true else return false end end |
.auth_test(url = nil, username, password) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/puppet/provider/sensu_api.rb', line 237 def self.auth_test(url = nil, username, password) opts = { :url => url, :username => username, :password => password, :return_response => true, :use_token => false } response = api_request('/auth/test', nil, opts) if response.kind_of?(Net::HTTPSuccess) return true elsif response.kind_of?(Net::HTTPUnauthorized) return false else Puppet.debug "Error testing username/password using SensuAPI" return false end end |
.auth_token ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/puppet/provider/sensu_api.rb', line 215 def self.auth_token() opts = { auth: false, return_response: true, method: 'post', } if @refresh_token.nil? config = Puppet::Provider::Sensuctl.sensuctl_config @access_token = config['access_token'] @refresh_token = config['refresh_token'] end response = api_request('/auth/token', {'refresh_token' => @refresh_token}, opts) if response.kind_of?(Net::HTTPSuccess) data = JSON.parse(response.body) @access_token = data['access_token'] @refresh_token = data['refresh_token'] return true else return false end end |
.get_bonsai_asset(name, opts = {}) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/puppet/provider/sensu_api.rb', line 286 def self.get_bonsai_asset(name, opts = {}) opts = { :url => 'https://bonsai.sensu.io' }.merge(opts) data = api_request("/api/v1/assets/#{name}", nil, opts) rescue Exception => e Puppet.notice "Unable to connect to bonsai at #{url}: #{e.}" Puppet.debug("ERROR: #{e.backtrace.join("\n")}") return {} else return data end |
.get_bonsai_latest_version(namespace, name, opts = {}) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/puppet/provider/sensu_api.rb', line 301 def self.get_bonsai_latest_version(namespace, name, opts = {}) return nil if namespace.nil? || name.nil? full_name = "#{namespace}/#{name}" @latest_version = {} if @latest_version.nil? return @latest_version[full_name] if @latest_version[full_name] @latest_version[full_name] = nil versions = [] bonsai_asset = Puppet::Provider::SensuAPI.get_bonsai_asset(full_name, opts) (bonsai_asset['versions'] || []).each do |bonsai_version| version = bonsai_version['version'] next unless version =~ /^[0-9]/ versions << version end versions = versions.sort_by { |v| Gem::Version.new(v) } @latest_version[full_name] = versions.last @latest_version[full_name] end |
.namespaces(opts = {}) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/puppet/provider/sensu_api.rb', line 42 def self.namespaces(opts = {}) opts[:namespace] = nil Puppet.debug("Fetching namespaces via Sensu API") data = api_request('namespaces', nil, opts) names = [] data.each do |d| names << d['name'] end names rescue Exception => e Puppet.debug("ERROR fetching namespaces via Sensu API: #{e.backtrace.join("\n")}") return [] end |
.type_properties ⇒ Object
31 32 33 |
# File 'lib/puppet/provider/sensu_api.rb', line 31 def self.type_properties resource_type.validproperties.reject { |p| p.to_sym == :ensure } end |
.update_access_token ⇒ Object
21 22 23 24 25 26 |
# File 'lib/puppet/provider/sensu_api.rb', line 21 def self.update_access_token auth_success = self.auth(@username, @password) return if auth_success auth_token_success = self.auth_token() return if auth_token_success end |
.version ⇒ Object
259 260 261 262 263 264 265 |
# File 'lib/puppet/provider/sensu_api.rb', line 259 def self.version data = api_request('/version', nil, {:failonfail => false}) data.fetch('sensu_backend', nil) rescue Exception => e Puppet.notice "Unable to query Sensu API version: #{e.}" return nil end |
.version_cmp(v) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/puppet/provider/sensu_api.rb', line 270 def self.version_cmp(v) if @current_version.nil? @current_version = version end return true if @current_version.nil? Gem::Version.new(@current_version) >= Gem::Version.new(v) # Rescue in case version is not numeric such as well Sensu Go is built from source, the version may be "(devel)" # See https://github.com/sensu/sensu-puppet/issues/1278 rescue ArgumentError => e Puppet.debug "Unable to compare version #{@current_version} with needed version #{v}: #{e}" return true end |
Instance Method Details
#api_request(*args) ⇒ Object
193 194 195 |
# File 'lib/puppet/provider/sensu_api.rb', line 193 def api_request(*args) self.class.api_request(*args) end |
#auth_test(*args) ⇒ Object
255 256 257 |
# File 'lib/puppet/provider/sensu_api.rb', line 255 def auth_test(*args) self.class.auth_test(*args) end |
#convert_boolean_property_value(value) ⇒ Object
38 39 40 |
# File 'lib/puppet/provider/sensu_api.rb', line 38 def convert_boolean_property_value(value) Puppet::Provider::Sensuctl.convert_boolean_property_value(value) end |
#get_bonsai_asset(name) ⇒ Object
298 299 300 |
# File 'lib/puppet/provider/sensu_api.rb', line 298 def get_bonsai_asset(name) self.class.get_bonsai_asset(name) end |
#get_entity(entity, namespace, opts = {}) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/puppet/provider/sensu_api.rb', line 59 def get_entity(entity, namespace, opts = {}) opts[:namespace] = namespace Puppet.debug("Fetching entity #{entity} via Sensu API") data = api_request("entities/#{entity}", nil, opts) return data rescue Exception => e raise Puppet::Error, "Unable to query entity data for entity #{entity}: #{e}" end |
#namespaces ⇒ Object
55 56 57 |
# File 'lib/puppet/provider/sensu_api.rb', line 55 def namespaces self.class.namespaces end |
#type_properties ⇒ Object
34 35 36 |
# File 'lib/puppet/provider/sensu_api.rb', line 34 def type_properties self.class.type_properties end |
#update_access_token ⇒ Object
27 28 29 |
# File 'lib/puppet/provider/sensu_api.rb', line 27 def update_access_token self.class.update_access_token end |
#validate_namespaces ⇒ Object
17 18 19 |
# File 'lib/puppet/provider/sensu_api.rb', line 17 def validate_namespaces self.class.validate_namespaces end |
#version ⇒ Object
266 267 268 |
# File 'lib/puppet/provider/sensu_api.rb', line 266 def version self.class.version end |
#version_cmp(*args) ⇒ Object
282 283 284 |
# File 'lib/puppet/provider/sensu_api.rb', line 282 def version_cmp(*args) self.class.version_cmp(*args) end |