Author: tomaz
Date: Mon May 23 23:02:46 2011
New Revision: 1126805
URL: http://svn.apache.org/viewvc?rev=1126805&view=rev
Log:
Properly handle response errors in the Rackspace driver and only throw
InvalidCredsError on 401 status code.
Modified:
incubator/libcloud/trunk/libcloud/common/rackspace.py
incubator/libcloud/trunk/test/compute/test_rackspace.py
Modified: incubator/libcloud/trunk/libcloud/common/rackspace.py
URL:
http://svn.apache.org/viewvc/incubator/libcloud/trunk/libcloud/common/rackspace.py?rev=1126805&r1=1126804&r2=1126805&view=diff
==============================================================================
--- incubator/libcloud/trunk/libcloud/common/rackspace.py (original)
+++ incubator/libcloud/trunk/libcloud/common/rackspace.py Mon May 23 23:02:46
2011
@@ -19,7 +19,7 @@ Common utilities for Rackspace Cloud Ser
import httplib
from urllib2 import urlparse
from libcloud.common.base import ConnectionUserAndKey
-from libcloud.compute.types import InvalidCredsError
+from libcloud.compute.types import InvalidCredsError, MalformedResponseError
AUTH_HOST_US='auth.api.rackspacecloud.com'
AUTH_HOST_UK='lon.auth.api.rackspacecloud.com'
@@ -95,19 +95,29 @@ class RackspaceBaseConnection(Connection
resp = conn.getresponse()
- if resp.status != httplib.NO_CONTENT:
- raise InvalidCredsError()
-
- headers = dict(resp.getheaders())
-
- try:
- self.server_url = headers['x-server-management-url']
- self.storage_url = headers['x-storage-url']
- self.cdn_management_url = headers['x-cdn-management-url']
- self.lb_url = self.server_url.replace("servers",
"ord.loadbalancers")
- self.auth_token = headers['x-auth-token']
- except KeyError:
+ if resp.status == httplib.NO_CONTENT:
+ # HTTP NO CONTENT (204): auth successful
+ headers = dict(resp.getheaders())
+
+ try:
+ self.server_url = headers['x-server-management-url']
+ self.storage_url = headers['x-storage-url']
+ self.cdn_management_url = headers['x-cdn-management-url']
+ self.lb_url = self.server_url.replace("servers",
"ord.loadbalancers")
+ self.auth_token = headers['x-auth-token']
+ except KeyError, e:
+ # Returned 204 but has missing information in the header,
something is wrong
+ raise MalformedResponseError('Malformed response',
+ body='Missing header: %s' %
(str(e)),
+ driver=self.driver)
+ elif resp.status == httplib.UNAUTHORIZED:
+ # HTTP UNAUTHORIZED (401): auth failed
raise InvalidCredsError()
+ else:
+ # Any response code != 401 or 204, something is wrong
+ raise MalformedResponseError('Malformed response',
+ body='code: %s body:%s' % (resp.status,
''.join(resp.body.readlines())),
+ driver=self.driver)
for key in ['server_url', 'storage_url', 'cdn_management_url',
'lb_url']:
Modified: incubator/libcloud/trunk/test/compute/test_rackspace.py
URL:
http://svn.apache.org/viewvc/incubator/libcloud/trunk/test/compute/test_rackspace.py?rev=1126805&r1=1126804&r2=1126805&view=diff
==============================================================================
--- incubator/libcloud/trunk/test/compute/test_rackspace.py (original)
+++ incubator/libcloud/trunk/test/compute/test_rackspace.py Mon May 23 23:02:46
2011
@@ -16,7 +16,7 @@ import sys
import unittest
import httplib
-from libcloud.common.types import InvalidCredsError
+from libcloud.common.types import InvalidCredsError, MalformedResponseError
from libcloud.compute.drivers.rackspace import RackspaceNodeDriver as Rackspace
from libcloud.compute.base import Node, NodeImage, NodeSize
@@ -46,8 +46,17 @@ class RackspaceTests(unittest.TestCase,
RackspaceMockHttp.type = 'UNAUTHORIZED_MISSING_KEY'
try:
self.driver = Rackspace(RACKSPACE_USER, RACKSPACE_KEY)
- except InvalidCredsError, e:
- self.assertEqual(True, isinstance(e, InvalidCredsError))
+ except MalformedResponseError, e:
+ self.assertEqual(True, isinstance(e, MalformedResponseError))
+ else:
+ self.fail('test should have thrown')
+
+ def test_auth_server_error(self):
+ RackspaceMockHttp.type = 'INTERNAL_SERVER_ERROR'
+ try:
+ self.driver = Rackspace(RACKSPACE_USER, RACKSPACE_KEY)
+ except MalformedResponseError, e:
+ self.assertEqual(True, isinstance(e, MalformedResponseError))
else:
self.fail('test should have thrown')
@@ -197,6 +206,9 @@ class RackspaceMockHttp(MockHttp):
def _v1_0_UNAUTHORIZED(self, method, url, body, headers):
return (httplib.UNAUTHORIZED, "", {},
httplib.responses[httplib.UNAUTHORIZED])
+ def _v1_0_INTERNAL_SERVER_ERROR(self, method, url, body, headers):
+ return (httplib.INTERNAL_SERVER_ERROR, "<h1>500: Internal Server
Error</h1>", {}, httplib.responses[httplib.INTERNAL_SERVER_ERROR])
+
def _v1_0_UNAUTHORIZED_MISSING_KEY(self, method, url, body, headers):
headers = {'x-server-management-url':
'https://servers.api.rackspacecloud.com/v1.0/slug',
'x-auth-token': 'FE011C19-CF86-4F87-BE5D-9229145D7A06',
@@ -277,7 +289,5 @@ class RackspaceMockHttp(MockHttp):
def _v1_0_slug_servers_3445_ips_public_67_23_21_133(self, method, url,
body, headers):
return (httplib.ACCEPTED, "", {}, httplib.responses[httplib.ACCEPTED])
-
-
if __name__ == '__main__':
sys.exit(unittest.main())