Class: ForemanAzureRm::AzureSdkAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_azure_rm/azure_sdk_adapter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tenant, app_ident, secret_key, sub_id, azure_environment) ⇒ AzureSdkAdapter

Returns a new instance of AzureSdkAdapter.



3
4
5
6
7
8
9
10
11
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 3

def initialize(tenant, app_ident, secret_key, sub_id, azure_environment)
  @tenant               = tenant
  @app_ident            = app_ident
  @secret_key           = secret_key
  @sub_id               = sub_id
  @azure_environment    = azure_environment
  @ad_settings          = ad_environment_settings(azure_environment)
  @environment_settings = environment_settings(azure_environment)
end

Class Method Details



234
235
236
237
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 234

def self.gallery_caching(rg_name)
  @gallery_caching ||= {}
  @gallery_caching[rg_name] ||= {}
end

Instance Method Details



239
240
241
242
243
244
245
246
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 239

def actual_gallery_image_id(rg_name, image_id)
  gallery_names = list_galleries.map(&:name)
  gallery_names.each do |gallery|
    gallery_image = list_gallery_images(rg_name, gallery).detect { |image| image.name == image_id }
    return gallery_image&.id
  end
  nil
end

#ad_environment_settings(azure_environment) ⇒ MsRestAzure::ActiveDirectoryServiceSettings

github.com/Azure/azure-sdk-for-ruby/issues/850 Retrieves a [MsRestAzure::ActiveDirectoryServiceSettings] object representing the settings for the given cloud.

Parameters:

  • azure_environment (String)

    The Azure environment to retrieve settings for.

Returns:

  • (MsRestAzure::ActiveDirectoryServiceSettings)

    Settings to be used for subsequent requests



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 59

def ad_environment_settings(azure_environment)
  case azure_environment.downcase
  when 'azureusgovernment'
    MsRestAzure::ActiveDirectoryServiceSettings.get_azure_us_government_settings
  when 'azurechina'
    MsRestAzure::ActiveDirectoryServiceSettings.get_azure_china_settings
  when 'azuregermancloud'
    MsRestAzure::ActiveDirectoryServiceSettings.get_azure_german_settings
  when 'azure'
    MsRestAzure::ActiveDirectoryServiceSettings.get_azure_settings
  end
end

#azure_credentials(base_url) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 34

def azure_credentials(base_url)
  provider = MsRestAzure::ApplicationTokenProvider.new(
  @tenant,
  @app_ident,
  @secret_key,
  @ad_settings)
  
  credentials = MsRest::TokenCredentials.new(provider)

  {
    credentials: credentials,
    tenant_id: @tenant,
    client_id: @app_ident,
    client_secret: @secret_key,
    subscription_id: @sub_id,
    base_url: base_url
  }
end

#check_vm_status(rg_name, vm_name) ⇒ Object



209
210
211
212
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 209

def check_vm_status(rg_name, vm_name)
  virtual_machine = compute_client.virtual_machines.get(rg_name, vm_name, expand:'instanceView')
  get_status(virtual_machine)
end

#compute_clientObject



18
19
20
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 18

def compute_client
  @compute_client ||= Compute::Client.new(azure_credentials(@environment_settings.resource_manager_endpoint_url))
end

#create_or_update_nic(rg_name, nic_name, parameters) ⇒ Object



189
190
191
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 189

def create_or_update_nic(rg_name, nic_name, parameters)
  network_client.network_interfaces.create_or_update(rg_name, nic_name, parameters)
end

#create_or_update_pip(rg_name, pip_name, parameters) ⇒ Object



185
186
187
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 185

def create_or_update_pip(rg_name, pip_name, parameters)
  network_client.public_ipaddresses.create_or_update(rg_name, pip_name, parameters)
end

#create_or_update_vm(rg_name, vm_name, parameters) ⇒ Object



174
175
176
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 174

def create_or_update_vm(rg_name, vm_name, parameters)
  compute_client.virtual_machines.create_or_update(rg_name, vm_name, parameters)
end

#create_or_update_vm_extensions(rg_name, vm_name, vm_extension_name, extension_params) ⇒ Object



178
179
180
181
182
183
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 178

def create_or_update_vm_extensions(rg_name, vm_name, vm_extension_name, extension_params)
  compute_client.virtual_machine_extensions.create_or_update(rg_name,
                                                      vm_name,
                                                      vm_extension_name,
                                                      extension_params) 
end

#delete_disk(rg_name, disk_name) ⇒ Object



205
206
207
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 205

def delete_disk(rg_name, disk_name)
  compute_client.disks.delete(rg_name, disk_name)
end

#delete_nic(rg_name, nic_name) ⇒ Object



197
198
199
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 197

def delete_nic(rg_name, nic_name)
  network_client.network_interfaces.delete(rg_name, nic_name)
end

#delete_pip(rg_name, pip_name) ⇒ Object



193
194
195
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 193

def delete_pip(rg_name, pip_name)
  network_client.public_ipaddresses.delete(rg_name, pip_name)
end

#delete_vm(rg_name, vm_name) ⇒ Object



201
202
203
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 201

def delete_vm(rg_name, vm_name)
  compute_client.virtual_machines.delete(rg_name, vm_name)
end

#environment_settings(azure_environment) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 72

def environment_settings(azure_environment)
  case azure_environment.downcase
  when 'azureusgovernment'
    MsRestAzure::AzureEnvironments::AzureUSGovernment
  when 'azurechina'
    MsRestAzure::AzureEnvironments::AzureChinaCloud
  when 'azuregermancloud'
    MsRestAzure::AzureEnvironments::AzureGermanCloud
  when 'azure'
    MsRestAzure::AzureEnvironments::AzureCloud
  end
end


248
249
250
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 248

