Skip to content

Commit

Permalink
Implement request list_images
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinLoiseau committed Oct 30, 2018
1 parent b7dc131 commit 8ff81af
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/fog/azurerm/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class AzureRM < Fog::Service
request :create_image
request :delete_image
request :get_image
request :list_images

model_path 'fog/azurerm/models/compute'
model :availability_set
Expand Down
53 changes: 53 additions & 0 deletions lib/fog/azurerm/requests/compute/list_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Fog
module Compute
class AzureRM
# Real class for Compute Request
class Real
def list_images(resource_group_name, async = false)
msg = 'Listing all Images'
Fog::Logger.debug msg
begin
if async
response = @compute_mgmt_client.images.list_by_resource_group_async(resource_group_name)
else
images = @compute_mgmt_client.images.list_by_resource_group(resource_group_name)
end
rescue MsRestAzure::AzureOperationError => e
raise_azure_exception(e, msg)
end
if async
response
else
Fog::Logger.debug 'Image listed successfully.'
images
end
end
end

# Mock class for Compute Request
class Mock
def list_images(*)
images = '[{
"id": "/subscriptions/########-####-####-####-############/resourceGroups/TestRG/providers/Microsoft.Compute/images/TestImage",
"name": "TestImage",
"resource_group": "TestRG",
"location": "West US",
"storage_profile": {
"os_disk": {
"os_type": "Linux",
"os_state": "Generalized",
"blob_uri": "https://myblob.blob.core.windows.net/images/testimage.vhd",
"caching": "ReadWrite",
"disk_size_gb": "5"
},
"data_disks": []
},
"provisioning_state": "Succeeded"
}]'
image_mapper = Azure::ARM::Compute::Models::ImageListResult.mapper
compute_client.deserialize(image_mapper, images, 'result.body')
end
end
end
end
end
22 changes: 22 additions & 0 deletions test/api_stub/requests/compute/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@ module Requests
module Compute
# Mock class for Virtual Machine Requests
class Image
def self.list_response(compute_client)
images = '[{
"id": "/subscriptions/########-####-####-####-############/resourceGroups/TestRG/providers/Microsoft.Compute/images/TestImage",
"name": "TestImage",
"resource_group": "TestRG",
"location": "West US",
"storage_profile": {
"os_disk": {
"os_type": "Linux",
"os_state": "Generalized",
"blob_uri": "https://myblob.blob.core.windows.net/images/testimage.vhd",
"caching": "ReadWrite",
"disk_size_gb": "5"
},
"data_disks": []
},
"provisioning_state": "Succeeded"
}]'
image_mapper = Azure::ARM::Compute::Models::ImageListResult.mapper
compute_client.deserialize(image_mapper, images, 'result.body')
end

def self.base_image_params
{
resource_group: 'fog-test-rg',
Expand Down
27 changes: 27 additions & 0 deletions test/requests/compute/test_list_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require File.expand_path '../../test_helper', __dir__
# Test class for List Images
class TestListVirtualMachineSizes < Minitest::Test
def setup
@service = Fog::Compute::AzureRM.new(credentials)
@client = @service.instance_variable_get(:@compute_mgmt_client)
@images = @client.images
end

def test_list_images_success
mocked_response = ApiStub::Requests::Compute::Image.list_response(@client)
@images.stub :list_by_resource_group, mocked_response do
assert_equal mocked_response, @service.list_images('fog-test-rg')
end
async_response = Concurrent::Promise.execute { 10 }
@images.stub :list_by_resource_group_async, async_response do
assert_equal async_response, @service.list_images('fog-test-rg', true)
end
end

def test_list_images_failure
response = proc { raise MsRestAzure::AzureOperationError.new(nil, nil, 'error' => { 'message' => 'mocked exception' }) }
@images.stub :list_by_resource_group, response do
assert_raises(MsRestAzure::AzureOperationError) { @service.list_images('fog-test-rg') }
end
end
end

0 comments on commit 8ff81af

Please sign in to comment.