1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[vcloud_director] Start mocking requests.

- also adds get_current_session request
This commit is contained in:
Nick Osborn 2013-09-26 11:29:51 +01:00
parent 36277525a4
commit 1f9837c865
10 changed files with 556 additions and 14 deletions

View file

@ -106,6 +106,7 @@ module Fog
request :post_task_cancel
request :post_vapp_undeploy
request :delete_vapp
request :get_current_session
class Model < Fog::Model
def initialize(attrs={})
@ -296,12 +297,101 @@ module Fog
@auth_token = response.headers['Set-Cookie'] || response.headers['set-cookie']
@org_name = response.body[:org]
end
end
class Mock
end
attr_reader :end_point, :api_version
def data
@@data ||= Hash.new do |hash, key|
hash[key] = {
:catalog => {
:name => 'Default Catalog',
:uuid => uuid
},
:catalog_items => [{
:type => 'vAppTemplate',
:name => 'vAppTemplate 1',
:uuid => uuid
}],
:networks => [{
:name => 'Default Network',
:uuid => uuid
}],
:org => {
:description => 'Organization for mocking',
:full_name => 'Mock Organization',
:name => org_name,
:uuid => uuid
},
:vdc => {
:description => 'vDC for mocking',
:name => 'MockVDC',
:uuid => uuid
}
}
end[@vcloud_director_username]
end
def initialize(options={})
@vcloud_director_password = options[:vcloud_director_password]
@vcloud_director_username = options[:vcloud_director_username]
#@connection_options = options[:connection_options] || {}
@host = options[:vcloud_director_host]
@path = options[:path] || Fog::Compute::VcloudDirector::Defaults::PATH
@persistent = options[:persistent] || false
@port = options[:port] || Fog::Compute::VcloudDirector::Defaults::PORT
@scheme = options[:scheme] || Fog::Compute::VcloudDirector::Defaults::SCHEME
#@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
@end_point = "#{@scheme}://#{@host}#{@path}/"
@api_version = options[:vcloud_director_api_version] || Fog::Compute::VcloudDirector::Defaults::API_VERSION
end
def auth_token
@auth_token || Fog::Mock.random_base64(32)
end
def org_name
@org_name ||=
begin
username = @vcloud_director_username.split('@')
unless username.size == 2
Fog::Logger.warning('vcloud_director_username should be in the form user@org_name')
end
username.last || 'vcd_org_name'
end
end
def user_name
@user_name ||= @vcloud_director_username.split('@').first
end
def uuid
[8,4,4,4,12].map {|i| Fog::Mock.random_hex(i)}.join('-')
end
def add_id_from_href!(data={})
data[:id] = data[:href].split('/').last
end
private
def make_href(path)
"#{@end_point}#{path}"
end
def xmlns
'http://www.vmware.com/vcloud/v1.5'
end
def xmlns_xsi
'http://www.w3.org/2001/XMLSchema-instance'
end
def xsi_schema_location
"http://www.vmware.com/vcloud/v1.5 http://#{@host}#{@path}/v1.5/schema/master.xsd"
end
end
end
end
end

View file

