From 42ca817a35f6a64603ca0a7e27feaf4ad4605f5a Mon Sep 17 00:00:00 2001 From: Kevin Loiseau Date: Wed, 13 Jun 2018 12:39:44 +0200 Subject: [PATCH] Enhance/enable managed disk deletion async (#347) Enhance/enable managed disk deletion async --- lib/fog/azurerm/docs/compute.md | 2 ++ lib/fog/azurerm/models/compute/managed_disk.rb | 11 +++++++++-- .../requests/compute/delete_managed_disk.rb | 16 ++++++++++++---- test/models/compute/test_managed_disk.rb | 11 +++++++++-- .../requests/compute/test_delete_managed_disk.rb | 4 ++-- 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/lib/fog/azurerm/docs/compute.md b/lib/fog/azurerm/docs/compute.md index 5aa2f024d..fe6c542f9 100644 --- a/lib/fog/azurerm/docs/compute.md +++ b/lib/fog/azurerm/docs/compute.md @@ -388,6 +388,8 @@ Get an managed disk object from the get method and then destroy that managed dis ```ruby managed_disk.destroy +# Can be made asynchronously (is synchronous by default) +managed_disk.destroy(true) ``` ## Check Availability Set Existence diff --git a/lib/fog/azurerm/models/compute/managed_disk.rb b/lib/fog/azurerm/models/compute/managed_disk.rb index c11065aa2..e1a3240d3 100644 --- a/lib/fog/azurerm/models/compute/managed_disk.rb +++ b/lib/fog/azurerm/models/compute/managed_disk.rb @@ -46,8 +46,10 @@ def save merge_attributes(Fog::Compute::AzureRM::ManagedDisk.parse(disk)) end - def destroy - service.delete_managed_disk(resource_group_name, name) + def destroy(async = false) + response = service.delete_managed_disk(resource_group_name, name, + async) + async ? create_fog_async_response(response) : response end private @@ -71,6 +73,11 @@ def managed_disk_params encryption_settings: encryption_settings } end + + def create_fog_async_response(response, delete_extra_resource = false) + disk = Fog::Compute::AzureRM::ManagedDisk.new(service: service) + Fog::AzureRM::AsyncResponse.new(disk, response, delete_extra_resource) + end end end end diff --git a/lib/fog/azurerm/requests/compute/delete_managed_disk.rb b/lib/fog/azurerm/requests/compute/delete_managed_disk.rb index ab6cfa9e3..88ce42eb9 100644 --- a/lib/fog/azurerm/requests/compute/delete_managed_disk.rb +++ b/lib/fog/azurerm/requests/compute/delete_managed_disk.rb @@ -4,16 +4,24 @@ module Compute class AzureRM # Real class for Compute Request class Real - def delete_managed_disk(resource_group_name, disk_name) + def delete_managed_disk(resource_group_name, disk_name, async) msg = "Deleting Managed Disk: #{disk_name}" Fog::Logger.debug msg begin - @compute_mgmt_client.disks.delete(resource_group_name, disk_name) + if async + response = @compute_mgmt_client.disks.delete_async(resource_group_name, disk_name) + else + @compute_mgmt_client.disks.delete(resource_group_name, disk_name) + end rescue MsRestAzure::AzureOperationError => e raise_azure_exception(e, msg) end - Fog::Logger.debug "Managed Disk #{disk_name} deleted successfully." - true + if async + response + else + Fog::Logger.debug "Managed Disk #{disk_name} deleted successfully." + true + end end end diff --git a/test/models/compute/test_managed_disk.rb b/test/models/compute/test_managed_disk.rb index db9da2c25..390179bd3 100644 --- a/test/models/compute/test_managed_disk.rb +++ b/test/models/compute/test_managed_disk.rb @@ -50,13 +50,20 @@ def test_save_method_response def test_destroy_method_true_response @service.stub :delete_managed_disk, true do - assert @managed_disk.destroy + assert @managed_disk.destroy(false) end end def test_destroy_method_false_response @service.stub :delete_managed_disk, false do - assert !@managed_disk.destroy + assert !@managed_disk.destroy(false) + end + end + + def test_destroy_method_can_take_params_async + async_response = Concurrent::Promise.execute { 10 } + @service.stub :delete_managed_disk, async_response do + assert_instance_of Fog::AzureRM::AsyncResponse, @managed_disk.destroy(true) end end end diff --git a/test/requests/compute/test_delete_managed_disk.rb b/test/requests/compute/test_delete_managed_disk.rb index a11490b5d..ed1e9ebbd 100644 --- a/test/requests/compute/test_delete_managed_disk.rb +++ b/test/requests/compute/test_delete_managed_disk.rb @@ -10,14 +10,14 @@ def setup def test_delete_managed_disk_success @managed_disks.stub :delete, true do - assert @service.delete_managed_disk('fog-test-rg', 'test-disk') + assert @service.delete_managed_disk('fog-test-rg', 'test-disk', false) end end def test_delete_managed_disk_failure response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) } @managed_disks.stub :delete, response do - assert_raises(MsRestAzure::AzureOperationError) { @service.delete_managed_disk('fog-test-rg', 'test-disk') } + assert_raises(MsRestAzure::AzureOperationError) { @service.delete_managed_disk('fog-test-rg', 'test-disk', false) } end end end