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.



87
88
89
90
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
# File 'lib/azure/armrest/configuration.rb', line 87

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',
  }.merge(args.symbolize_keys)

  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

  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

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

  @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’



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

def accept
  @accept
end

#api_versionObject

The api-version string



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

def api_version
  @api_version
end

#client_idObject

The client ID used to gather token information.



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

def client_id
  @client_id
end

#client_keyObject

The client key used to gather token information.



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

def client_key
  @client_key
end

#content_typeObject

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



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

def content_type
  @content_type
end

#grant_typeObject

The grant type. The default is client_credentials.



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

def grant_type
  @grant_type
end

#providersObject (readonly)

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



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

def providers
  @providers
end

#proxyObject

Proxy to be used for all http requests.



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

def proxy
  @proxy
end

#resource_groupObject

The resource group used for http requests.



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

def resource_group
  @resource_group
end

#ssl_verifyObject

SSL verify mode for all http requests.



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

def ssl_verify
  @ssl_verify
end

#ssl_versionObject

SSL version to be used for all http requests.



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

def ssl_version
  @ssl_version
end

#subscription_idObject

The subscription ID used for each http request.



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

def subscription_id
  @subscription_id
end

#tenant_idObject

The tenant ID used to gather token information.



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

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)


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

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
# 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] = [] }
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.



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

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.



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

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



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

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

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/azure/armrest/configuration.rb', line 126

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



122
123
124
# File 'lib/azure/armrest/configuration.rb', line 122

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



157
158
159
# File 'lib/azure/armrest/configuration.rb', line 157

def provider_default_api_version(provider, service)
  @provider_api_versions[provider.downcase][service.downcase]
end

#set_token(token, token_expiration) ⇒ Object

Set the token value and expiration time.



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

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.



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

def token
  ensure_token
  @token
end

#token_expirationObject

Returns the expiration datetime of the current token



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

def token_expiration
  ensure_token
  @token_expiration
end