incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Allow client to change driver/provider using HTTP headers
Date Thu, 09 Dec 2010 15:55:52 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 client/lib/deltacloud.rb                 |   28 +++++++++++++++++++++++-----
 server/lib/sinatra/rack_driver_select.rb |    5 +----
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index c632594..987487d 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -34,8 +34,9 @@ module DeltaCloud
   # @param [String, password] API password
   # @param [String, user_name] API URL (eg. http://localhost:3001/api)
   # @return [DeltaCloud::API]
-  def self.new(user_name, password, api_url, &block)
-    API.new(user_name, password, api_url, &block)
+  def self.new(user_name, password, api_url, opts={}, &block)
+    opts ||= {}
+    API.new(user_name, password, api_url, opts, &block)
   end
 
   # Check given credentials if their are valid against
@@ -62,11 +63,13 @@ module DeltaCloud
   end
 
   class API
-    attr_reader   :api_uri, :driver_name, :api_version, :features, :entry_points
+    attr_reader :api_uri, :driver_name, :api_version, :features, :entry_points
+    attr_reader :api_driver, :api_provider
 
     def initialize(user_name, password, api_url, opts={}, &block)
       opts[:version] = true
-      @username, @password = user_name, password
+      @api_driver, @api_provider = opts[:driver], opts[:provider]
+      @username, @password = opts[:username] || user_name, opts[:password] || password
       @api_uri = URI.parse(api_url)
       @features, @entry_points = {}, {}
       @verbose = opts[:verbose] || false
@@ -241,6 +244,21 @@ module DeltaCloud
       raise NoMethodError
     end
 
+    def use_driver(driver, opts={})
+      @api_driver = driver
+      @username = opts[:username]
+      @password = opts[:password]
+      @api_provider = opts[:provider] if opts[:provider]
+      return self
+    end
+
+    def extended_headers
+      headers = {}
+      headers["X-Deltacloud-Driver"] = "#{@api_driver}" if @api_driver
+      headers["X-Deltacloud-Provider"] = "#{@api_provider}" if @api_provider
+      headers
+    end
+
     # Basic request method
     #
     def request(*args, &block)
@@ -264,7 +282,7 @@ module DeltaCloud
           end
         end
       else
-        RestClient.send(conf[:method], conf[:path], default_headers) do |response, request,
block|
+        RestClient.send(conf[:method], conf[:path], default_headers.merge(extended_headers))
do |response, request, block|
           handle_backend_error(response) if response.code.eql?(500)
           if conf[:method].eql?(:get) and [301, 302, 307].include? response.code
             response.follow_redirection(request) do |response, request, block|
diff --git a/server/lib/sinatra/rack_driver_select.rb b/server/lib/sinatra/rack_driver_select.rb
index aa213d4..f00a2c8 100644
--- a/server/lib/sinatra/rack_driver_select.rb
+++ b/server/lib/sinatra/rack_driver_select.rb
@@ -15,10 +15,7 @@ class RackDriverSelect
   end
 
   def extract_driver(env)
-    if env['HTTP_HEADERS']
-      driver_name = env['HTTP_HEADERS'].match(/X\-Deltacloud\-Driver:(\w+)/i).to_a
-      driver_name[1] ? driver_name[1].downcase : nil
-    end
+    driver_name = env['HTTP_X_DELTACLOUD_DRIVER'].downcase if env['HTTP_X_DELTACLOUD_DRIVER']
   end
 
 end
-- 
1.7.3.2


Mime
View raw message