incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 3/3] Adapted drivers to use new exception handling DSL
Date Mon, 04 Apr 2011 14:02:06 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb    |   18 ++++++++++-----
 .../lib/deltacloud/drivers/ec2/ec2_mock_driver.rb  |    2 +-
 server/lib/deltacloud/drivers/mock/mock_driver.rb  |   20 ++++++++++------
 .../drivers/rackspace/rackspace_driver.rb          |   24 +++++++++++++++-----
 .../drivers/rimuhosting/rimuhosting_client.rb      |    3 +-
 .../drivers/rimuhosting/rimuhosting_driver.rb      |    9 ++-----
 server/lib/deltacloud/drivers/sbc/sbc_client.rb    |    4 +-
 server/lib/deltacloud/drivers/sbc/sbc_driver.rb    |   12 ++++++++++
 .../drivers/terremark/terremark_driver.rb          |   13 ++++------
 9 files changed, 67 insertions(+), 38 deletions(-)

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 302aac2..04074c0 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -713,12 +713,18 @@ module Deltacloud
           end
         end
 
-        def catched_exceptions_list
-          {
-            :auth => [ /AuthFailure/ ],
-            :error => [ /Aws::AwsError/, /Error/ ],
-            :glob => [ /AWS::(\w+)/, /Deltacloud::Runner::(\w+)/ ]
-          }
+        exceptions do
+          on /(AuthFailure|InvalidAccessKeyId)/ do
+            status 401
+          end
+
+          on /Error/ do
+            status 502
+          end
+
+          on /Deltacloud::Runner::(\w+)/ do
+            status 500
+          end
         end
 
       end
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb
index 6a9c051..01fccea 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb
@@ -180,7 +180,7 @@ Deltacloud::Drivers::EC2::EC2Driver.class_eval do
 
   def new_client(credentials, opts={})
     if credentials.user != 'mockuser' and credentials.password != 'mockpassword'
-      raise Deltacloud::AuthException.new
+      raise "AuthFailure"
     end
     RightAws::MockEc2.new
   end
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index a4f9e11..16ac76e 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -492,7 +492,7 @@ class MockDriver < Deltacloud::BaseDriver
     begin
       check_credentials(credentials)
       return true
-    rescue Deltacloud::AuthException
+    rescue
     end
     return false
   end
@@ -501,16 +501,20 @@ class MockDriver < Deltacloud::BaseDriver
 
   def check_credentials(credentials)
     if ( credentials.user != 'mockuser' ) or ( credentials.password != 'mockpassword' )
-      raise Deltacloud::AuthException.new
+      raise 'AuthFailure'
     end
   end
 
-  def catched_exceptions_list
-    {
-      :auth => [],
-      :error => [ /Deltacloud::BackendError/, /Errno::ENOENT/ ],
-      :glob => [ /Error/ ]
-    }
+  exceptions do
+
+    on /AuthFailure/ do
+      status 401
+    end
+
+    on /Err/ do
+      status 500
+    end
+
   end
 
 end
diff --git a/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb b/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
index b5f0c27..3089398 100644
--- a/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
+++ b/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
@@ -390,12 +390,24 @@ private
     end
   end
 
-  def catched_exceptions_list
-    {
-      :auth => [ /Authentication failed/ ],
-      :error => [ /Error/ ],
-      :glob => [ /CloudServers::Exception::(\w+)/, /Deltacloud::Runner::(\w+)/ ]
-    }
+  exceptions do
+
+    on /No offering found/ do
+      status 400 
+    end
+
+    on /Authentication failed/ do
+      status 401
+    end
+
+    on /Error/ do
+      status 500
+    end
+
+    on /CloudServers::Exception::(\w+)/ do
+      status 500
+    end
+
   end
 
   private
diff --git a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb
index c1f26ff..9d1d821 100755
--- a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb
+++ b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb
@@ -40,6 +40,7 @@ class RimuHostingClient
     if(!@auth.nil?)
       headers["Authorization"] = @auth
     end
+    safely do
     r = @service.send_request(method, @uri.path + resource, data, headers)
          puts r.body
     res = JSON.parse(r.body)
@@ -47,7 +48,7 @@ class RimuHostingClient
 
     if(res['response_type'] == "ERROR" and ( (res['error_info']['error_class'] == "PermissionException")
or
 					     (res['error_info']['error_class'] == "LoginRequired") )) 
-      raise Deltacloud::AuthException.new
+      raise "AuthFailure"
     end
     res
   end
diff --git a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
index ce5dc5f..6d37644 100755
--- a/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
+++ b/server/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb
@@ -149,15 +149,12 @@ class RimuHostingDriver < Deltacloud::BaseDriver
     stopped.to( :finish )         .automatically
   end
 
-  def safely(&block)
-    begin
-      block.call
-    rescue Exception => e
-      raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
+  exceptions do
+    on /AuthFailure/ do
+      status 401
     end
   end
 
-
 end
 
     end
diff --git a/server/lib/deltacloud/drivers/sbc/sbc_client.rb b/server/lib/deltacloud/drivers/sbc/sbc_client.rb
index 7c6012a..23da4ff 100644
--- a/server/lib/deltacloud/drivers/sbc/sbc_client.rb
+++ b/server/lib/deltacloud/drivers/sbc/sbc_client.rb
@@ -182,9 +182,9 @@ class SBCClient
   #
   def backend_error!(resp)
     if resp.is_a?(Net::HTTPUnauthorized)
-      raise Deltacloud::AuthException, resp.message
+      raise "AuthFailure"
     else
-      raise Deltacloud::BackendError.new(resp.code, resp.body, resp.message, '')
+      raise "BackendError"
     end
   end
 
diff --git a/server/lib/deltacloud/drivers/sbc/sbc_driver.rb b/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
index 1ef3d63..4ad20b5 100644
--- a/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
+++ b/server/lib/deltacloud/drivers/sbc/sbc_driver.rb
@@ -120,6 +120,18 @@ class SBCDriver < Deltacloud::BaseDriver
     instance(credentials, instance_id)
   end
 
+  exceptions do
+
+    on /AuthFailure/ do
+      status 401
+    end
+
+    on /BackendError/ do
+      status 502
+    end
+
+  end
+
   #
   # --------------------- Private helpers ---------------------
   #
diff --git a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
index 6e876d4..83a9cf3 100644
--- a/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
+++ b/server/lib/deltacloud/drivers/terremark/terremark_driver.rb
@@ -194,7 +194,7 @@ end
 def valid_credentials?(credentials)
   begin
     new_client(credentials)
-  rescue Deltacloud::AuthException
+  rescue
     return false
   end
   true
@@ -274,20 +274,17 @@ end
       vdc_id = terremark_client.default_vdc_id
     end
     if (vdc_id.nil?)
-       raise DeltaCloud::AuthException.new
+       raise "AuthFailure"
     end
     terremark_client
   end
 
-  def safely(&block)
-    begin
-      block.call
-    rescue Exception => e
-      raise Deltacloud::BackendError.new(500, e.class.to_s, e.message, e.backtrace)
+  exceptions do
+    on /AuthFailure/ do
+      status 401
     end
   end
 
-
 end
 
     end
-- 
1.7.4.1


Mime
View raw message