@ -0,0 +1,75 @@
module Fog
module Compute
class VcloudDirector
class Real
# Retrieve a representation of the current session.
#
# A Session object contains one or more URLs from which you can begin
# browsing, as well as links to the query service and entity resolver.
# The list of URLs in the Session object is based on the role and
# privileges of the authenticated user.
#
# A Session object expires after a configurable interval of client
# inactivity.
#
# === Returns
# * response<~Excon::Response>:
# * body<~Hash>:
#
# {vCloud API Documentation}[http://pubs.vmware.com/vcd-51/topic/com.vmware.vcloud.api.reference.doc_51/doc/operations/GET-CurrentSession.html]
def get_current_session
request(
:expects => 200,
:idempotent => true,
:method => 'GET',
:parser => Fog::ToHashDocument.new,
:path => 'session'
)
end
end
class Mock
def get_current_session
body =
{:xmlns => xmlns,
:xmlns_xsi => xmlns_xsi,
:user => user_name,
:org => data[:org][:name],
:type => 'application/vnd.vmware.vcloud.session+xml',
:href => make_href('session/'),
:xsi_schemaLocation=> xsi_schema_location,
:Link =>
[{:rel => 'down',
:type => 'application/vnd.vmware.vcloud.orgList+xml',
:href => make_href('org/')},
{:rel => 'down',
:type => 'application/vnd.vmware.admin.vcloud+xml',
:href => make_href('admin/')},
{:rel => 'down',
:type => 'application/vnd.vmware.vcloud.org+xml',
:name => data[:org][:name],
:href => make_href("org/#{data[:org][:uuid]}")},
{:rel => 'down',
:type => 'application/vnd.vmware.vcloud.query.queryList+xml',
:href => make_href('query')},
{:rel => 'entityResolver',
:type => 'application/vnd.vmware.vcloud.entity+xml',
:href => make_href('entity/')}]}
if @api_version.to_f >= 5.1
body[:Link] << {
:rel => 'down:extensibility',
:type => 'application/vnd.vmware.vcloud.apiextensibility+xml',
:href => make_href('extensibility')
}
end
Excon::Response.new(
:body => body,
:headers => {'Content-Type' => "#{body[:type]};version=#{api_version}"},
:status => 200
)
end
end
end
end
end

View file

@ -2,7 +2,6 @@ module Fog
module Compute
class VcloudDirector
class Real
def get_organization(organization_id)
request({
:expects => 200,
@ -11,7 +10,62 @@ module Fog
:path => "org/#{organization_id}"
})
end
end
class Mock
def get_organization(organization_id)
org = data[:org]
body =
{:xmlns=>xmlns,
:xmlns_xsi=>xmlns_xsi,
:name=>org[:name],
:id=>"urn:vcloud:org:#{org[:uuid]}",
:type=>"application/vnd.vmware.vcloud.org+xml",
:href=>make_href("org/#{org[:uuid]}"),
:xsi_schemaLocation=>xsi_schema_location,
:Link=>
[{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.vdc+xml",
:name=>data[:vdc][:name],
:href=>make_href("vdc/#{data[:vdc][:uuid]}")},
{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.tasksList+xml",
:href=>make_href("tasksList/#{org[:uuid]}")},
{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.catalog+xml",
:name=>data[:catalog][:name],
:href=>make_href("catalog/#{data[:catalog][:uuid]}")},
{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.controlAccess+xml",
:href=>
make_href("org/#{org[:uuid]}/catalog/#{data[:catalog][:uuid]}/controlAccess/")},
{:rel=>"controlAccess",
:type=>"application/vnd.vmware.vcloud.controlAccess+xml",
:href=>
make_href("org/#{org[:uuid]}/catalog/#{data[:catalog][:uuid]}/action/controlAccess")},
{:rel=>"add",
:type=>"application/vnd.vmware.admin.catalog+xml",
:href=>make_href("admin/org/#{org[:uuid]}/catalogs")},
{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.metadata+xml",
:href=>make_href("org/#{org[:uuid]}/metadata")}],
:Description=>org[:description]||'',
:FullName=>org[:full_name]}
body[:Link] += data[:networks].map do |network|
{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.orgNetwork+xml",
:name=>network[:name],
:href=>make_href("network/#{network[:uuid]}")}
end
Excon::Response.new(
:body => body,
:headers => {'Content-Type' => "#{body[:type]};version=#{api_version}"},
:status => 200
)
end
end
end
end

View file

@ -2,16 +2,35 @@ module Fog
module Compute
class VcloudDirector
class Real
def get_organizations
request({
:expects => 200,
:method => 'GET',
:parser => Fog::ToHashDocument.new,
:path => "org"
:path => 'org'
})
end
end
class Mock
def get_organizations
body =
{:xmlns=>xmlns,
:xmlns_xsi=>xmlns_xsi,
:type=>"application/vnd.vmware.vcloud.orgList+xml",
:href=>make_href('org/'),
:xsi_schemaLocation=>xsi_schema_location,
:Org=>
{:type=>"application/vnd.vmware.vcloud.org+xml",
:name=>data[:org][:name],
:href=>make_href("org/#{data[:org][:uuid]}")}}
Excon::Response.new(
:body => body,
:headers => {'Content-Type' => "#{body[:type]};version=#{api_version}"},
:status => 200
)
end
end
end
end

