class Azure::Armrest::Configuration

Attributes

accept[RW]

The accept type specified for http request results. The default is ‘application/json’

api_version[RW]

The api-version string

client_id[RW]

The client ID used to gather token information.

client_key[RW]

The client key used to gather token information.

content_type[RW]

The content type specified for http requests. The default is ‘application/json’

environment[RW]

The environment object which determines various endpoint URL’s. The default is Azure::Armrest::Environment::Public.

grant_type[RW]

The grant type. The default is client_credentials.

max_retries[RW]

Maximum number of attempts to retry an http request in the case of request throttling or server side service issues.

max_threads[RW]

Maximum number of threads to use within methods that use Parallel for thread pooling.

providers[R]

Namespace providers, their resource types, locations and supported api-version strings.

proxy[R]

Proxy to be used for all http requests.

resource_group[RW]

The resource group used for http requests.

ssl_verify[RW]

SSL verify mode for all http requests.

ssl_version[RW]

SSL version to be used for all http requests.

subscription_id[R]

The subscription ID used for each http request.

tenant_id[RW]

The tenant ID used to gather token information.

timeout[RW]

Timeout value for http requests in seconds. The default is 60.

Public Class Methods

cache_token(configuration) click to toggle source

Cache the token for a configuration that a token has been fetched from Azure

# File lib/azure/armrest/configuration.rb, line 21
def self.cache_token(configuration)
  raise ArgumentError, "Configuration does not have a token" if configuration.token.nil?
  token_cache[configuration.hash] = [configuration.token, configuration.token_expiration]
end
clear_caches() click to toggle source

Clear all class level caches. Typically used for testing only.

# File lib/azure/armrest/configuration.rb, line 5
def self.clear_caches
  token_cache.clear
end
log() click to toggle source

Returns the logger instance. It might be initially set through a log file path, file handler, or already a logger instance.

# File lib/azure/armrest/configuration.rb, line 206
def self.log
  RestClient.log
end
log=(output) click to toggle source

Sets the log to output, which can be a file, a file handle, or a logger instance

# File lib/azure/armrest/configuration.rb, line 213
def self.log=(output)
  case output
    when String
      RestClient.log = Logger.new(output)
    else
      RestClient.log = output
  end
end
new(args) click to toggle source

Yields a new Azure::Armrest::Configuration objects. Note that you must specify a client_id, client_key, tenant_id. The subscription_id is optional but should be specified in most cases. All other parameters are optional.

Example:

config = Azure::Armrest::Configuration.new(
  :client_id       => 'xxxx',
  :client_key      => 'yyyy',
  :tenant_id       => 'zzzz',
  :subscription_id => 'abcd'
)

If you specify a :resource_group, that group will be used for resource group based service class requests. Otherwise, you will need to specify a resource group for most service methods.

Although you can specify an :api_version, it is typically overridden by individual service classes.

The constructor will also validate that the subscription ID is valid if present.

# File lib/azure/armrest/configuration.rb, line 102
def initialize(args)
  # Use defaults, and override with provided arguments
  options = {
    :api_version   => '2015-01-01',
    :accept        => 'application/json',
    :content_type  => 'application/json',
    :grant_type    => 'client_credentials',
    :proxy         => ENV['http_proxy'],
    :ssl_version   => 'TLSv1_2',
    :timeout       => 60,
    :max_threads   => 10,
    :max_retries   => 3,
    :environment   => Azure::Armrest::Environment::Public
  }.merge(args.symbolize_keys)

  # Avoid thread safety issues for VCR testing.
  options[:max_threads] = 1 if defined?(VCR)

  user_token = options.delete(:token)
  user_token_expiration = options.delete(:token_expiration)

  # We need to ensure these are set before subscription_id=
  @tenant_id = options.delete(:tenant_id)
  @client_id = options.delete(:client_id)
  @client_key = options.delete(:client_key)

  unless client_id && client_key && tenant_id
    raise ArgumentError, "client_id, client_key, and tenant_id must all be specified"
  end

  # Then set the remaining options automatically
  options.each { |key, value| send("#{key}=", value) }

  if user_token && user_token_expiration
    set_token(user_token, user_token_expiration)
  elsif user_token || user_token_expiration
    raise ArgumentError, "token and token_expiration must be both specified"
  end
end
retrieve_token(configuration) click to toggle source

Retrieve the cached token for a configuration. Return both the token and its expiration date, or nil if not cached

# File lib/azure/armrest/configuration.rb, line 16
def self.retrieve_token(configuration)
  token_cache[configuration.hash]
end
token_cache() click to toggle source

Used to store unique token information.

# File lib/azure/armrest/configuration.rb, line 10
def self.token_cache
  @token_cache ||= Hash.new { |h, k| h[k] = [] }
end

Public Instance Methods

eql?(other) click to toggle source
# File lib/azure/armrest/configuration.rb, line 164
def eql?(other)
  return true if equal?(other)
  return false unless self.class == other.class
  tenant_id == other.tenant_id && client_id == other.client_id && client_key == other.client_key
