From 71d269c2ea9405b967587a485a1e8fdb7e921edd Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 8 Oct 2018 15:12:41 +0200 Subject: [PATCH] Enhance request attach disk to take more than one disk --- lib/fog/azurerm/models/compute/server.rb | 6 ++- .../compute/attach_data_disk_to_vm.rb | 39 +++++++++++++------ test/models/compute/test_server.rb | 11 ++++++ .../compute/test_attach_data_disk_to_vm.rb | 11 ++++++ 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/lib/fog/azurerm/models/compute/server.rb b/lib/fog/azurerm/models/compute/server.rb index ce49a9c19..3d9992b38 100644 --- a/lib/fog/azurerm/models/compute/server.rb +++ b/lib/fog/azurerm/models/compute/server.rb @@ -174,7 +174,11 @@ def detach_data_disk(disk_name, async = false) end def attach_managed_disk(disk_name, disk_resource_group, async = false) - response = service.attach_data_disk_to_vm(data_disk_params(disk_name, nil, nil, disk_resource_group), async) + attach_managed_disks(disk_name, disk_resource_group, async) + end + + def attach_managed_disks(disk_names, disk_resource_group, async = false) + response = service.attach_data_disk_to_vm(data_disk_params(disk_names, nil, nil, disk_resource_group), async) async ? create_fog_async_response(response) : merge_attributes(Fog::Compute::AzureRM::Server.parse(response)) end diff --git a/lib/fog/azurerm/requests/compute/attach_data_disk_to_vm.rb b/lib/fog/azurerm/requests/compute/attach_data_disk_to_vm.rb index 78927701f..1369231f8 100644 --- a/lib/fog/azurerm/requests/compute/attach_data_disk_to_vm.rb +++ b/lib/fog/azurerm/requests/compute/attach_data_disk_to_vm.rb @@ -7,26 +7,25 @@ def attach_data_disk_to_vm(disk_params, async) # Variable un-packing for easy access vm_name = disk_params[:vm_name] vm_resource_group = disk_params[:vm_resource_group] - disk_name = disk_params[:disk_name] + disk_names = disk_params[:disk_name] disk_resource_group = disk_params[:disk_resource_group] disk_size = disk_params[:disk_size_gb] storage_account_name = disk_params[:storage_account_name] - msg = "Attaching Data Disk #{disk_name} to Virtual Machine #{vm_name} in Resource Group #{vm_resource_group}" + disk_names = disk_names.is_a?(Array) ? disk_names : [disk_names] + + msg = "Attaching Data Disk #{disk_names.join(', ')} to Virtual Machine #{vm_name} in Resource Group #{vm_resource_group}" Fog::Logger.debug msg vm = get_virtual_machine_instance(vm_resource_group, vm_name) lun = get_logical_unit_number(vm.storage_profile.data_disks) + access_key = get_storage_access_key(vm_resource_group, storage_account_name) if storage_account_name + + data_disks = get_list_data_disks(disk_names, disk_size, lun, storage_account_name, access_key, disk_resource_group) - # Attach data disk to VM - if storage_account_name - # Un-managed data disk - access_key = get_storage_access_key(vm_resource_group, storage_account_name) - data_disk = get_unmanaged_disk_object(disk_name, disk_size, lun, storage_account_name, access_key) - elsif disk_resource_group - # Managed data disk - data_disk = get_data_disk_object(disk_resource_group, disk_name, lun) + data_disks.each do |data_disk| + vm.storage_profile.data_disks.push(data_disk) end - vm.storage_profile.data_disks.push(data_disk) + begin if async response = @compute_mgmt_client.virtual_machines.create_or_update_async(vm_resource_group, vm_name, vm) @@ -43,13 +42,29 @@ def attach_data_disk_to_vm(disk_params, async) if async response else - Fog::Logger.debug "Data Disk #{disk_name} attached to Virtual Machine #{vm_name} successfully." + Fog::Logger.debug "Data Disk #{disk_names.join(', ')} attached to Virtual Machine #{vm_name} successfully." virtual_machine end end private + def get_list_data_disks(disk_names, disk_size, lun, storage_account_name, access_key, disk_resource_group) + data_disks = [] + disk_names.each do |disk_name| + # Attach data disk to VM + if storage_account_name + # Un-managed data disk + data_disk = get_unmanaged_disk_object(disk_name, disk_size, lun, storage_account_name, access_key) + elsif disk_resource_group + # Managed data disk + data_disk = get_data_disk_object(disk_resource_group, disk_name, lun) + end + data_disks += [data_disk] + end + data_disks + end + def get_virtual_machine_instance(resource_group, vm_name) msg = "Getting Virtual Machine #{vm_name} from Resource Group #{resource_group}" Fog::Logger.debug msg diff --git a/test/models/compute/test_server.rb b/test/models/compute/test_server.rb index 2fe36a4ec..37450e1be 100644 --- a/test/models/compute/test_server.rb +++ b/test/models/compute/test_server.rb @@ -234,4 +234,15 @@ def test_detach_managed_disk_response assert_instance_of Fog::AzureRM::AsyncResponse, @server.detach_managed_disk('managed_disk_name', true) end end + + def test_attach_managed_disks_response + response = ApiStub::Models::Compute::Server.create_linux_virtual_machine_response(@compute_client) + @service.stub :attach_data_disk_to_vm, response do + assert_instance_of Fog::Compute::AzureRM::Server, @server.attach_managed_disks(%w(managed_disk_name1 managed_disk_name2), 'resoure_group') + end + async_response = Concurrent::Promise.execute { 10 } + @service.stub :attach_data_disk_to_vm, async_response do + assert_instance_of Fog::AzureRM::AsyncResponse, @server.attach_managed_disks(%w(managed_disk_name1 managed_disk_name2), 'resoure_group', true) + end + end end diff --git a/test/requests/compute/test_attach_data_disk_to_vm.rb b/test/requests/compute/test_attach_data_disk_to_vm.rb index f2f417454..46f3153bf 100644 --- a/test/requests/compute/test_attach_data_disk_to_vm.rb +++ b/test/requests/compute/test_attach_data_disk_to_vm.rb @@ -102,4 +102,15 @@ def test_get_storage_key_failure end end end + + def test_detach_multiple_data_disks_from_vm_success + @virtual_machines.stub :get, @get_vm_response do + @disks.stub :get, @get_managed_disk_response do + @virtual_machines.stub :create_or_update, @get_vm_managed_disk_response do + input_params = { vm_name: 'fog-test-vm', vm_resource_group: 'fog-test-rg', disk_name: %w(mydatadisk1 mydatadisk2), disk_size_gb: nil, storage_account_name: nil } + assert_equal @service.attach_data_disk_to_vm(input_params, false), @get_vm_managed_disk_response + end + end + end + end end