View file

@ -2,7 +2,6 @@ module Fog
module Compute
class VcloudDirector
class Real
def get_vdc(vdc_id)
request(
:expects => 200,
@ -11,7 +10,144 @@ module Fog
:path => "vdc/#{vdc_id}"
)
end
end
class Mock
def get_vdc(vdc_id)
vdc = data[:vdc]
raise Excon::Errors::NotFound unless vdc_id == vdc[:uuid]
body =
{:xmlns=>xmlns,
:xmlns_xsi=>xmlns_xsi,
:status=>"1",
:name=>vdc[:name],
:id=>"urn:vcloud:vdc:#{vdc[:uuid]}",
:type=>"application/vnd.vmware.vcloud.vdc+xml",
:href=>make_href("vdc/#{vdc[:uuid]}"),
:xsi_schemaLocation=>xsi_schema_location,
:Link=>
[{:rel=>"up",
:type=>"application/vnd.vmware.vcloud.org+xml",
:href=>make_href("org/#{data[:org][:uuid]}")},
{:rel=>"down",
:type=>"application/vnd.vmware.vcloud.metadata+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/metadata")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.uploadVAppTemplateParams+xml",
:href=>
make_href("vdc/#{vdc[:uuid]}/action/uploadVAppTemplate")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.media+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/media")},
{:rel=>"add",
:type=>
"application/vnd.vmware.vcloud.instantiateVAppTemplateParams+xml",
:href=>
make_href("vdc/#{vdc[:uuid]}/action/instantiateVAppTemplate")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.cloneVAppParams+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/action/cloneVApp")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.cloneVAppTemplateParams+xml",
:href=>
make_href("vdc/#{vdc[:uuid]}/action/cloneVAppTemplate")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.cloneMediaParams+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/action/cloneMedia")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.captureVAppParams+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/action/captureVApp")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.composeVAppParams+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/action/composeVApp")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.diskCreateParams+xml",
:href=>make_href("vdc/#{vdc[:uuid]}/disk")},
{:rel=>"edgeGateways",
:type=>"application/vnd.vmware.vcloud.query.records+xml",
:href=>make_href("admin/vdc/#{vdc[:uuid]}/edgeGateways")},
{:rel=>"add",
:type=>"application/vnd.vmware.vcloud.orgVdcNetwork+xml",
:href=>make_href("admin/vdc/#{vdc[:uuid]}/networks")},
{:rel=>"orgVdcNetworks",
:type=>"application/vnd.vmware.vcloud.query.records+xml",
:href=>make_href("admin/vdc/#{vdc[:uuid]}/networks")}],
:Description=>vdc[:description]||'',
:AllocationModel=>"AllocationVApp",
:ComputeCapacity=>
{:Cpu=>
{:Units=>"MHz",
:Allocated=>"0",
:Limit=>"0",
:Reserved=>"0",
:Used=>"0",
:Overhead=>"0"},
:Memory=>
{:Units=>"MB",
:Allocated=>"0",
:Limit=>"0",
:Reserved=>"0",
:Used=>"0",
:Overhead=>"0"}},
:ResourceEntities => {},
:AvailableNetworks => {},
:Capabilities=>
{:SupportedHardwareVersions=>
{:SupportedHardwareVersion=>"vmx-08"}},
:NicQuota=>"0",
:NetworkQuota=>"20",
:UsedNetworkCount=>"0",
:VmQuota=>"100",
:IsEnabled=>"true"}
# Emulating Fog::ToHashDocument:
if data[:networks].size == 1
body[:AvailableNetworks][:Network] =
[{:type=>"application/vnd.vmware.vcloud.network+xml",
:name=>data[:networks].first[:name],
:href=>make_href("network/#{data[:networks].first[:name]}")}]
else
body[:AvailableNetworks][:Network] = data[:networks].map do |network|
{:type=>"application/vnd.vmware.vcloud.network+xml",
:name=>network[:name],
:href=>make_href("network/#{network[:name]}")}
end
end
# Emulating Fog::ToHashDocument:
if data[:catalog_items].size == 1
item_type = data[:catalog_items].first[:type]
body[:ResourceEntities][:ResourceEntity] =
{:type=>"application/vnd.vmware.vcloud.#{item_type}+xml",
:name=>data[:catalog_items].first[:name],
:href=>
make_href("#{item_type}/#{item_type}-#{data[:catalog_items].first[:uuid]}")}
else
body[:ResourceEntities][:ResourceEntity] = data[:catalog_items].map do |catalog_item|
item_type = catalog_item[:type]
{:type=>"application/vnd.vmware.vcloud.#{item_type}+xml",
:name=>catalog_item[:name],
:href=>
make_href("#{item_type}/#{item_type}-#{catalog_item[:uuid]}")}
end
end
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]}")}]}
end
Excon::Response.new(
:body => body,
:headers => {'Content-Type' => "#{body[:type]};version=#{api_version}"},
:status => 200
)
end
end
end
end