end
hash() click to toggle source
# File lib/azure/armrest/configuration.rb, line 142
def hash
  [environment.name, tenant_id, client_id, client_key].join('_').hash
end
provider_default_api_version(provider, service) click to toggle source

Return the default api version for the given provider and service

# File lib/azure/armrest/configuration.rb, line 195
def provider_default_api_version(provider, service)
  if @provider_api_versions
    @provider_api_versions[provider.downcase][service.downcase]
  else
    nil # Typically only for the fetch_providers method.
  end
end
proxy=(value) click to toggle source

Allow for strings or URI objects when assigning a proxy.

# File lib/azure/armrest/configuration.rb, line 148
def proxy=(value)
  @proxy = value ? value.to_s : value
end
set_token(token, token_expiration) click to toggle source

Set the token value and expiration time.

# File lib/azure/armrest/configuration.rb, line 180
def set_token(token, token_expiration)
  validate_token_time(token_expiration)

  @token, @token_expiration = token, token_expiration.utc
  self.class.cache_token(self)
end
subscription_id=(value) click to toggle source

Set the subscription ID, and validate the value. This also sets provider information.

# File lib/azure/armrest/configuration.rb, line 155
def subscription_id=(value)
  @subscription_id = value
  return if value.nil? || value.empty?
  validate_subscription
  @providers = fetch_providers
  set_provider_api_versions
  value
end
subscriptions() click to toggle source

Returns a list of subscriptions for the current configuration object.

# File lib/azure/armrest/configuration.rb, line 224
def subscriptions
  Azure::Armrest::SubscriptionService.new(self).list
end
token() click to toggle source

Returns the token for the current cache key, or sets it if it does not exist or it has expired.

# File lib/azure/armrest/configuration.rb, line 173
def token
  ensure_token
  @token
end
token_expiration() click to toggle source

Returns the expiration datetime of the current token

# File lib/azure/armrest/configuration.rb, line 189
def token_expiration
  ensure_token
  @token_expiration
end

Private Instance Methods

ensure_token() click to toggle source
# File lib/azure/armrest/configuration.rb, line 252
def ensure_token
  @token, @token_expiration = self.class.retrieve_token(self) if @token.nil?
  fetch_token if @token.nil? || Time.now.utc > @token_expiration
end
fetch_providers() click to toggle source
# File lib/azure/armrest/configuration.rb, line 292
def fetch_providers
  Azure::Armrest::ResourceProviderService.new(self).list
end
fetch_token() click to toggle source
# File lib/azure/armrest/configuration.rb, line 296
def fetch_token
  token_url = File.join(environment.active_directory_authority, tenant_id, 'oauth2', 'token')

  response = JSON.parse(
    ArmrestService.send(
      :rest_post,
      :url         => token_url,
      :proxy       => proxy,
      :ssl_version => ssl_version,
      :ssl_verify  => ssl_verify,
      :timeout     => timeout,
      :payload     => {
        :grant_type    => grant_type,
        :client_id     => client_id,
        :client_secret => client_key,
        :resource      => environment.active_directory_resource_id
      }
    )
  )

  @token = 'Bearer ' + response['access_token']
  @token_expiration = Time.now.utc + response['expires_in'].to_i

  self.class.cache_token(self)
end
set_provider_api_versions() click to toggle source

Build a one-time lookup hash that sets the appropriate api-version string for a given provider and resource type. If possible, select a non-preview version that is not set in the future. Otherwise, just just the most recent one.

# File lib/azure/armrest/configuration.rb, line 270
def set_provider_api_versions
  # A lookup table for getting api-version strings per provider and service.
  @provider_api_versions = Hash.new { |hash, key| hash[key] = {} }

  providers.each do |rp|
    rp.resource_types.each do |rt|
      if rt.api_versions.any? { |v| v !~ /preview/i && Time.parse(v).utc <= Time.now.utc }
        api_version = rt.api_versions.reject do |version|
          version =~ /preview/i || Time.parse(version).utc > Time.now.utc
        end.first
      else
        api_version = rt.api_versions.first
      end

      namespace     = rp['namespace'].downcase # Avoid name collision
      resource_type = rt.resource_type.downcase

      @provider_api_versions[namespace][resource_type] = api_version
    end
  end
end
validate_subscription() click to toggle source

Validate the subscription ID for the given credentials. Returns the subscription ID if valid.

If the subscription ID that was provided in the constructor cannot be found within the list of valid subscriptions, then an error is raised.

If the subscription ID that was provided is found but disabled then a warning will be issued, but no error will be raised.

# File lib/azure/armrest/configuration.rb, line 240
def validate_subscription
  found = subscriptions.find { |sub| sub.subscription_id == subscription_id }

  unless found
    raise ArgumentError, "Subscription ID '#{subscription_id}' not found"
  end

  if found.state.casecmp('enabled') != 0
    warn "Subscription '#{found.subscription_id}' found but not enabled."
  end
end
validate_token_time(time) click to toggle source

Don’t allow tokens from the past to be set.

# File lib/azure/armrest/configuration.rb, line 259
def validate_token_time(time)
  if time.utc < Time.now.utc
    raise ArgumentError, 'token_expiration date invalid'
  end
end