diff options
author | Brian Waldon <bcwaldon@gmail.com> | 2012-02-03 13:29:57 -0800 |
---|---|---|
committer | Brian Waldon <bcwaldon@gmail.com> | 2012-02-14 12:20:20 -0800 |
commit | afd5b22368076fc640563b7df6fb71dab57fe627 (patch) | |
tree | 9656e48da966bcb95106d99b3a1f43725870a627 /nova/api | |
parent | c9ca372b0b9fe887dd3ac6bdb02514b5495a1917 (diff) | |
download | nova-afd5b22368076fc640563b7df6fb71dab57fe627.tar.gz nova-afd5b22368076fc640563b7df6fb71dab57fe627.tar.xz nova-afd5b22368076fc640563b7df6fb71dab57fe627.zip |
Replace ApiError with new exceptions
* Convert ApiError to EC2APIError
* Add new exceptions to replace ApiError where it didn't belong
* Fixes bug 926250
Change-Id: Ia711440ee0313faf8ea8c87e2c0a2f5b39cc55a2
Diffstat (limited to 'nova/api')
-rw-r--r-- | nova/api/ec2/__init__.py | 4 | ||||
-rw-r--r-- | nova/api/ec2/cloud.py | 38 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/cloudpipe.py | 2 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/console_output.py | 4 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/consoles.py | 4 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/floating_ips.py | 10 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/server_start_stop.py | 32 | ||||
-rw-r--r-- | nova/api/openstack/compute/contrib/volumetypes.py | 2 | ||||
-rw-r--r-- | nova/api/openstack/compute/server_metadata.py | 2 | ||||
-rw-r--r-- | nova/api/openstack/compute/servers.py | 8 | ||||
-rw-r--r-- | nova/api/openstack/volume/types.py | 2 |
11 files changed, 50 insertions, 58 deletions
diff --git a/nova/api/ec2/__init__.py b/nova/api/ec2/__init__.py index ddb3a2044..416783c45 100644 --- a/nova/api/ec2/__init__.py +++ b/nova/api/ec2/__init__.py @@ -598,8 +598,8 @@ class Executor(wsgi.Application): except exception.NotFound as ex: LOG.info(_('NotFound raised: %s'), unicode(ex), context=context) return ec2_error(req, request_id, type(ex).__name__, unicode(ex)) - except exception.ApiError as ex: - LOG.exception(_('ApiError raised: %s'), unicode(ex), + except exception.EC2APIError as ex: + LOG.exception(_('EC2APIError raised: %s'), unicode(ex), context=context) if ex.code: return ec2_error(req, request_id, ex.code, unicode(ex)) diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py index 5ac69b1a7..560a2d0dd 100644 --- a/nova/api/ec2/cloud.py +++ b/nova/api/ec2/cloud.py @@ -600,7 +600,7 @@ class CloudController(object): group_id=None, **kwargs): if not group_name and not group_id: err = "Not enough parameters, need group_name or group_id" - raise exception.ApiError(_(err)) + raise exception.EC2APIError(_(err)) self.compute_api.ensure_default_security_group(context) notfound = exception.SecurityGroupNotFound if group_name: @@ -626,7 +626,7 @@ class CloudController(object): rulesvalues = self._rule_args_to_dict(context, values) if not rulesvalues: err = "%s Not enough parameters to build a valid rule" - raise exception.ApiError(_(err % rulesvalues)) + raise exception.EC2APIError(_(err % rulesvalues)) for values_for_rule in rulesvalues: values_for_rule['parent_group_id'] = security_group.id @@ -640,7 +640,7 @@ class CloudController(object): context, security_group_id=security_group['id']) return True - raise exception.ApiError(_("No rule for the specified parameters.")) + raise exception.EC2APIError(_("No rule for the specified parameters.")) # TODO(soren): This has only been tested with Boto as the client. # Unfortunately, it seems Boto is using an old API @@ -650,7 +650,7 @@ class CloudController(object): group_id=None, **kwargs): if not group_name and not group_id: err = "Not enough parameters, need group_name or group_id" - raise exception.ApiError(_(err)) + raise exception.EC2APIError(_(err)) self.compute_api.ensure_default_security_group(context) notfound = exception.SecurityGroupNotFound if group_name: @@ -676,13 +676,13 @@ class CloudController(object): rulesvalues = self._rule_args_to_dict(context, values) if not rulesvalues: err = "%s Not enough parameters to build a valid rule" - raise exception.ApiError(_(err % rulesvalues)) + raise exception.EC2APIError(_(err % rulesvalues)) for values_for_rule in rulesvalues: values_for_rule['parent_group_id'] = security_group.id if self._security_group_rule_exists(security_group, values_for_rule): err = '%s - This rule already exists in group' - raise exception.ApiError(_(err) % values_for_rule) + raise exception.EC2APIError(_(err) % values_for_rule) postvalues.append(values_for_rule) for values_for_rule in postvalues: @@ -696,7 +696,7 @@ class CloudController(object): security_group_id=security_group['id']) return True - raise exception.ApiError(_("No rule for the specified parameters.")) + raise exception.EC2APIError(_("No rule for the specified parameters.")) def _get_source_project_id(self, context, source_security_group_owner_id): if source_security_group_owner_id: @@ -735,7 +735,8 @@ class CloudController(object): LOG.audit(_("Create Security Group %s"), group_name, context=context) self.compute_api.ensure_default_security_group(context) if db.security_group_exists(context, context.project_id, group_name): - raise exception.ApiError(_('group %s already exists') % group_name) + msg = _('group %s already exists') + raise exception.EC2APIError(msg % group_name) group = {'user_id': context.user_id, 'project_id': context.project_id, @@ -750,7 +751,7 @@ class CloudController(object): **kwargs): if not group_name and not group_id: err = "Not enough parameters, need group_name or group_id" - raise exception.ApiError(_(err)) + raise exception.EC2APIError(_(err)) notfound = exception.SecurityGroupNotFound if group_name: security_group = db.security_group_get_by_name(context, @@ -906,7 +907,7 @@ class CloudController(object): def describe_instance_attribute(self, context, instance_id, attribute, **kwargs): def _unsupported_attribute(instance, result): - raise exception.ApiError(_('attribute not supported: %s') % + raise exception.EC2APIError(_('attribute not supported: %s') % attribute) def _format_attr_block_device_mapping(instance, result): @@ -962,7 +963,7 @@ class CloudController(object): fn = attribute_formatter.get(attribute) if fn is None: - raise exception.ApiError( + raise exception.EC2APIError( _('attribute not supported: %s') % attribute) ec2_instance_id = instance_id @@ -1225,7 +1226,7 @@ class CloudController(object): raise exception.ImageNotFound(image_id=kwargs['image_id']) if image_state != 'available': - raise exception.ApiError(_('Image must be available')) + raise exception.EC2APIError(_('Image must be available')) (instances, resv_id) = self.compute_api.create(context, instance_type=instance_types.get_instance_type_by_name( @@ -1425,7 +1426,7 @@ class CloudController(object): fn = supported_attributes.get(attribute) if fn is None: - raise exception.ApiError(_('attribute not supported: %s') + raise exception.EC2APIError(_('attribute not supported: %s') % attribute) try: image = self._get_image(context, image_id) @@ -1440,14 +1441,15 @@ class CloudController(object): operation_type, **kwargs): # TODO(devcamcar): Support users and groups other than 'all'. if attribute != 'launchPermission': - raise exception.ApiError(_('attribute not supported: %s') + raise exception.EC2APIError(_('attribute not supported: %s') % attribute) if not 'user_group' in kwargs: - raise exception.ApiError(_('user or group not specified')) + raise exception.EC2APIError(_('user or group not specified')) if len(kwargs['user_group']) != 1 and kwargs['user_group'][0] != 'all': - raise exception.ApiError(_('only group "all" is supported')) + raise exception.EC2APIError(_('only group "all" is supported')) if not operation_type in ['add', 'remove']: - raise exception.ApiError(_('operation_type must be add or remove')) + msg = _('operation_type must be add or remove') + raise exception.EC2APIError(msg) LOG.audit(_("Updating image %s publicity"), image_id, context=context) try: @@ -1504,7 +1506,7 @@ class CloudController(object): # Or is there any better way? timeout = 1 * 60 * 60 * 60 if time.time() > start_time + timeout: - raise exception.ApiError( + raise exception.EC2APIError( _('Couldn\'t stop instance with in %d sec') % timeout) src_image = self._get_image(context, instance['image_ref']) diff --git a/nova/api/openstack/compute/contrib/cloudpipe.py b/nova/api/openstack/compute/contrib/cloudpipe.py index d5a975840..83c4bb47a 100644 --- a/nova/api/openstack/compute/contrib/cloudpipe.py +++ b/nova/api/openstack/compute/contrib/cloudpipe.py @@ -116,7 +116,7 @@ class CloudpipeController(object): except db.NoMoreNetworks: msg = _("Unable to claim IP for VPN instances, ensure it " "isn't running, and try again in a few minutes") - raise exception.ApiError(msg) + raise exception.HTTPBadRequest(explanation=msg) instance = self._get_cloudpipe_for_project(ctxt, proj) return {'instance_id': instance['uuid']} diff --git a/nova/api/openstack/compute/contrib/console_output.py b/nova/api/openstack/compute/contrib/console_output.py index a9db43358..0752c3c70 100644 --- a/nova/api/openstack/compute/contrib/console_output.py +++ b/nova/api/openstack/compute/contrib/console_output.py @@ -54,8 +54,8 @@ class ConsoleOutputController(wsgi.Controller): output = self.compute_api.get_console_output(context, instance, length) - except exception.ApiError, e: - raise webob.exc.HTTPBadRequest(explanation=e.message) + except exception.NotFound: + raise webob.exc.HTTPNotFound(_('Instance not found')) return {'output': output} diff --git a/nova/api/openstack/compute/contrib/consoles.py b/nova/api/openstack/compute/contrib/consoles.py index 7cf757fd1..f308c6717 100644 --- a/nova/api/openstack/compute/contrib/consoles.py +++ b/nova/api/openstack/compute/contrib/consoles.py @@ -54,10 +54,10 @@ class ConsolesController(wsgi.Controller): console_type) except exception.ConsoleTypeInvalid, e: raise webob.exc.HTTPBadRequest(_('Invalid type specification')) - except exception.ApiError, e: - raise webob.exc.HTTPBadRequest(explanation=e.message) except exception.NotAuthorized, e: raise webob.exc.HTTPUnauthorized() + except exception.NotFound: + raise webob.exc.HTTPNotFound(_('Instance not found')) return {'console': {'type': console_type, 'url': output['url']}} diff --git a/nova/api/openstack/compute/contrib/floating_ips.py b/nova/api/openstack/compute/contrib/floating_ips.py index 291d09554..ad74bc8a8 100644 --- a/nova/api/openstack/compute/contrib/floating_ips.py +++ b/nova/api/openstack/compute/contrib/floating_ips.py @@ -204,14 +204,14 @@ class FloatingIPActionController(wsgi.Controller): msg = _("Address not specified") raise webob.exc.HTTPBadRequest(explanation=msg) + instance = self.compute_api.get(context, id) + try: - instance = self.compute_api.get(context, id) self.compute_api.associate_floating_ip(context, instance, address) - except exception.ApiError, e: - raise webob.exc.HTTPBadRequest(explanation=e.message) - except exception.NotAuthorized, e: - raise webob.exc.HTTPUnauthorized() + except exception.FixedIpNotFoundForInstance: + msg = _("No fixed ips associated to instance") + raise webob.exc.HTTPBadRequest(explanation=msg) except rpc.RemoteError: msg = _("Associate floating ip failed") raise webob.exc.HTTPInternalServerError(explanation=msg) diff --git a/nova/api/openstack/compute/contrib/server_start_stop.py b/nova/api/openstack/compute/contrib/server_start_stop.py index 9bf71fdc5..5729a2b30 100644 --- a/nova/api/openstack/compute/contrib/server_start_stop.py +++ b/nova/api/openstack/compute/contrib/server_start_stop.py @@ -31,35 +31,29 @@ class ServerStartStopActionController(wsgi.Controller): super(ServerStartStopActionController, self).__init__(*args, **kwargs) self.compute_api = compute.API() + def _get_instance(self, context, instance_uuid): + try: + return self.compute_api.get(context, instance_uuid) + except exception.NotFound: + msg = _("Instance not found") + raise webob.exc.HTTPNotFound(explanation=msg) + @wsgi.action('os-start') def _start_server(self, req, id, body): """Start an instance. """ context = req.environ['nova.context'] - - try: - LOG.debug(_("start instance %r"), id) - instance = self.compute_api.get(context, id) - self.compute_api.start(context, instance) - except exception.ApiError, e: - raise webob.exc.HTTPBadRequest(explanation=e.message) - except exception.NotAuthorized, e: - raise webob.exc.HTTPUnauthorized() + LOG.debug(_("start instance %r"), id) + instance = self._get_instance(context, id) + self.compute_api.start(context, instance) return webob.Response(status_int=202) @wsgi.action('os-stop') def _stop_server(self, req, id, body): """Stop an instance.""" context = req.environ['nova.context'] - - try: - LOG.debug(_("stop instance %r"), id) - instance = self.compute_api.get(context, id) - self.compute_api.stop(context, instance) - except exception.ApiError, e: - raise webob.exc.HTTPBadRequest(explanation=e.message) - except exception.NotAuthorized, e: - raise webob.exc.HTTPUnauthorized() - + LOG.debug(_("stop instance %r"), id) + instance = self._get_instance(context, id) + self.compute_api.stop(context, instance) return webob.Response(status_int=202) diff --git a/nova/api/openstack/compute/contrib/volumetypes.py b/nova/api/openstack/compute/contrib/volumetypes.py index cbc205ea7..af68b3adb 100644 --- a/nova/api/openstack/compute/contrib/volumetypes.py +++ b/nova/api/openstack/compute/contrib/volumetypes.py @@ -100,7 +100,7 @@ class VolumeTypesController(object): try: vol_type = volume_types.get_volume_type(context, id) - except exception.NotFound or exception.ApiError: + except exception.NotFound: raise exc.HTTPNotFound() return {'volume_type': vol_type} diff --git a/nova/api/openstack/compute/server_metadata.py b/nova/api/openstack/compute/server_metadata.py index 52a90f96e..cd7ebb545 100644 --- a/nova/api/openstack/compute/server_metadata.py +++ b/nova/api/openstack/compute/server_metadata.py @@ -165,7 +165,7 @@ class Controller(object): def _handle_quota_error(self, error): """Reraise quota errors as api-specific http exceptions.""" - if error.code == "MetadataLimitExceeded": + if error.kwargs['code'] == "MetadataLimitExceeded": raise exc.HTTPRequestEntityTooLarge(explanation=error.message, headers={'Retry-After': 0}) raise error diff --git a/nova/api/openstack/compute/servers.py b/nova/api/openstack/compute/servers.py index b55e8c6a3..a9d3a93a2 100644 --- a/nova/api/openstack/compute/servers.py +++ b/nova/api/openstack/compute/servers.py @@ -496,12 +496,8 @@ class Controller(wsgi.Controller): "InstanceLimitExceeded": error.message, } - expl = code_mappings.get(error.code) - if expl: - raise exc.HTTPRequestEntityTooLarge(explanation=expl, - headers={'Retry-After': 0}) - # if the original error is okay, just reraise it - raise exc.HTTPRequestEntityTooLarge(explanation=error.msg, + expl = code_mappings.get(error.kwargs['code'], error.message) + raise exc.HTTPRequestEntityTooLarge(explanation=expl, headers={'Retry-After': 0}) def _validate_server_name(self, value): diff --git a/nova/api/openstack/volume/types.py b/nova/api/openstack/volume/types.py index 609a6cfc9..97fb52497 100644 --- a/nova/api/openstack/volume/types.py +++ b/nova/api/openstack/volume/types.py @@ -64,7 +64,7 @@ class VolumeTypesController(object): try: vol_type = volume_types.get_volume_type(context, id) - except exception.NotFound or exception.ApiError: + except exception.NotFound: raise exc.HTTPNotFound() return {'volume_type': vol_type} |