Module: Miasma::Contrib::AzureApiCore::ApiCommon
- Defined in:
- lib/miasma/contrib/azure.rb
Class Method Summary collapse
Instance Method Summary collapse
- #access_token_expired? ⇒ Boolean
- #client_access_token ⇒ Object
-
#connect ⇒ Object
Setup for API connections.
-
#connection ⇒ HTTP
Connection for requests (forces headers).
-
#endpoint ⇒ String
Endpoint for request.
-
#make_request(connection, http_method, request_args) ⇒ HTTP::Response
Perform request.
- #oauth_token_buffer_seconds ⇒ Object
- #oauth_token_information ⇒ Object
-
#perform_request_retry(exception) ⇒ TrueClass, FalseClass
Define when request should be retried.
- #request_client_token ⇒ Object
- #retryable_allowed?(*_) ⇒ Boolean
-
#uri_escape(string) ⇒ String
Custom escape.
Class Method Details
.included(klass) ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/miasma/contrib/azure.rb', line 222 def self.included(klass) klass.class_eval do attribute :azure_tenant_id, String attribute :azure_client_id, String attribute :azure_subscription_id, String attribute :azure_client_secret, String attribute :azure_region, String attribute :azure_resource, String, :default => 'https://management.azure.com/' attribute :azure_login_url, String, :default => 'https://login.microsoftonline.com' attribute :azure_blob_account_name, String attribute :azure_blob_secret_key, String attribute :azure_root_orchestration_container, String, :default => 'miasma-orchestration-templates' attr_reader :signer end end |
Instance Method Details
#access_token_expired? ⇒ Boolean
300 301 302 303 304 305 306 307 |
# File 'lib/miasma/contrib/azure.rb', line 300 def access_token_expired? if(oauth_token_information[:expires_on]) (oauth_token_information[:expires_on] + oauth_token_buffer_seconds) < Time.now else true end end |
#client_access_token ⇒ Object
309 310 311 312 |
# File 'lib/miasma/contrib/azure.rb', line 309 def client_access_token request_client_token if access_token_expired? oauth_token_information[:access_token] end |
#connect ⇒ Object
Setup for API connections
240 241 242 |
# File 'lib/miasma/contrib/azure.rb', line 240 def connect @oauth_token_information = Smash.new end |
#connection ⇒ HTTP
Returns connection for requests (forces headers).
245 246 247 248 249 250 251 252 253 |
# File 'lib/miasma/contrib/azure.rb', line 245 def connection unless(signer) super.headers( 'Authorization' => "Bearer #{client_access_token}" ) else super end end |
#endpoint ⇒ String
Returns endpoint for request.
292 293 294 |
# File 'lib/miasma/contrib/azure.rb', line 292 def endpoint azure_resource end |
#make_request(connection, http_method, request_args) ⇒ HTTP::Response
Perform request
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/miasma/contrib/azure.rb', line 261 def make_request(connection, http_method, request_args) dest, = request_args = ? .to_smash : Smash.new [:headers] = Smash[connection..headers.to_a].merge(.fetch(:headers, Smash.new)) service = Bogo::Utility.snake(self.class.name.split('::')[-2,1].first) if(signer) [:headers] ||= Smash.new [:headers]['x-ms-date'] = AzureApiCore.time_rfc1123 if(self.respond_to?(:api_version)) [:headers]['x-ms-version'] = self.send(:api_version) end [:headers]['Authorization'] = signer.generate( http_method, URI.parse(dest).path, ) az_connection = connection.headers([:headers]) else if(self.respond_to?(:api_version)) [:params] ||= Smash.new [:params]['api-version'] = self.send(:api_version) end if(self.respond_to?(:root_path)) p_dest = URI.parse(dest) dest = "#{p_dest.scheme}://#{p_dest.host}" dest = File.join(dest, self.send(:root_path), p_dest.path) end az_connection = connection end az_connection.send(http_method, dest, ) end |
#oauth_token_buffer_seconds ⇒ Object
296 297 298 |
# File 'lib/miasma/contrib/azure.rb', line 296 def oauth_token_buffer_seconds 240 end |
#oauth_token_information ⇒ Object
314 315 316 |
# File 'lib/miasma/contrib/azure.rb', line 314 def oauth_token_information @oauth_token_information end |
#perform_request_retry(exception) ⇒ TrueClass, FalseClass
Define when request should be retried
354 355 356 357 358 359 360 |
# File 'lib/miasma/contrib/azure.rb', line 354 def perform_request_retry(exception) if(exception.is_a?(Error::ApiError::RequestError)) exception.response.code >= 500 else false end end |
#request_client_token ⇒ Object
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/miasma/contrib/azure.rb', line 318 def request_client_token result = HTTP.post( [azure_login_url, azure_tenant_id, 'oauth2', 'token'].join('/'), :form => { :grant_type => 'client_credentials', :client_id => azure_client_id, :client_secret => azure_client_secret, :resource => azure_resource } ) unless(result.code == 200) raise Miasma::Error::ApiError.new( 'Request for client authentication token failed', :response => result ) end @oauth_token_information = MultiJson.load( result.body.to_s ).to_smash @oauth_token_information[:expires_on] = Time.at(@oauth_token_information[:expires_on].to_i) @oauth_token_information[:not_before] = Time.at(@oauth_token_information[:not_before].to_i) @oauth_token_information end |
#retryable_allowed?(*_) ⇒ Boolean
342 343 344 345 346 347 348 |
# File 'lib/miasma/contrib/azure.rb', line 342 def retryable_allowed?(*_) if(ENV['DEBUG']) false else super end end |
#uri_escape(string) ⇒ String
Returns custom escape.
363 364 365 |
# File 'lib/miasma/contrib/azure.rb', line 363 def uri_escape(string) signer.safe_escape(string) end |