Module: Miasma::Contrib::AzureApiCore::ApiCommon

Included in:
Miasma::Contrib::Azure::Api, Models::Orchestration::Azure, Models::Storage::Azure
Defined in:
lib/miasma/contrib/azure.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Returns:

  • (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_tokenObject



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

#connectObject

Setup for API connections



240
241
242
# File 'lib/miasma/contrib/azure.rb', line 240

def connect
  @oauth_token_information = Smash.new
end

#connectionHTTP

Returns connection for requests (forces headers).

Returns:

  • (HTTP)

    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

#endpointString

Returns endpoint for request.

Returns:

  • (String)

    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

Parameters:

  • connection (HTTP)
  • http_method (Symbol)
  • request_args (Array)

Returns:

  • (HTTP::Response)


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, 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_secondsObject



296
297
298
# File 'lib/miasma/contrib/azure.rb', line 296

def oauth_token_buffer_seconds
  240
end

#oauth_token_informationObject



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

Parameters:

  • exception (Exception)

Returns:

  • (TrueClass, FalseClass)


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_tokenObject



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_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

Returns:

  • (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.

Returns:

  • (String)

    custom escape



363
364
365
# File 'lib/miasma/contrib/azure.rb', line 363

def uri_escape(string)
  signer.safe_escape(string)
end