Class: Azure::Armrest::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/armrest/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Configuration

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.



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

Instance Attribute Details

#acceptObject

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



51
52
53
# File 'lib/azure/armrest/configuration.rb', line 51

def accept
  @accept
end

#api_versionObject

The api-version string



27
28
29
# File 'lib/azure/armrest/configuration.rb', line 27

def api_version
  @api_version
end

#client_idObject

The client ID used to gather token information.



30
31
32
# File 'lib/azure/armrest/configuration.rb', line 30

def client_id
  @client_id
end

#client_keyObject

The client key used to gather token information.



33
34
35
# File 'lib/azure/armrest/configuration.rb', line 33

def client_key
  @client_key
end

#content_typeObject

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



48
49
50
# File 'lib/azure/armrest/configuration.rb', line 48

def content_type
  @content_type
end

#environmentObject

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



73
74
75
# File 'lib/azure/armrest/configuration.rb', line 73

def environment
  @environment
end

#grant_typeObject

The grant type. The default is client_credentials.



45
46
47
# File 'lib/azure/armrest/configuration.rb', line 45

def grant_type
  @grant_type
end

#max_retriesObject

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



77
78
79
# File 'lib/azure/armrest/configuration.rb', line 77

def max_retries
  @max_retries
end

#max_threadsObject

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



69
70
71
# File 'lib/azure/armrest/configuration.rb', line 69

def max_threads
  @max_threads
end

#providersObject (readonly)

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



66
67
68
# File 'lib/azure/armrest/configuration.rb', line 66

def providers
  @providers
end

#proxyObject

Proxy to be used for all http requests.



54
55
56
# File 'lib/azure/armrest/configuration.rb', line 54

def proxy
  @proxy
end

#resource_groupObject

The resource group used for http requests.



42
43
44
# File 'lib/azure/armrest/configuration.rb', line 42

def resource_group
  @resource_group
end

#ssl_verifyObject

SSL verify mode for all http requests.



60
61
62
# File 'lib/azure/armrest/configuration.rb', line 60

def ssl_verify
  @ssl_verify
end

#ssl_versionObject

SSL version to be used for all http requests.



57
58
59
# File 'lib/azure/armrest/configuration.rb', line 57

def ssl_version
  @ssl_version
end

#subscription_idObject

The subscription ID used for each http request.



39
40
41
# File 'lib/azure/armrest/configuration.rb', line 39

def subscription_id
  @subscription_id
end

#tenant_idObject

The tenant ID used to gather token information.



36
37
38
# File 'lib/azure/armrest/configuration.rb', line 36

def tenant_id
  @tenant_id
end

#timeoutObject

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



63
64
65
# File 'lib/azure/armrest/configuration.rb', line 63

def timeout
  @timeout
end

Class Method Details

.cache_token(configuration) ⇒ Object

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

Raises:

  • (ArgumentError)


21
22
23
24
# 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_cachesObject

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



5
6
7
# File 'lib/azure/armrest/configuration.rb', line 5

def self.clear_caches
  token_cache.clear
end

.logObject

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



206
207
208
# File 'lib/azure/armrest/configuration.rb', line 206

def self.log
  RestClient.log
end

.log=(output) ⇒ Object

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



213
214
215
216
217
218
219
220
# 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

.retrieve_token(configuration) ⇒ Object

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



16
17
18
# File 'lib/azure/armrest/configuration.rb', line 16

def self.retrieve_token(configuration)
  token_cache[configuration.hash]
end

.token_cacheObject

Used to store unique token information.



10
11
12
# File 'lib/azure/armrest/configuration.rb', line 10

def self.token_cache
  @token_cache ||= Hash.new { |h, k| h[k] = [] }
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


164
165
166
167
168
# 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

#hashObject



142
143
144
# 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) ⇒ Object

Return the default api version for the given provider and service



195
196
197
198
199
200
201
# 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

#set_token(token, token_expiration) ⇒ Object

Set the token value and expiration time.



180
181
182
183
184
185
# 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

#subscriptionsObject

Returns a list of subscriptions for the current configuration object.



224
225
226
# File 'lib/azure/armrest/configuration.rb', line 224

def subscriptions
  Azure::Armrest::SubscriptionService.new(self).list
end

#tokenObject

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



173
174
175
176
# File 'lib/azure/armrest/configuration.rb', line 173

def token
  ensure_token
  @token
end

#token_expirationObject

Returns the expiration datetime of the current token



189
190
191
192
# File 'lib/azure/armrest/configuration.rb', line 189

def token_expiration
  ensure_token
  @token_expiration
end