From 5cce71baaa13fc651d76b6b26a29effbf879f1b1 Mon Sep 17 00:00:00 2001 From: Chuck Short Date: Thu, 3 Jan 2013 09:04:58 -0600 Subject: Refactor EC2 keypairs exception According to the following document: http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/api-error-codes.html The EC2 API sends specific error codes when keypair listing or keypair generation fails, specifically the error codes are InvalidKeyPair.Duplicate - duplicate keypair InvalidKeyPair.Format - keypair format invalid InvalidKeyPair.NotFound - keypair not found ResourceLimitExceeded - keypair quota exceeded Refactored create_key_pair and describe_key_pair so that it throws the correct EC2 error codes according to the public EC2 API specs. Also removed some duplicate exceptions as well. Fixes LP: 1072318 DocImpact Change-Id: Ib7045e49211d9300e9cb6ca0bfe80e569d635c9b Signed-off-by: Chuck Short --- nova/api/ec2/__init__.py | 8 +++++++- nova/api/ec2/cloud.py | 14 +++----------- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'nova/api') diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py index 7cd7e1c7d..85b87e3e5 100644 --- a/nova/api/ec2/__init__.py +++ b/nova/api/ec2/__init__.py @@ -511,7 +511,13 @@ class Executor(wsgi.Application): except exception.KeyPairExists as ex: LOG.debug(_('KeyPairExists raised: %s'), unicode(ex), context=context) - return ec2_error(req, request_id, type(ex).__name__, unicode(ex)) + code = 'InvalidKeyPair.Duplicate' + return ec2_error(req, request_id, code, unicode(ex)) + except exception.InvalidKeypair as ex: + LOG.debug(_('InvalidKeypair raised: %s'), unicode(ex), + context) + code = 'InvalidKeyPair.Format' + return ec2_error(req, request_id, code, unicode(ex)) except exception.InvalidParameterValue as ex: LOG.debug(_('InvalidParameterValue raised: %s'), unicode(ex), context=context) diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 414b2e969..31f486b81 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -434,7 +434,8 @@ class CloudController(object): #If looking for non existent key pair if key_name is not None and not key_pairs: msg = _('Could not find key pair(s): %s') % ','.join(key_name) - raise exception.EC2APIError(msg) + raise exception.KeypairNotFound(msg, + code="InvalidKeyPair.Duplicate") result = [] for key_pair in key_pairs: @@ -457,13 +458,7 @@ class CloudController(object): key_name) except exception.KeypairLimitExceeded: msg = _("Quota exceeded, too many key pairs.") - raise exception.EC2APIError(msg) - except exception.InvalidKeypair: - msg = _("Keypair data is invalid") - raise exception.EC2APIError(msg) - except exception.KeyPairExists: - msg = _("Key pair '%s' already exists.") % key_name - raise exception.KeyPairExists(msg) + raise exception.EC2APIError(msg, code='ResourceLimitExceeded') return {'keyName': key_name, 'keyFingerprint': keypair['fingerprint'], 'keyMaterial': keypair['private_key']} @@ -486,9 +481,6 @@ class CloudController(object): except exception.InvalidKeypair: msg = _("Keypair data is invalid") raise exception.EC2APIError(msg) - except exception.KeyPairExists: - msg = _("Key pair '%s' already exists.") % key_name - raise exception.EC2APIError(msg) return {'keyName': key_name, 'keyFingerprint': keypair['fingerprint']} -- cgit