module Miasma::Contrib::AzureApiCore::ApiCommon
Public Class Methods
included(klass)
click to toggle source
# 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
Public Instance Methods
access_token_expired?()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
Setup for API connections
# File lib/miasma/contrib/azure.rb, line 240 def connect @oauth_token_information = Smash.new end
connection()
click to toggle source
@return [HTTP] connection for requests (forces headers)
Calls superclass method
# File lib/miasma/contrib/azure.rb, line 245 def connection unless(signer) super.headers( 'Authorization' => "Bearer #{client_access_token}" ) else super end end
endpoint()
click to toggle source
@return [String] endpoint for request
# File lib/miasma/contrib/azure.rb, line 292 def endpoint azure_resource end
make_request(connection, http_method, request_args)
click to toggle source
Perform request
@param connection [HTTP] @param http_method [Symbol] @param request_args [Array] @return [HTTP::Response]
# File lib/miasma/contrib/azure.rb, line 261 def make_request(connection, http_method, request_args) dest, options = request_args options = options ? options.to_smash : Smash.new options[:headers] = Smash[connection.default_options.headers.to_a].merge(options.fetch(:headers, Smash.new)) service = Bogo::Utility.snake(self.class.name.split('::')[-2,1].first) if(signer) options[:headers] ||= Smash.new options[:headers]['x-ms-date'] = AzureApiCore.time_rfc1123 if(self.respond_to?(:api_version)) options[:headers]['x-ms-version'] = self.send(:api_version) end options[:headers]['Authorization'] = signer.generate( http_method, URI.parse(dest).path, options ) az_connection = connection.headers(options[:headers]) else if(self.respond_to?(:api_version)) options[:params] ||= Smash.new options[: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, options) end
oauth_token_buffer_seconds()
click to toggle source
# File lib/miasma/contrib/azure.rb, line 296 def oauth_token_buffer_seconds 240 end
oauth_token_information()
click to toggle source
# File lib/miasma/contrib/azure.rb, line 314 def oauth_token_information @oauth_token_information end
perform_request_retry(exception)
click to toggle source
Define when request should be retried
@param exception [Exception] @return [TrueClass, FalseClass]
# 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()
click to toggle source
# 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?(*_)
click to toggle source
Calls superclass method
# File lib/miasma/contrib/azure.rb, line 342 def retryable_allowed?(*_) if(ENV['DEBUG']) false else super end end
uri_escape(string)
click to toggle source
@return [String] custom escape
# File lib/miasma/contrib/azure.rb, line 363 def uri_escape(string) signer.safe_escape(string) end