From: Tobias Crawley <[email protected]> This replaces url generation with a lookup table and allows passing through a full hostname as the endpoint instead of just the region name. --- client/lib/deltacloud.rb | 4 +- server/lib/deltacloud/drivers/ec2/ec2_driver.rb | 49 ++++++++++++++++------- 2 files changed, 36 insertions(+), 17 deletions(-)
diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb index 114b013..6985d5c 100644 --- a/client/lib/deltacloud.rb +++ b/client/lib/deltacloud.rb @@ -46,8 +46,8 @@ module DeltaCloud # @param [String, password] API password # @param [String, user_name] API URL (eg. http://localhost:3001/api) # @return [true|false] - def self.valid_credentials?(user_name, password, api_url) - api=API.new(user_name, password, api_url) + def self.valid_credentials?(user_name, password, api_url, opts={}) + api=API.new(user_name, password, api_url, opts) result = false api.request(:get, '', :force_auth => '1') do |response| result = true if response.code.eql?(200) diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb index c547d5c..77304c0 100644 --- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb +++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb @@ -418,23 +418,42 @@ module Deltacloud private def new_client(credentials, type = :ec2) - case type - when :ec2 then Aws::Ec2.new(credentials.user, credentials.password, :endpoint_url => endpoint_for_service(:ec2)) - when :s3 then Aws::S3.new(credentials.user, credentials.password, :endpoint_url => endpoint_for_service(:s3)) - end - end + klass = case type + when :elb then Aws::Elb + when :ec2 then Aws::Ec2 + when :s3 then Aws::S3 + end + klass.new(credentials.user, credentials.password, :server => endpoint_for_service(type)) + end + + DEFAULT_SERVICE_ENDPOINTS = { + 'ec2' => { + 'ap-southeast-1' => 'ec2.ap-southeast-1.amazonaws.com', + 'eu-west-1' => 'ec2.eu-west-1.amazonaws.com', + 'us-east-1' => 'ec2.us-east-1.amazonaws.com', + 'us-west-1' => 'ec2.us-west-1.amazonaws.com' + }, + + 'elb' => { + 'ap-southeast-1' => 'elasticloadbalancing.ap-southeast-1.amazonaws.com', + 'eu-west-1' => 'elasticloadbalancing.eu-west-1.amazonaws.com', + 'us-east-1' => 'elasticloadbalancing.us-east-1.amazonaws.com', + 'us-west-1' => 'elasticloadbalancing.us-west-1.amazonaws.com' + }, + + 's3' => { + 'ap-southeast-1' => 's3.amazonaws.com', + 'eu-west-1' => 's3.amazonaws.com', + 'us-east-1' => 's3.amazonaws.com', + 'us-west-1' => 's3.amazonaws.com' + } + } def endpoint_for_service(service) - url = "" - url << case service - when :ec2 - 'ec2.' - when :elb - 'elasticloadbalancing.' - end - url << (Thread.current[:provider] || ENV['API_PROVIDER'] || DEFAULT_REGION) - url << '.amazonaws.com' - url + endpoint = (Thread.current[:provider] || ENV['API_PROVIDER'] || DEFAULT_REGION) + # return the endpoint if it does not map to a default endpoint, allowing + # the endpoint to be a full hostname instead of a region. + DEFAULT_SERVICE_ENDPOINTS[service.to_s][endpoint] || endpoint end def tag_instance(credentials, instance, name) -- 1.7.3.2
