Class: ForemanAzureRm::AzureSdkAdapter
- Inherits:
-
Object
- Object
- ForemanAzureRm::AzureSdkAdapter
- Defined in:
- app/lib/foreman_azure_rm/azure_sdk_adapter.rb
Class Method Summary collapse
Instance Method Summary collapse
- #actual_gallery_image_id(rg_name, image_id) ⇒ Object
-
#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.
- #azure_credentials(base_url) ⇒ Object
- #check_vm_status(rg_name, vm_name) ⇒ Object
- #compute_client ⇒ Object
- #create_or_update_nic(rg_name, nic_name, parameters) ⇒ Object
- #create_or_update_pip(rg_name, pip_name, parameters) ⇒ Object
- #create_or_update_vm(rg_name, vm_name, parameters) ⇒ Object
- #create_or_update_vm_extensions(rg_name, vm_name, vm_extension_name, extension_params) ⇒ Object
- #delete_disk(rg_name, disk_name) ⇒ Object
- #delete_nic(rg_name, nic_name) ⇒ Object
- #delete_pip(rg_name, pip_name) ⇒ Object
- #delete_vm(rg_name, vm_name) ⇒ Object
- #environment_settings(azure_environment) ⇒ Object
- #fetch_gallery_image_id(rg_name, image_id) ⇒ Object
- #get_custom_image(rg_name, image_name) ⇒ Object
- #get_gallery_image(rg_name, gallery_name, gallery_image_name) ⇒ Object
- #get_marketplace_image(location, publisher_name, offer, skus, version) ⇒ Object
- #get_status(virtual_machine) ⇒ Object
-
#get_storage_accts ⇒ Object
rubocop:disable Naming/AccessorMethodName.
- #get_vm(rg_name, vm_name) ⇒ Object
- #get_vm_extension(rg_name, vm_name, vm_extension_name) ⇒ Object
-
#initialize(tenant, app_ident, secret_key, sub_id, azure_environment) ⇒ AzureSdkAdapter
constructor
A new instance of AzureSdkAdapter.
- #list_custom_images ⇒ Object
- #list_galleries ⇒ Object
- #list_gallery_image_versions(rg_name, gallery_name, gallery_image_name) ⇒ Object
- #list_gallery_images(rg_name, gallery_name) ⇒ Object
- #list_regions(subscription_id) ⇒ Object
- #list_resources(filter) ⇒ Object
- #list_versions(location, publisher_name, offer, skus) ⇒ Object
- #list_vm_sizes(region) ⇒ Object
- #list_vms(region) ⇒ Object
- #network_client ⇒ Object
- #public_ip(rg_name, pip_name) ⇒ Object
- #resource_client ⇒ Object
- #rgs ⇒ Object
- #start_vm(rg_name, vm_name) ⇒ Object
- #stop_vm(rg_name, vm_name) ⇒ Object
- #storage_client ⇒ Object
- #subnets(rg_name, vnet_name) ⇒ Object
- #subscription_client ⇒ Object
- #vm_disk(rg_name, disk_name) ⇒ Object
- #vm_nic(rg_name, nic_name) ⇒ Object
- #vnets ⇒ Object
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 'app/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
.gallery_caching(rg_name) ⇒ Object
234 235 236 237 |
# File 'app/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
#actual_gallery_image_id(rg_name, image_id) ⇒ Object
239 240 241 242 243 244 245 |
# File 'app/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) return unless (gallery = gallery_names.first) gallery_image = list_gallery_images(rg_name, gallery).detect { |image| image.name == image_id } gallery_image&.id 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.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 60 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 52 |
# File 'app/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
211 212 213 214 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 211 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_client ⇒ Object
18 19 20 |
# File 'app/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
191 192 193 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 191 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
187 188 189 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 187 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
176 177 178 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 176 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
180 181 182 183 184 185 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 180 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
207 208 209 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 207 def delete_disk(rg_name, disk_name) compute_client.disks.delete(rg_name, disk_name) end |
#delete_nic(rg_name, nic_name) ⇒ Object
199 200 201 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 199 def delete_nic(rg_name, nic_name) network_client.network_interfaces.delete(rg_name, nic_name) end |
#delete_pip(rg_name, pip_name) ⇒ Object
195 196 197 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 195 def delete_pip(rg_name, pip_name) network_client.public_ipaddresses.delete(rg_name, pip_name) end |
#delete_vm(rg_name, vm_name) ⇒ Object
203 204 205 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 203 def delete_vm(rg_name, vm_name) compute_client.virtual_machines.delete(rg_name, vm_name) end |
#environment_settings(azure_environment) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 73 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 |
#fetch_gallery_image_id(rg_name, image_id) ⇒ Object
247 248 249 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 247 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
151 152 153 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 151 def get_custom_image(rg_name, image_name) compute_client.images.get(rg_name, image_name) end |
#get_gallery_image(rg_name, gallery_name, gallery_image_name) ⇒ Object
163 164 165 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 163 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
139 140 141 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 139 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
216 217 218 219 220 221 222 223 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 216 def get_status(virtual_machine) vm_statuses = virtual_machine.instance_view.statuses vm_status = nil vm_statuses.each do |status| vm_status = status.code.split('/')[1] if status.code.include? 'PowerState' end vm_status end |
#get_storage_accts ⇒ Object
rubocop:disable Naming/AccessorMethodName
171 172 173 174 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 171 def get_storage_accts # rubocop:disable Naming/AccessorMethodName result = storage_client.storage_accounts.list result.value end |
#get_vm(rg_name, vm_name) ⇒ Object
135 136 137 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 135 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
119 120 121 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 119 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_images ⇒ Object
147 148 149 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 147 def list_custom_images compute_client.images.list end |
#list_galleries ⇒ Object
155 156 157 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 155 def list_galleries compute_client.galleries.list end |
#list_gallery_image_versions(rg_name, gallery_name, gallery_image_name) ⇒ Object
167 168 169 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 167 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 |
#list_gallery_images(rg_name, gallery_name) ⇒ Object
159 160 161 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 159 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
86 87 88 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 86 def list_regions(subscription_id) subscription_client.subscriptions.list_locations(subscription_id) end |
#list_resources(filter) ⇒ Object
90 91 92 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 90 def list_resources(filter) resource_client.resources.list(filter) end |
#list_versions(location, publisher_name, offer, skus) ⇒ Object
143 144 145 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 143 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
123 124 125 126 127 128 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 123 def list_vm_sizes(region) return [] if region.blank? stripped_region = region.gsub(/\s+/, '').downcase compute_client.virtual_machine_sizes.list(stripped_region).value end |
#list_vms(region) ⇒ Object
130 131 132 133 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 130 def list_vms(region) # List all VMs in a resource group compute_client.virtual_machines.list_by_location(region) end |
#network_client ⇒ Object
22 23 24 |
# File 'app/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
107 108 109 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 107 def public_ip(rg_name, pip_name) network_client.public_ipaddresses.get(rg_name, pip_name) end |
#resource_client ⇒ Object
13 14 15 16 |
# File 'app/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 |
#rgs ⇒ Object
94 95 96 97 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 94 def rgs rgs = resource_client.resource_groups.list rgs.map(&:name) end |
#start_vm(rg_name, vm_name) ⇒ Object
225 226 227 |
# File 'app/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 'app/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_client ⇒ Object
26 27 28 |
# File 'app/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
103 104 105 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 103 def subnets(rg_name, vnet_name) network_client.subnets.list(rg_name, vnet_name) end |
#subscription_client ⇒ Object
30 31 32 |
# File 'app/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
115 116 117 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 115 def vm_disk(rg_name, disk_name) compute_client.disks.get(rg_name, disk_name) end |
#vm_nic(rg_name, nic_name) ⇒ Object
111 112 113 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 111 def vm_nic(rg_name, nic_name) network_client.network_interfaces.get(rg_name, nic_name) end |
#vnets ⇒ Object
99 100 101 |
# File 'app/lib/foreman_azure_rm/azure_sdk_adapter.rb', line 99 def vnets network_client.virtual_networks.list_all end |