View file

@ -3,15 +3,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
VCR.use_cassette(File.basename(__FILE__)) do
Shindo.tests("Compute::VcloudDirector | organizations", ['vclouddirector', 'all']) do
pending if Fog.mocking?
organizations = vcloud_director.organizations
tests("#There is at least one organization").returns(true){ organizations.size >= 1 }
tests("#There is at least one organization").returns(true) { organizations.size >= 1 }
org = organizations.first
tests("Compute::VcloudDirector | organization") do
tests("#name").returns(String){ org.name.class }
tests("#type").returns("application/vnd.vmware.vcloud.org+xml"){ org.type }
tests("#name").returns(String) { org.name.class }
tests("#type").returns("application/vnd.vmware.vcloud.org+xml") { org.type }
end
tests("Compute::VcloudDirector | organization", ['get']) do
@ -19,4 +18,5 @@ VCR.use_cassette(File.basename(__FILE__)) do
tests("#get").returns(org.id) { organizations.get(org.id).id }
end
end
end

View file

@ -10,10 +10,10 @@ VCR.use_cassette(File.basename(__FILE__)) do
vdc = vdcs.first
tests("Compute::VcloudDirector | vdc") do
tests("#id").returns(String){ vdc.id.class }
tests("#name").returns(String){ vdc.name.class }
tests("#href").returns(String){ vdc.href.class }
tests("#type").returns("application/vnd.vmware.vcloud.vdc+xml"){ vdc.type }
tests("#id").returns(String) { vdc.id.class }
tests("#name").returns(String) { vdc.name.class }
tests("#href").returns(String) { vdc.href.class }
tests("#type").returns("application/vnd.vmware.vcloud.vdc+xml") { vdc.type }
end
tests("Compute::VcloudDirector | vdc", ['lazy load attrs']) do
@ -41,4 +41,5 @@ VCR.use_cassette(File.basename(__FILE__)) do
tests("#get").returns(vdc.id) { vdcs.get(vdc.id).id }
end
end
end

View file