def fetch_gallery_image_id(rg_name, image_id)
  AzureSdkAdapter.gallery_caching(rg_name)[image_id] ||= actual_gallery_image_id(rg_name, image_id)
end

#get_custom_image(rg_name, image_name) ⇒ Object



149
150
151
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 149

def get_custom_image(rg_name, image_name)
  compute_client.images.get(rg_name, image_name)
end


161
162
163
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 161

def get_gallery_image(rg_name, gallery_name, gallery_image_name)
  compute_client.gallery_images.get(rg_name, gallery_name, gallery_image_name)
end

#get_marketplace_image(location, publisher_name, offer, skus, version) ⇒ Object



137
138
139
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 137

def get_marketplace_image(location, publisher_name, offer, skus, version)
  compute_client.virtual_machine_images.get(location, publisher_name, offer, skus, version)
end

#get_status(virtual_machine) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 214

def get_status(virtual_machine)
  vm_statuses = virtual_machine.instance_view.statuses
  vm_status = nil
  vm_statuses.each do |status|
    if status.code.include? 'PowerState'
      vm_status = status.code.split('/')[1]
    end
  end
  vm_status
end

#get_storage_acctsObject



169
170
171
172
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 169

def get_storage_accts
  result = storage_client.storage_accounts.list
  result.value
end

#get_vm(rg_name, vm_name) ⇒ Object



133
134
135
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 133

def get_vm(rg_name, vm_name)
  compute_client.virtual_machines.get(rg_name, vm_name)
end

#get_vm_extension(rg_name, vm_name, vm_extension_name) ⇒ Object



118
119
120
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 118

def get_vm_extension(rg_name, vm_name, vm_extension_name)
  compute_client.virtual_machine_extensions.get(rg_name, vm_name, vm_extension_name)
end

#list_custom_imagesObject



145
146
147
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 145

def list_custom_images
  compute_client.images.list
end

#list_galleriesObject



153
154
155
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 153

def list_galleries
  compute_client.galleries.list
end


165
166
167
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 165

def list_gallery_image_versions(rg_name, gallery_name, gallery_image_name)
  compute_client.gallery_image_versions.list_by_gallery_image(rg_name, gallery_name, gallery_image_name)
end


157
158
159
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 157

def list_gallery_images(rg_name, gallery_name)
  compute_client.gallery_images.list_by_gallery(rg_name, gallery_name)
end

#list_regions(subscription_id) ⇒ Object



85
86
87
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 85

def list_regions(subscription_id)
  subscription_client.subscriptions.list_locations(subscription_id)
end

#list_resources(filter) ⇒ Object



89
90
91
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 89

def list_resources(filter)
  resource_client.resources.list(filter)
end

#list_versions(location, publisher_name, offer, skus) ⇒ Object



141
142
143
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 141

def list_versions(location, publisher_name, offer, skus)
  compute_client.virtual_machine_images.list(location, publisher_name, offer, skus)
end

#list_vm_sizes(region) ⇒ Object



122
123
124
125
126
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 122

def list_vm_sizes(region)
  return [] unless region.present?
  stripped_region = region.gsub(/\s+/, '').downcase
  compute_client.virtual_machine_sizes.list(stripped_region).value()
end

#list_vms(region) ⇒ Object



128
129
130
131
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 128

def list_vms(region)
  # List all VMs in a resource group
  virtual_machines = compute_client.virtual_machines.list_by_location(region)
end

#network_clientObject



22
23
24
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 22

def network_client
  @network_client ||= Network::Client.new(azure_credentials(@environment_settings.resource_manager_endpoint_url))
end

#public_ip(rg_name, pip_name) ⇒ Object



106
107
108
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 106

def public_ip(rg_name, pip_name)
  network_client.public_ipaddresses.get(rg_name, pip_name)
end

#resource_clientObject



13
14
15
16
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 13

def resource_client
  #resource_manager_endpoint_url
  @resource_client ||= Resources::Client.new(azure_credentials(@environment_settings.resource_manager_endpoint_url))
end

#rgsObject



93
94
95
96
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 93

def rgs
  rgs      = resource_client.resource_groups.list
  rgs.map(&:name)
end

#start_vm(rg_name, vm_name) ⇒ Object



225
226
227
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 225

def start_vm(rg_name, vm_name)
  compute_client.virtual_machines.start(rg_name, vm_name)
end

#stop_vm(rg_name, vm_name) ⇒ Object



229
230
231
232
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 229

def stop_vm(rg_name, vm_name)
  compute_client.virtual_machines.power_off(rg_name, vm_name)
  compute_client.virtual_machines.deallocate(rg_name, vm_name)
end

#storage_clientObject



26
27
28
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 26

def storage_client
  @storage_client ||= Storage::Client.new(azure_credentials(@environment_settings.resource_manager_endpoint_url))
end

#subnets(rg_name, vnet_name) ⇒ Object



102
103
104
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 102

def subnets(rg_name, vnet_name)
  network_client.subnets.list(rg_name, vnet_name)
end

#subscription_clientObject



30
31
32
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 30

def subscription_client
  @subscription_client ||= Subscriptions::Client.new(azure_credentials(@environment_settings.resource_manager_endpoint_url))
end

#vm_disk(rg_name, disk_name) ⇒ Object



114
115
116
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 114

def vm_disk(rg_name, disk_name)
  compute_client.disks.get(rg_name, disk_name)
end

#vm_nic(rg_name, nic_name) ⇒ Object



110
111
112
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 110

def vm_nic(rg_name,nic_name)
  network_client.network_interfaces.get(rg_name, nic_name)
end

#vnetsObject



98
99
100
# File 'lib/foreman_azure_rm/azure_sdk_adapter.rb', line 98

def vnets
  network_client.virtual_networks.list_all
end