Class: Train::Transports::Azure::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/train/transports/azure.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/train/transports/azure.rb', line 24

def initialize(options)
  @apis = {}

  # Override for any cli options
  # azure://subscription_id
  options[:subscription_id] = options[:host] || options[:subscription_id]
  super(options)

  @cache_enabled[:api_call] = true
  @cache[:api_call] = {}

  if @options[:client_secret].nil? && @options[:client_id].nil?
    parse_credentials_file
  end
  connect
end

Instance Method Details

#azure_client(klass = ::Azure::Resources::Profiles::Latest::Mgmt::Client) ⇒ Object



45
46
47
48
49
# File 'lib/train/transports/azure.rb', line 45

def azure_client(klass = ::Azure::Resources::Profiles::Latest::Mgmt::Client)
  return klass.new(@credentials) unless cache_enabled?(:api_call)

  @cache[:api_call][klass.to_s.to_sym] ||= klass.new(@credentials)
end

#connectObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/train/transports/azure.rb', line 51

def connect
  provider = ::MsRestAzure::ApplicationTokenProvider.new(
    @options[:tenant_id],
    @options[:client_id],
    @options[:client_secret],
  )

  @credentials = {
    credentials: ::MsRest::TokenCredentials.new(provider),
    subscription_id: @options[:subscription_id],
    tenant_id: @options[:tenant_id],
    client_id: @options[:client_id],
    client_secret: @options[:client_secret],
  }
end

#get_api_version(resource_type, options) ⇒ Object

Returns the api version for the specified resource type

If an api version has been specified in the options then the apis version table is updated with that value and it is returned

However if it is not specified, or multiple types are being interrogated then this method will interrogate Azure for each of the types versions and pick the latest one. This is added to the apis table so that it can be retrieved quickly again of another one of those resources is encountered again in the resource collection.



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
# File 'lib/train/transports/azure.rb', line 89

def get_api_version(resource_type, options)
  # if an api version has been set in the options, add to the apis hashtable with
  # the resource type
  if options[:apiversion]
    @apis[resource_type] = options[:apiversion]
  else
    # only attempt to get the api version from Azure if the resource type
    # is not present in the apis hashtable
    unless @apis.key?(resource_type)

      # determine the namespace for the resource type
      namespace, type = resource_type.split(%r{/})

      client = azure_client(::Azure::Resources::Profiles::Latest::Mgmt::Client)
      provider = client.providers.get(namespace)

      # get the latest API version for the type
      # assuming that this is the first one in the list
      api_versions = (provider.resource_types.find { |v| v.resource_type == type }).api_versions
      @apis[resource_type] = api_versions[0]
    end
  end

  # return the api version for the type
  @apis[resource_type]
end

#platformObject



41
42
43
# File 'lib/train/transports/azure.rb', line 41

def platform
  direct_platform('azure')
end

#uriObject



67
68
69
# File 'lib/train/transports/azure.rb', line 67

def uri
  "azure://#{@options[:subscription_id]}"
end