From df93720c0d381357244215090a3627505cff48bc Mon Sep 17 00:00:00 2001 From: Edward Muller Date: Thu, 29 Apr 2010 04:26:18 +0800 Subject: [PATCH] [terremark] More mocking --- lib/fog/terremark/ecloud.rb | 18 ++++-- .../requests/shared/get_organization.rb | 45 +++++++++++++- .../requests/shared/get_organizations.rb | 3 +- lib/fog/terremark/shared.rb | 60 ++++++++++++++----- lib/fog/terremark/vcloud.rb | 18 ++++-- 5 files changed, 120 insertions(+), 24 deletions(-) diff --git a/lib/fog/terremark/ecloud.rb b/lib/fog/terremark/ecloud.rb index 9d82cbd49..9fa344a85 100644 --- a/lib/fog/terremark/ecloud.rb +++ b/lib/fog/terremark/ecloud.rb @@ -5,6 +5,13 @@ module Fog module Bin end + module Defaults + HOST = 'services.enterprisecloud.terremark.com' + PATH = '/api/v0.8a-ext2.0' + PORT = 443 + SCHEME = 'https' + end + extend Fog::Terremark::Shared def self.new(options={}) @@ -31,10 +38,10 @@ module Fog def initialize(options={}) @terremark_password = options[:terremark_ecloud_password] @terremark_username = options[:terremark_ecloud_username] - @host = options[:host] || "services.enterprisecloud.terremark.com" - @path = options[:path] || "/api/v0.8a-ext2.0" - @port = options[:port] || 443 - @scheme = options[:scheme] || 'https' + @host = options[:host] || Fog::Terremark::Ecloud::Defaults::HOST + @path = options[:path] || Fog::Terremark::Ecloud::Defaults::PATH + @port = options[:port] || Fog::Terremark::Ecloud::Defaults::PORT + @scheme = options[:scheme] || Fog::Terremark::Ecloud::Defaults::SCHEME @cookie = get_organizations.headers['Set-Cookie'] end @@ -46,6 +53,9 @@ module Fog def initialize(option = {}) super + @base_url = Fog::Terremark::Ecloud::Defaults::SCHEME + "://" + + Fog::Terremark::Ecloud::Defaults::HOST + + Fog::Terremark::Ecloud::Defaults::PATH @data = self.class.data[:terremark_ecloud_username] end end diff --git a/lib/fog/terremark/requests/shared/get_organization.rb b/lib/fog/terremark/requests/shared/get_organization.rb index ee47df4e5..709d635fa 100644 --- a/lib/fog/terremark/requests/shared/get_organization.rb +++ b/lib/fog/terremark/requests/shared/get_organization.rb @@ -33,7 +33,50 @@ module Fog module Mock def get_organization(organization_id) - raise MockNotImplemented.new("Contributions welcome!") + response = Excon::Response.new + + if org = @data[:organizations].detect { |org| org[:info][:id] == organization_id } + + body = { "name" => org[:info][:name], + "href" => "#{@base_url}/#{org[:info][:id]}", + "Links" => [] } + + body["Links"] = case self + when Fog::Terremark::Vcloud::Mock + _vdc_links(org[:vdcs][0]) + when Fog::Terremark::Ecloud::Mock + org[:vdcs].map do |vdc| + _vdc_links(vdc) + end.flatten + end + + response.status = 200 + response.body = body + response.headers = Fog::Terremark::Shared::Mock.headers(response.body, "application/vnd.vmware.vcloud.org+xml") + else + response.status = Fog::Terremark::Shared::Mock.unathorized_status + response.headers = Fog::Terremark::Shared::Mock.error_headers + end + + response + end + + private + + def _vdc_links(vdc) + [{ "name" => vdc[:name], + "href" => "#{@base_url}/vdc/#{vdc[:id]}", + "rel" => "down", + "type" => "application/vnd.vmware.vcloud.vdc+xml" }, + { "name" => "#{vdc[:name]} Catalog", + "href" => "#{@base_url}/vdc/#{vdc[:id]}/catalog", + "rel" => "down", + "type" => "application/vnd.vmware.vcloud.catalog+xml" }, + { "name" => "#{vdc[:name]} Tasks List", + "href" => "#{@base_url}/vdc/#{vdc[:id]}/taskslist", + "rel" => "down", + "type" => "application/vnd.vmware.vcloud.tasksList+xml" } + ] end end diff --git a/lib/fog/terremark/requests/shared/get_organizations.rb b/lib/fog/terremark/requests/shared/get_organizations.rb index dc51ecce4..0036526d6 100644 --- a/lib/fog/terremark/requests/shared/get_organizations.rb +++ b/lib/fog/terremark/requests/shared/get_organizations.rb @@ -33,11 +33,12 @@ module Fog response = Excon::Response.new org_list = @data[:organizations].map do |organization| { "name" => organization[:info][:name], - "href" => "https://services.enterprisecloud.terremark.com/api/v0.8a-ext2.0/org/#{organization[:info][:id]}", + "href" => "#{@base_url}/org/#{organization[:info][:id]}", "type" => "application/vnd.vmware.vcloud.org+xml" } end response.body = { "OrgList" => org_list } + response.status = 200 response.headers = Fog::Terremark::Shared::Mock.headers(response.body, "application/vnd.vmware.vcloud.orgList+xml") response end diff --git a/lib/fog/terremark/shared.rb b/lib/fog/terremark/shared.rb index 78e1d52d3..2ea254d91 100644 --- a/lib/fog/terremark/shared.rb +++ b/lib/fog/terremark/shared.rb @@ -2,6 +2,23 @@ module Fog module Terremark module Shared + # Commond methods shared by Real and Mock + module Common + + # TODO: bust cache on organization creation? + def default_organization_id + @default_organization_id ||= begin + org_list = get_organizations.body['OrgList'] + if org_list.length == 1 + org_list.first['href'].split('/').last.to_i + else + nil + end + end + end + + end + module Parser def parse(data) @@ -16,18 +33,7 @@ module Fog end module Real - - # TODO: bust cache on organization creation? - def default_organization_id - @default_organization_id ||= begin - org_list = get_organizations.body['OrgList'] - if org_list.length == 1 - org_list.first['href'].split('/').last.to_i - else - nil - end - end - end + include Common private @@ -51,6 +57,7 @@ module Fog end module Mock + include Common DATA = { :organizations => @@ -58,12 +65,33 @@ module Fog { :info => { :name => "Boom Inc.", - :id => "1" - } + :id => 1 + }, + :vdcs => [ + { :id => 21, + :name => "Boomstick" + }, + { :id => 22, + :name => "Rock-n-Roll" + } + ] } ] } + def self.error_headers + {"X-Powered-By"=>"ASP.NET", + "Date"=> Time.now.to_s, + "Content-Type"=>"text/html", + "Content-Length"=>"0", + "Server"=>"Microsoft-IIS/7.0", + "Cache-Control"=>"private"} + end + + def self.unathorized_status + 401 + end + def self.headers(body, content_type) {"X-Powered-By"=>"ASP.NET", "Date"=> Time.now.to_s, @@ -74,6 +102,10 @@ module Fog "Cache-Control"=>"private"} end + def self.status + 200 + end + def initialize(options={}) self.class.instance_eval ' def self.data diff --git a/lib/fog/terremark/vcloud.rb b/lib/fog/terremark/vcloud.rb index 5a9636e26..2eb7d20c8 100644 --- a/lib/fog/terremark/vcloud.rb +++ b/lib/fog/terremark/vcloud.rb @@ -5,6 +5,13 @@ module Fog module Bin end + module Defaults + HOST = 'services.vcloudexpress.terremark.com' + PATH = '/api/v0.8' + PORT = 443 + SCHEME = 'https' + end + extend Fog::Terremark::Shared def self.new(options={}) @@ -31,10 +38,10 @@ module Fog def initialize(options={}) @terremark_password = options[:terremark_vcloud_password] @terremark_username = options[:terremark_vcloud_username] - @host = options[:host] || "services.vcloudexpress.terremark.com" - @path = options[:path] || "/api/v0.8" - @port = options[:port] || 443 - @scheme = options[:scheme] || 'https' + @host = options[:host] || Fog::Terremark::Vcloud::Defaults::HOST + @path = options[:path] || Fog::Terremark::Vcloud::Defaults::PATH + @port = options[:port] || FOG::Terremark::Vcloud::Defaults::PORT + @scheme = options[:scheme] || FOG::Terremark::Vcloud::Defaults::SCHEME @cookie = get_organizations.headers['Set-Cookie'] end @@ -92,6 +99,9 @@ module Fog def initialize(option = {}) super + @base_url = Fog::Terremark::Vcloud::Defaults::SCHEME + "://" + + Fog::Terremark::Vcloud::Defaults::HOST + + Fog::Terremark::Vcloud::Defaults::PATH @data = self.class.data[:terremark_vcloud_username] end end