From ee27759fa4bbf73dcd84f73119e1674e9d264e00 Mon Sep 17 00:00:00 2001 From: Nick Osborn Date: Sun, 13 Oct 2013 16:18:59 +0100 Subject: [PATCH] [vcloud_director] Mocking for vDC storage profiles. --- lib/fog/vcloud_director/compute.rb | 28 ++++++++++----- .../requests/compute/get_vdc.rb | 15 ++++---- .../requests/compute/get_vdc_storage_class.rb | 36 +++++++++++++++++++ .../requests/compute/schema_helper.rb | 16 ++++++--- .../compute/vdc_storage_profile_tests.rb | 28 +++++++++++++++ .../requests/compute/vdc_tests.rb | 4 +-- 6 files changed, 104 insertions(+), 23 deletions(-) create mode 100644 tests/vcloud_director/requests/compute/vdc_storage_profile_tests.rb diff --git a/lib/fog/vcloud_director/compute.rb b/lib/fog/vcloud_director/compute.rb index 32ef3d830..7b711cbd6 100644 --- a/lib/fog/vcloud_director/compute.rb +++ b/lib/fog/vcloud_director/compute.rb @@ -450,6 +450,13 @@ module Fog :name => 'vAppTemplate 1' } }, + :edge_gateways => { + uuid => { + :name => 'MockEdgeGateway', + :networks => [uplink_network_uuid, default_network_uuid], + :vdc => vdc_uuid + } + }, :medias => {}, :networks => { default_network_uuid => { @@ -510,17 +517,20 @@ module Fog :uuid => uuid }, :tasks => {}, - :vdcs => { - vdc_uuid => { - :description => 'vDC for mocking', - :name => 'MockVDC' + :vdc_storage_classes => { + uuid => { + :default => true, + :enabled => true, + :limit => 2 * 1024 * 1024, + :name => 'DefaultMockStorageClass', + :units => 'MB', + :vdc => vdc_uuid, } }, - :edge_gateways => { - uuid => { - :name => 'MockEdgeGateway', - :vdc => vdc_uuid, - :networks => [uplink_network_uuid, default_network_uuid] + :vdcs => { + vdc_uuid => { + :description => 'vDC for mocking', + :name => 'MockVDC' } } } diff --git a/lib/fog/vcloud_director/requests/compute/get_vdc.rb b/lib/fog/vcloud_director/requests/compute/get_vdc.rb index 7f2860e00..155019f90 100644 --- a/lib/fog/vcloud_director/requests/compute/get_vdc.rb +++ b/lib/fog/vcloud_director/requests/compute/get_vdc.rb @@ -136,12 +136,15 @@ module Fog body[:ResourceEntities][:ResourceEntity] = resources if api_version.to_f >= 5.1 - # TODO - #body[:VdcStorageProfiles] = - # {:VdcStorageProfile=> - # [{:type=>"application/vnd.vmware.vcloud.vdcStorageProfile+xml", - # :name=>profile[:name], - # :href=>make_href("vdcStorageProfile/#{profile[:uuid]}")}]} + body[:VdcStorageProfiles] = {} + body[:VdcStorageProfiles][:VdcStorageProfile] = + data[:vdc_storage_classes].select do |id, storage_class| + storage_class[:vdc] == vdc_id + end.map do |id, storage_class| + {:type => 'application/vnd.vmware.vcloud.vdcStorageProfile+xml', + :name => storage_class[:name], + :href => make_href("vdcStorageProfile/#{id}")} + end end response.status = 200 diff --git a/lib/fog/vcloud_director/requests/compute/get_vdc_storage_class.rb b/lib/fog/vcloud_director/requests/compute/get_vdc_storage_class.rb index 8edb0c76e..eaa1c7f2e 100644 --- a/lib/fog/vcloud_director/requests/compute/get_vdc_storage_class.rb +++ b/lib/fog/vcloud_director/requests/compute/get_vdc_storage_class.rb @@ -24,6 +24,42 @@ module Fog ) end end + + class Mock + def get_vdc_storage_class(id) + response = Excon::Response.new + + unless vdc_storage_class = data[:vdc_storage_classes][id] + response.status = 403 + raise Excon::Errors.status_error({:expects => 200}, response) + end + + body = + {:xmlns=>xmlns, + :xmlns_xsi=>xmlns_xsi, + :name=>vdc_storage_class[:name], + :id=>"urn:vcloud:vdcstorageProfile:#{id}", + :type=>"application/vnd.vmware.vcloud.vdcStorageProfile+xml", + :href=>make_href("api/vdcStorageProfile/#{id}"), + :xsi_schemaLocation=>xsi_schema_location, + :Link=> + [{:rel=>"up", + :type=>"application/vnd.vmware.vcloud.vdc+xml", + :href=>make_href("vdc/#{vdc_storage_class[:vdc]}")}, + {:rel=>"down", + :type=>"application/vnd.vmware.vcloud.metadata+xml", + :href=>make_href("vdcStorageProfile/#{id}/metadata")}], + :Enabled=>vdc_storage_class[:enabled].to_s, + :Units=>vdc_storage_class[:units], + :Limit=>vdc_storage_class[:limit].to_s, + :Default=>vdc_storage_class[:default].to_s} + + response.status = 200 + response.headers = {'Content-Type' => "#{body[:type]};version=#{api_version}"} + response.body = body + response + end + end end end end diff --git a/tests/vcloud_director/requests/compute/schema_helper.rb b/tests/vcloud_director/requests/compute/schema_helper.rb index f5f2523d1..7aa669f2a 100644 --- a/tests/vcloud_director/requests/compute/schema_helper.rb +++ b/tests/vcloud_director/requests/compute/schema_helper.rb @@ -218,7 +218,7 @@ class VcloudDirector # Container for references to storage profiles associated with a vDC. VDC_STORAGE_PROFILES_TYPE = { - :VdcStorageProfile => REFERENCE_TYPE + :VdcStorageProfile => [REFERENCE_TYPE] } # Allows you to specify certain capabilities of this virtual machine. @@ -247,8 +247,7 @@ class VcloudDirector :imageType => String, :size => String, :Owner => OWNER_TYPE, - # TODO: - #:VdcStorageProfiles => VDC_STORAGE_PROFILES_TYPE # >= 5.1 + :VdcStorageProfile => REFERENCE_TYPE }) METADATA_TYPE = RESOURCE_TYPE.merge({ @@ -302,8 +301,15 @@ class VcloudDirector :UsedNetworkCount => String, :VmQuota => Fog::Nullable::String, :IsEnabled => Fog::Nullable::String, - # TODO: - #:VdcStorageProfiles => VDC_STORAGE_PROFILES_TYPE # >= 5.1 + :VdcStorageProfiles => VDC_STORAGE_PROFILES_TYPE # >= 5.1 + }) + + # Represents a storage profile in an organization vDC. + VDC_STORAGE_PROFILE_TYPE = ENTITY_TYPE.merge({ + :Enabled => Fog::Nullable::String, + :Units => String, + :Limit => String, + :Default => String }) # Information about an individual operating system. diff --git a/tests/vcloud_director/requests/compute/vdc_storage_profile_tests.rb b/tests/vcloud_director/requests/compute/vdc_storage_profile_tests.rb new file mode 100644 index 000000000..914878073 --- /dev/null +++ b/tests/vcloud_director/requests/compute/vdc_storage_profile_tests.rb @@ -0,0 +1,28 @@ +Shindo.tests('Compute::VcloudDirector | vdc_storage_profile requests', ['vclouddirector']) do + + @service = Fog::Compute::VcloudDirector.new + @org = VcloudDirector::Compute::Helper.current_org(@service) + @vdc_id = VcloudDirector::Compute::Helper.first_vdc_id(@org) + @vdc = @service.get_vdc(@vdc_id).body + + @vdc[:VdcStorageProfiles][:VdcStorageProfile] = [@vdc[:VdcStorageProfiles][:VdcStorageProfile]] if @vdc[:VdcStorageProfiles][:VdcStorageProfile].is_a?(Hash) + @vdc[:VdcStorageProfiles][:VdcStorageProfile].each do |storage_profile| + @vdc_storage_profile_id = storage_profile[:href].split('/').last + + tests(storage_profile[:name]) do + tests("#get_vdc_storage_class").data_matches_schema(VcloudDirector::Compute::Schema::VDC_STORAGE_PROFILE_TYPE) do + @service.get_vdc_storage_class(@vdc_storage_profile_id).body + end + + tests('#get_vdc_storage_class_metadata').data_matches_schema(VcloudDirector::Compute::Schema::METADATA_TYPE) do + pending if Fog.mocking? + @service.get_vdc_storage_class_metadata(@vdc_storage_profile_id).body + end + end + end + + tests('Retrieve non-existent vDC storage profile').raises(Excon::Errors::Forbidden) do + @service.get_vdc_storage_class('00000000-0000-0000-0000-000000000000') + end + +end diff --git a/tests/vcloud_director/requests/compute/vdc_tests.rb b/tests/vcloud_director/requests/compute/vdc_tests.rb index b385c47af..d471d4543 100644 --- a/tests/vcloud_director/requests/compute/vdc_tests.rb +++ b/tests/vcloud_director/requests/compute/vdc_tests.rb @@ -11,9 +11,7 @@ Shindo.tests('Compute::VcloudDirector | vdc requests', ['vclouddirector']) do vdc = @service.get_vdc(@vdc_id).body vdc[:AvailableNetworks][:Network] = [vdc[:AvailableNetworks][:Network]] if vdc[:AvailableNetworks][:Network].is_a?(Hash) vdc[:ResourceEntities][:ResourceEntity] = [vdc[:ResourceEntities][:ResourceEntity]] if vdc[:ResourceEntities][:ResourceEntity].is_a?(Hash) - if vdc.has_key?(:VdcStorageProfiles) - vdc[:VdcStorageProfiles][:VdcStorageProfile] = [vdc[:VdcStorageProfiles][:VdcStorageProfile]] if vdc[:VdcStorageProfiles][:VdcStorageProfile].is_a?(Hash) - end + vdc[:VdcStorageProfiles][:VdcStorageProfile] = [vdc[:VdcStorageProfiles][:VdcStorageProfile]] if vdc[:VdcStorageProfiles][:VdcStorageProfile].is_a?(Hash) vdc end