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 and subscription_id. 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.

Note that while the constructor will fail if invalid credentials are supplied, it does not validate your subscription ID. If you want to validate your subscription ID as well, use the validate_subscription! method.



91
92
93
94
95
96
97
98
99
100
101
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
# File 'lib/azure/armrest/configuration.rb', line 91

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',
    :max_threads  => 10
  }.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)

  options.each { |key, value| send("#{key}=", value) }

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

  # Allows for URI objects or Strings.
  @proxy = @proxy.to_s if @proxy

  validate_subscription

  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

  @providers = fetch_providers
  set_provider_api_versions
end

Instance Attribute Details

#acceptObject

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



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

def accept
  @accept
end

#api_versionObject

The api-version string



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

def api_version
  @api_version
end

#client_idObject

The client ID used to gather token information.



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

def client_id
  @client_id
end

#client_keyObject

The client key used to gather token information.



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

def client_key
  @client_key
end

#content_typeObject

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



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

def content_type
  @content_type
end

#grant_typeObject

The grant type. The default is client_credentials.



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

def grant_type
  @grant_type
end

#max_threadsObject

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



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

def max_threads
  @max_threads
end

#providersObject (readonly)

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



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

def providers
  @providers
end

#proxyObject

Proxy to be used for all http requests.



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

def proxy
  @proxy
end

#resource_groupObject

The resource group used for http requests.



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

def resource_group
  @resource_group
end

#ssl_verifyObject

SSL verify mode for all http requests.



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

def ssl_verify
  @ssl_verify
end

#ssl_versionObject

SSL version to be used for all http requests.



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

def ssl_version
  @ssl_version
end

#subscription_idObject

The subscription ID used for each http request.



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

def subscription_id
  @subscription_id
end

#tenant_idObject

The tenant ID used to gather token information.



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

def tenant_id
  @tenant_id
end

Class Method Details

.cache_token(configuration) ⇒ Object

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

Raises:

  • (ArgumentError)


20
21
22
23
# File 'lib/azure/armrest/configuration.rb', line 20

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
8
9
# File 'lib/azure/armrest/configuration.rb', line 5

def self.clear_caches
  # Used to store unique token information.
  @token_cache = Hash.new { |h, k| h[k] = [] }
  CacheMethod.config.storage.clear
end

.logObject

The name of the file or handle used to log http requests. – We have to do a little extra work here to convert a possible file handle to a file name.



178
179
180
181
# File 'lib/azure/armrest/configuration.rb', line 178

def self.log
  file = RestClient.log.instance_variable_get("@target_file")
  file || RestClient.log
end

.log=(output) ⇒ Object

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



185
186
187
# File 'lib/azure/armrest/configuration.rb', line 185

def self.log=(output)
  RestClient.log = output
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



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

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

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
137
138
# File 'lib/azure/armrest/configuration.rb', line 134

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



130
131
132
# File 'lib/azure/armrest/configuration.rb', line 130

def hash
  [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



165
166
167
168
169
170
171
# File 'lib/azure/armrest/configuration.rb', line 165

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.



150
151
152
153
154
155
# File 'lib/azure/armrest/configuration.rb', line 150

def set_token(token, token_expiration)
  validate_token_time(token_expiration)

  @token, @token_expiration = token, token_expiration.utc
  self.class.cache_token(self)
end

#tokenObject

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



143
144
145
146
# File 'lib/azure/armrest/configuration.rb', line 143

def token
  ensure_token
  @token
end

#token_expirationObject

Returns the expiration datetime of the current token



159
160
161
162
# File 'lib/azure/armrest/configuration.rb', line 159

def token_expiration
  ensure_token
  @token_expiration
end