Class: Train::Transports::Azure::Connection
- Inherits:
-
BaseConnection
- Object
- BaseConnection
- Train::Transports::Azure::Connection
- Defined in:
- lib/train/transports/azure.rb
Constant Summary collapse
- DEFAULT_FILE =
::File.join(Dir.home, '.azure', 'credentials')
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #azure_client(klass = ::Azure::Resources::Profiles::Latest::Mgmt::Client) ⇒ Object
- #connect ⇒ Object
-
#get_api_version(resource_type, options) ⇒ Object
Returns the api version for the specified resource type.
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #msi_auth? ⇒ Boolean
- #platform ⇒ Object
- #unique_identifier ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/train/transports/azure.rb', line 33 def initialize() @apis = {} # Override for any cli options # azure://subscription_id [:subscription_id] = [:host] || [:subscription_id] super() @cache_enabled[:api_call] = true @cache[:api_call] = {} if [:client_secret].nil? && [:client_id].nil? [:credentials_file] = DEFAULT_FILE if [:credentials_file].nil? .merge!(Helpers::Azure::FileCredentials.parse()) end [:msi_port] = [:msi_port].to_i unless [:msi_port].nil? # additional platform details release = Gem.loaded_specs['azure_mgmt_resources'].version @platform_details = { release: "azure_mgmt_resources-v#{release}" } connect end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
29 30 31 |
# File 'lib/train/transports/azure.rb', line 29 def end |
Instance Method Details
#azure_client(klass = ::Azure::Resources::Profiles::Latest::Mgmt::Client) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/train/transports/azure.rb', line 62 def azure_client(klass = ::Azure::Resources::Profiles::Latest::Mgmt::Client) if cache_enabled?(:api_call) return @cache[:api_call][klass.to_s.to_sym] unless @cache[:api_call][klass.to_s.to_sym].nil? end if klass == ::Azure::Resources::Profiles::Latest::Mgmt::Client @credentials[:base_url] = MsRestAzure::AzureEnvironments::AzureCloud.resource_manager_endpoint_url elsif klass == ::Azure::GraphRbac::Profiles::Latest::Client client = GraphRbac.client(@credentials) end client ||= klass.new(@credentials) # Cache if enabled @cache[:api_call][klass.to_s.to_sym] ||= client if cache_enabled?(:api_call) client end |
#connect ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/train/transports/azure.rb', line 81 def connect if msi_auth? # this needs set for azure cloud to authenticate ENV['MSI_VM'] = 'true' provider = ::MsRestAzure::MSITokenProvider.new([:msi_port]) else provider = ::MsRestAzure::ApplicationTokenProvider.new( [:tenant_id], [:client_id], [:client_secret], ) end @credentials = { credentials: ::MsRest::TokenCredentials.new(provider), subscription_id: [:subscription_id], tenant_id: [:tenant_id], } @credentials[:client_id] = [:client_id] unless [:client_id].nil? @credentials[:client_secret] = [:client_secret] unless [:client_secret].nil? 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.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/train/transports/azure.rb', line 125 def get_api_version(resource_type, ) # if an api version has been set in the options, add to the apis hashtable with # the resource type if [:apiversion] @apis[resource_type] = [: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 |
#msi_auth? ⇒ Boolean
156 157 158 |
# File 'lib/train/transports/azure.rb', line 156 def msi_auth? [:client_id].nil? && [:client_secret].nil? && port_open?([:msi_port]) end |
#platform ⇒ Object
58 59 60 |
# File 'lib/train/transports/azure.rb', line 58 def platform force_platform!('azure', @platform_details) end |
#unique_identifier ⇒ Object
152 153 154 |
# File 'lib/train/transports/azure.rb', line 152 def unique_identifier [:subscription_id] || [:tenant_id] end |
#uri ⇒ Object
103 104 105 |
# File 'lib/train/transports/azure.rb', line 103 def uri "azure://#{@options[:subscription_id]}" end |