Updated Branches: refs/heads/trunk 7371d594b -> 043c81ca4
Update CloudStack driver to better handle errors and throw ProviderError instead of a generic Exception. Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/043c81ca Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/043c81ca Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/043c81ca Branch: refs/heads/trunk Commit: 043c81ca4fa05f4949bf1eae9d19206bcde8f66b Parents: 7371d59 Author: Tomaz Muraus <[email protected]> Authored: Wed Dec 11 00:54:07 2013 +0100 Committer: Tomaz Muraus <[email protected]> Committed: Wed Dec 11 00:54:07 2013 +0100 ---------------------------------------------------------------------- CHANGES | 4 ++++ libcloud/common/cloudstack.py | 13 ++++++++++++- .../cloudstack/registerSSHKeyPair_error.json | 1 + libcloud/test/compute/test_cloudstack.py | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/043c81ca/CHANGES ---------------------------------------------------------------------- diff --git a/CHANGES b/CHANGES index ff3905c..f534fcd 100644 --- a/CHANGES +++ b/CHANGES @@ -62,6 +62,10 @@ Changes with Apache Libcloud in development This could happen if node is in an error state. (LIBCLOUD-455) [Dustin Spicuzza, Tomaz Muraus] + - Update CloudStack driver to better handle errors and throw ProviderError + instead of a generic Exception. + [Tomaz Muraus] + *) Storage - Allow user to specify 'Content-Disposition' header in the CloudFiles http://git-wip-us.apache.org/repos/asf/libcloud/blob/043c81ca/libcloud/common/cloudstack.py ---------------------------------------------------------------------- diff --git a/libcloud/common/cloudstack.py b/libcloud/common/cloudstack.py index 4a22ba3..b6150f4 100644 --- a/libcloud/common/cloudstack.py +++ b/libcloud/common/cloudstack.py @@ -22,6 +22,7 @@ from libcloud.utils.py3 import httplib from libcloud.utils.py3 import urlencode from libcloud.utils.py3 import b +from libcloud.common.types import ProviderError from libcloud.common.base import ConnectionUserAndKey, PollingConnection from libcloud.common.base import JsonResponse from libcloud.common.types import MalformedResponseError @@ -33,7 +34,17 @@ class CloudStackResponse(JsonResponse): if self.status == httplib.UNAUTHORIZED: raise InvalidCredsError('Invalid provider credentials') - return self.body + body = self.parse_body() + values = list(body.values())[0] + + if 'errortext' in values: + value = values['errortext'] + else: + value = self.body + + error = ProviderError(value=value, http_code=self.status, + driver=self.connection.driver) + raise error class CloudStackConnection(ConnectionUserAndKey, PollingConnection): http://git-wip-us.apache.org/repos/asf/libcloud/blob/043c81ca/libcloud/test/compute/fixtures/cloudstack/registerSSHKeyPair_error.json ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/fixtures/cloudstack/registerSSHKeyPair_error.json b/libcloud/test/compute/fixtures/cloudstack/registerSSHKeyPair_error.json new file mode 100644 index 0000000..6622ab1 --- /dev/null +++ b/libcloud/test/compute/fixtures/cloudstack/registerSSHKeyPair_error.json @@ -0,0 +1 @@ +{ "registersshkeypairresponse" : {"uuidList":[],"errorcode":431,"errortext":"Public key is invalid"} } http://git-wip-us.apache.org/repos/asf/libcloud/blob/043c81ca/libcloud/test/compute/test_cloudstack.py ---------------------------------------------------------------------- diff --git a/libcloud/test/compute/test_cloudstack.py b/libcloud/test/compute/test_cloudstack.py index c9adbdb..c038270 100644 --- a/libcloud/test/compute/test_cloudstack.py +++ b/libcloud/test/compute/test_cloudstack.py @@ -25,6 +25,7 @@ try: except ImportError: import json +from libcloud.common.types import ProviderError from libcloud.compute.drivers.cloudstack import CloudStackNodeDriver from libcloud.compute.types import LibcloudError, Provider, InvalidCredsError from libcloud.compute.types import KeyPairDoesNotExistError @@ -57,6 +58,17 @@ class CloudStackCommonTestCase(TestCaseMixin): host='api.dummy.com') self.assertRaises(InvalidCredsError, driver.list_nodes) + def test_import_keypair_from_string_api_error(self): + CloudStackMockHttp.type = 'api_error' + + name = 'test-pair' + key_material = '' + + expected_msg = 'Public key is invalid' + self.assertRaisesRegexp(ProviderError, expected_msg, + self.driver.import_key_pair_from_string, + name=name, key_material=key_material) + def test_create_node_immediate_failure(self): size = self.driver.list_sizes()[0] image = self.driver.list_images()[0] @@ -503,6 +515,11 @@ class CloudStackMockHttp(MockHttpTestCase): return (httplib.UNAUTHORIZED, body, {}, httplib.responses[httplib.UNAUTHORIZED]) + def _test_path_api_error(self, method, url, body, headers): + body = self.fixtures.load('registerSSHKeyPair_error.json') + return (431, body, {}, + httplib.responses[httplib.OK]) + def _test_path(self, method, url, body, headers): url = urlparse.urlparse(url) query = dict(parse_qsl(url.query))
