lib/fog/azurerm/requests/compute/create_virtual_machine.rb in far-gem-0.5.1 vs lib/fog/azurerm/requests/compute/create_virtual_machine.rb in far-gem-0.5.2
- old
+ new
@@ -9,10 +9,17 @@
class Real
def create_virtual_machine(vm_config, async = false)
vm_name = vm_config[:name]
rg_name = vm_config[:resource_group]
+ # In case of updating the VM, we check if the user has passed any value for os_disk_name
+ # If the user has not passed any value, we try to retrieve the value of os_disk_name from the VM
+ # If the VM exists then the os_disk_name is retrieved; else it remains nil
+ os_disk_parameters = get_os_disk_parameters(rg_name, vm_name) if vm_config[:os_disk_name].nil? || vm_config[:os_disk_vhd_uri].nil?
+ vm_config[:os_disk_name] = os_disk_parameters[:os_disk_name] if vm_config[:os_disk_name].nil?
+ vm_config[:os_disk_vhd_uri] = os_disk_parameters[:os_disk_vhd_uri] if vm_config[:os_disk_vhd_uri].nil?
+
msg = "Creating Virtual Machine '#{vm_name}' in Resource Group '#{rg_name}'..."
Fog::Logger.debug msg
vm = Azure::ARM::Compute::Models::VirtualMachine.new
vm.location = vm_config[:location]
@@ -65,11 +72,11 @@
# Arguments unpacking
platform = vm_config[:platform]
vm_name = vm_config[:name]
username = vm_config[:username]
password = vm_config[:password]
- custom_data = vm_config[:custom_data]
+ custom_data = vm_config[:custom_data] unless vm_config[:custom_data].nil?
provision_vm_agent = vm_config[:provision_vm_agent]
enable_automatic_updates = vm_config[:enable_automatic_updates]
disable_password_auth = vm_config[:disable_password_authentication]
ssh_key_path = vm_config[:ssh_key_path]
ssh_key_data = vm_config[:ssh_key_data]
@@ -77,11 +84,11 @@
# Build and return os profile object
os_profile = Azure::ARM::Compute::Models::OSProfile.new
os_profile.computer_name = vm_name
os_profile.admin_username = username
os_profile.admin_password = password
- os_profile.custom_data = Base64.strict_encode64(custom_data.nil? ? WHITE_SPACE : custom_data)
+ os_profile.custom_data = Base64.strict_encode64(custom_data) unless vm_config[:custom_data].nil?
if platform.casecmp(WINDOWS).zero?
os_profile.windows_configuration = get_windows_config(provision_vm_agent, enable_automatic_updates)
else
os_profile.linux_configuration = get_linux_config(disable_password_auth, ssh_key_path, ssh_key_data)
@@ -152,16 +159,18 @@
platform = vm_config[:platform]
resource_group = vm_config[:resource_group]
os_disk_size = vm_config[:os_disk_size]
location = vm_config[:location]
image_ref = vm_config[:image_ref]
+ os_disk_name = vm_config[:os_disk_name]
+ os_disk_vhd_uri = vm_config[:os_disk_vhd_uri]
storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
# Set OS disk VHD path
os_disk = Azure::ARM::Compute::Models::OSDisk.new
vhd = Azure::ARM::Compute::Models::VirtualHardDisk.new
- vhd.uri = get_blob_endpoint(storage_account_name) + "/vhds/#{vm_name}_os_disk.vhd"
+ vhd.uri = os_disk_vhd_uri.nil? ? get_blob_endpoint(storage_account_name) + "/vhds/#{vm_name}_os_disk.vhd" : os_disk_vhd_uri
os_disk.vhd = vhd
if vhd_path.nil? && image_ref.nil?
# Marketplace
storage_profile.image_reference = image_reference(publisher, offer, sku, version)
@@ -178,11 +187,11 @@
image = get_image(image_resource_group, image_name)
storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
storage_profile.image_reference.id = image.id
end
- storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_caching, os_disk_size, platform, vm_name)
+ storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_name, os_disk_caching, os_disk_size, platform, vm_name)
storage_profile
end
def get_managed_vm_storage_profile(vm_config)
# Argument unpacking
@@ -198,10 +207,11 @@
os_disk_caching = vm_config[:os_disk_caching]
os_disk_size = vm_config[:os_disk_size]
platform = vm_config[:platform]
vm_name = vm_config[:name]
image_ref = vm_config[:image_ref]
+ os_disk_name = vm_config[:os_disk_name]
# Build storage profile
storage_profile = Azure::ARM::Compute::Models::StorageProfile.new
os_disk = Azure::ARM::Compute::Models::OSDisk.new
managed_disk = Azure::ARM::Compute::Models::ManagedDiskParameters.new
@@ -224,11 +234,11 @@
image = get_image(image_resource_group, image_name)
storage_profile.image_reference = Azure::ARM::Compute::Models::ImageReference.new
storage_profile.image_reference.id = image.id
end
- storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_caching, os_disk_size, platform, vm_name)
+ storage_profile.os_disk = configure_os_disk_object(os_disk, os_disk_name, os_disk_caching, os_disk_size, platform, vm_name)
storage_profile
end
def image_reference(publisher, offer, sku, version)
image_reference = Azure::ARM::Compute::Models::ImageReference.new
@@ -247,12 +257,13 @@
resource_group: resource_group,
vm_name: vm_name
}
end
- def configure_os_disk_object(os_disk, os_disk_caching, os_disk_size, platform, vm_name)
- os_disk.name = "#{vm_name}_os_disk"
+ def configure_os_disk_object(os_disk, os_disk_name, os_disk_caching, os_disk_size, platform, vm_name)
+ # It will use the os_disk_name provided or it will generate a name for itself if it is nil
+ os_disk.name = os_disk_name.nil? ? "#{vm_name}_os_disk" : os_disk_name
os_disk.os_type = platform
os_disk.disk_size_gb = os_disk_size unless os_disk_size.nil?
os_disk.create_option = Azure::ARM::Compute::Models::DiskCreateOptionTypes::FromImage
os_disk.caching = unless os_disk_caching.nil?
case os_disk_caching
@@ -329,9 +340,26 @@
def delete_storage_account(resource_group)
@storage_service.storage_accounts.delete_storage_account_from_tag(resource_group,
TEMPORARY_STORAGE_ACCOUNT_TAG_KEY,
TEMPORARY_STORAGE_ACCOUNT_TAG_VALUE)
+ end
+
+ def get_os_disk_parameters(resource_group, virtual_machine_name)
+ os_disk_parameters = {}
+
+ begin
+ vm = get_virtual_machine(resource_group, virtual_machine_name, false)
+ rescue
+ return os_disk_parameters
+ end
+
+ unless vm.storage_profile.nil?
+ os_disk_parameters[:os_disk_name] = vm.storage_profile.os_disk.name
+ os_disk_parameters[:os_disk_vhd_uri] = vm.storage_profile.os_disk.vhd.uri unless vm.storage_profile.os_disk.vhd.nil?
+ end
+
+ os_disk_parameters
end
end
# This class provides the mock implementation for unit tests.
class Mock