Skip to content

Commit

Permalink
Enhance/enable managed disk deletion async (fog#347)
Browse files Browse the repository at this point in the history
Enhance/enable managed disk deletion async
  • Loading branch information
KevinLoiseau authored and bilal-naeem-confiz committed Jun 13, 2018
1 parent 981270c commit 42ca817
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lib/fog/azurerm/docs/compute.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/fog/azurerm/models/compute/managed_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions lib/fog/azurerm/requests/compute/delete_managed_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions test/models/compute/test_managed_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/requests/compute/test_delete_managed_disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 42ca817

Please sign in to comment.