@ -0,0 +1,48 @@
Shindo.tests('Compute::VcloudDirector | organization requests', ['vclouddirector']) do
ORG_LIST_FORMAT = {
:xmlns => 'http://www.vmware.com/vcloud/v1.5',
:xmlns_xsi => 'http://www.w3.org/2001/XMLSchema-instance',
:type => 'application/vnd.vmware.vcloud.orgList+xml',
:href => String,
:xsi_schemaLocation => String,
:Org => [{
:type => 'application/vnd.vmware.vcloud.org+xml',
:name => String,
:href => String
}]
}
ORG_FORMAT = {
:xmlns => 'http://www.vmware.com/vcloud/v1.5',
:xmlns_xsi => 'http://www.w3.org/2001/XMLSchema-instance',
:name => String,
:id => String,
:type => 'application/vnd.vmware.vcloud.org+xml',
:href => String,
:xsi_schemaLocation => String,
:Link => [{
:rel => String,
:type => String,
# :name => String, -- not always true
:href => String,
}],
:Description => String,
:FullName => String
}
@service = Fog::Compute::VcloudDirector.new
tests('#get_organizations').formats(ORG_LIST_FORMAT) do
@org_list = @service.get_organizations.body
@org_list[:Org] = [@org_list[:Org]] if @org_list[:Org].is_a?(Hash)
@org_list
end
tests('#get_organization').formats(ORG_FORMAT) do
org = @org_list[:Org].detect {|o| o[:name] == @service.org_name}
org_uuid = org[:href].split('/').last
@service.get_organization(org_uuid).body
end
end

View file

@ -0,0 +1,29 @@
Shindo.tests('Compute::VcloudDirector | session requests', ['vclouddirector']) do
SESSION_FORMAT = {
:xmlns => 'http://www.vmware.com/vcloud/v1.5',
:xmlns_xsi => 'http://www.w3.org/2001/XMLSchema-instance',
:user => String,
:org => String,
:type => 'application/vnd.vmware.vcloud.session+xml',
:href => String,
:xsi_schemaLocation => String,
:Link => [{
:rel => String,
:type => String,
:href => String
}]
}
@service = Fog::Compute::VcloudDirector.new
tests('#post_login_sessions').formats(SESSION_FORMAT) do
pending
@service.login.body # calls post_login_sessions
end
tests('#get_current_session').formats(SESSION_FORMAT) do
@service.get_current_session.body
end
end

View file

@ -0,0 +1,90 @@
Shindo.tests('Compute::VcloudDirector | vdc requests', ['vclouddirector']) do
VDC_FORMAT = {
:xmlns => 'http://www.vmware.com/vcloud/v1.5',
:xmlns_xsi => 'http://www.w3.org/2001/XMLSchema-instance',
:status => String,
:name => String,
:id => String,
:type => 'application/vnd.vmware.vcloud.vdc+xml',
:href => String,
:xsi_schemaLocation => String,
:Link => [{
:rel => String,
:type => String,
:href => String
}],
:Description => String,
:AllocationModel => String,
:ComputeCapacity => {
:Cpu => {
:Units => String,
:Allocated => String,
:Limit => String,
:Reserved => String,
:Used => String,
:Overhead => String,
},
:Memory => {
:Units => String,
:Allocated => String,
:Limit => String,
:Reserved => String,
:Used => String,
:Overhead => String
}
},
:ResourceEntities => {
:ResourceEntity => [{
:type => String,
:name => String,
:href => String
}]
},
:AvailableNetworks => {
:Network => [{
:type => String,
:name => String,
:href => String
}]
},
:Capabilities => {
:SupportedHardwareVersions => {
:SupportedHardwareVersion => String,
}
},
:NicQuota => String,
:NetworkQuota => String,
:UsedNetworkCount => String,
:VmQuota => String,
:IsEnabled => String,
# TODO
#:VdcStorageProfiles => { # >= 5.1
# :VdcStorageProfile => [{
# :type => String,
# :name => String,
# :href => String
# }]
#}
}
@service = Fog::Compute::VcloudDirector.new
tests('#Get current organization') do
session = @service.get_current_session.body
org_href = session[:Link].detect {|l| l[:type] == 'application/vnd.vmware.vcloud.org+xml'}[:href]
@org = @service.get_organization(org_uuid = org_href.split('/')[-1]).body
end
tests('#get_vdc').formats(VDC_FORMAT) do
vdc_href = @org[:Link].detect {|l| l[:type] == 'application/vnd.vmware.vcloud.vdc+xml'}[:href]
vdc = @service.get_vdc(vdc_href.split('/')[-1]).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
end
end