incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tcraw...@redhat.com
Subject [PATCH 1/4] Rework endpoint selection for new ec2 driver.
Date Wed, 15 Dec 2010 23:04:39 GMT
From: Tobias Crawley <tcrawley@redhat.com>

This renames provider => endpoint, 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                        |   12 +++---
 server/bin/deltacloudd                          |    6 +-
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |   49 ++++++++++++++++-------
 server/lib/sinatra/rack_driver_select.rb        |    2 +-
 4 files changed, 44 insertions(+), 25 deletions(-)

diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 114b013..c81380a 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)
@@ -64,11 +64,11 @@ module DeltaCloud
 
   class API
     attr_reader :api_uri, :driver_name, :api_version, :features, :entry_points
-    attr_reader :api_driver, :api_provider
+    attr_reader :api_driver, :api_endpoint
 
     def initialize(user_name, password, api_url, opts={}, &block)
       opts[:version] = true
-      @api_driver, @api_provider = opts[:driver], opts[:provider]
+      @api_driver, @api_endpoint = opts[:driver], opts[:endpoint]
       @username, @password = opts[:username] || user_name, opts[:password] || password
       @api_uri = URI.parse(api_url)
       @features, @entry_points = {}, {}
@@ -252,14 +252,14 @@ module DeltaCloud
       end
       @username = opts[:username] if opts[:username]
       @password = opts[:password] if opts[:password]
-      @api_provider = opts[:provider] if opts[:provider]
+      @api_endpoint = opts[:endpoint] if opts[:endpoint]
       return self
     end
 
     def extended_headers
       headers = {}
       headers["X-Deltacloud-Driver"] = @api_driver.to_s if @api_driver
-      headers["X-Deltacloud-Provider"] = @api_provider.to_s if @api_provider
+      headers["X-Deltacloud-Endpoint"] = @api_endpoint.to_s if @api_endpoint
       headers
     end
 
diff --git a/server/bin/deltacloudd b/server/bin/deltacloudd
index 70be6ca..64fb9cd 100755
--- a/server/bin/deltacloudd
+++ b/server/bin/deltacloudd
@@ -36,8 +36,8 @@ BANNER
   opts.on( '-p', '--port PORT', 'Use PORT (default: 3001)') do |port|
     ENV["API_PORT"] = port
   end
-  opts.on( '-P', '--provider PROVIDER', 'Use PROVIDER (default is set in the driver)') do
|provider|
-    ENV['API_PROVIDER'] = provider
+  opts.on( '-E', '--endpoint ENDPOINT', 'Use ENDPOINT (default is set in the driver)') do
|endpoint|
+    ENV['API_ENDPOINT'] = endpoint
   end
   opts.on( '-e', '--env ENV', 'Environment (default: "development")') { |env| options[:env]
= env }
   opts.on( '-h', '--help', '') { options[:help] = true }
@@ -59,7 +59,7 @@ ENV["API_HOST"] = "localhost" unless ENV["API_HOST"]
 ENV["API_PORT"] = "3001" unless ENV["API_PORT"]
 
 msg = "Starting Deltacloud API :: #{ENV["API_DRIVER"]} "
-msg << ":: #{ENV['API_PROVIDER']} " if ENV['API_PROVIDER']
+msg << ":: #{ENV['API_ENDPOINT']} " if ENV['API_ENDPOINT']
 msg << ":: http://#{ENV["API_HOST"]}:#{ENV["API_PORT"]}/api"
 puts msg
 puts
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index c547d5c..a019fa4 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-ap-southeast-1.amazonaws.com',
+            'eu-west-1' => 's3.amazonaws.com',
+            'us-east-1' => 's3.amazonaws.com',
+            'us-west-1' => 's3-us-west-1.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[:endpoint] || ENV['API_ENDPOINT'] || 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)
diff --git a/server/lib/sinatra/rack_driver_select.rb b/server/lib/sinatra/rack_driver_select.rb
index 3e7d038..6fef677 100644
--- a/server/lib/sinatra/rack_driver_select.rb
+++ b/server/lib/sinatra/rack_driver_select.rb
@@ -7,7 +7,7 @@ class RackDriverSelect
 
   HEADER_TO_ENV_MAP = {
     'HTTP_X_DELTACLOUD_DRIVER' => :driver,
-    'HTTP_X_DELTACLOUD_PROVIDER' => :provider
+    'HTTP_X_DELTACLOUD_ENDPOINT' => :endpoint
   }
   
   def call(env)
-- 
1.7.3.2


Mime
View raw message