From 922aefc0bbdccabb89cd932270c53bdd379061d1 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Sun, 30 Dec 2012 13:50:10 +0000 Subject: [PATCH] Exceptions cleanup Ensure that the quantum exceptions in FAULT_MAP are grouped together (this will save endless bug fixes when the server will return 500 instead of 4xx) Change-Id: I89581e1b6b4af3eb1803d6226686adf0b576d1e7 --- quantum/api/v2/base.py | 19 ++------ quantum/common/exceptions.py | 45 +++++++++---------- quantum/extensions/securitygroup.py | 2 +- quantum/plugins/ryu/db/api_v2.py | 4 +- .../unit/test_extension_security_group.py | 4 +- 5 files changed, 30 insertions(+), 44 deletions(-) diff --git a/quantum/api/v2/base.py b/quantum/api/v2/base.py index 8a9eb58e6..d9ab54006 100644 --- a/quantum/api/v2/base.py +++ b/quantum/api/v2/base.py @@ -29,27 +29,14 @@ LOG = logging.getLogger(__name__) XML_NS_V20 = 'http://openstack.org/quantum/api/v2.0' FAULT_MAP = {exceptions.NotFound: webob.exc.HTTPNotFound, - exceptions.InUse: webob.exc.HTTPConflict, exceptions.Conflict: webob.exc.HTTPConflict, + exceptions.InUse: webob.exc.HTTPConflict, exceptions.BadRequest: webob.exc.HTTPBadRequest, - exceptions.ResourceExhausted: webob.exc.HTTPServiceUnavailable, - exceptions.MacAddressGenerationFailure: - webob.exc.HTTPServiceUnavailable, - exceptions.StateInvalid: webob.exc.HTTPBadRequest, - exceptions.InvalidInput: webob.exc.HTTPBadRequest, - exceptions.OverlappingAllocationPools: webob.exc.HTTPConflict, - exceptions.OutOfBoundsAllocationPool: webob.exc.HTTPBadRequest, - exceptions.InvalidAllocationPool: webob.exc.HTTPBadRequest, - exceptions.InvalidSharedSetting: webob.exc.HTTPConflict, - exceptions.HostRoutesExhausted: webob.exc.HTTPBadRequest, - exceptions.DNSNameServersExhausted: webob.exc.HTTPBadRequest, - # Some plugins enforce policies as well - exceptions.PolicyNotAuthorized: webob.exc.HTTPForbidden, + exceptions.ServiceUnavailable: webob.exc.HTTPServiceUnavailable, + exceptions.NotAuthorized: webob.exc.HTTPForbidden, netaddr.AddrFormatError: webob.exc.HTTPBadRequest, AttributeError: webob.exc.HTTPBadRequest, ValueError: webob.exc.HTTPBadRequest, - exceptions.IpAddressGenerationFailure: webob.exc.HTTPConflict, - exceptions.OverQuota: webob.exc.HTTPConflict, } QUOTAS = quota.QUOTAS diff --git a/quantum/common/exceptions.py b/quantum/common/exceptions.py index ae9c9e3b5..cb6a8da40 100644 --- a/quantum/common/exceptions.py +++ b/quantum/common/exceptions.py @@ -50,6 +50,10 @@ class NotAuthorized(QuantumException): message = _("Not authorized.") +class ServiceUnavailable(QuantumException): + message = _("The service is unailable") + + class AdminRequired(NotAuthorized): message = _("User does not have admin privileges: %(reason)s") @@ -79,7 +83,7 @@ class PolicyNotFound(NotFound): message = _("Policy configuration policy.json could not be found") -class StateInvalid(QuantumException): +class StateInvalid(BadRequest): message = _("Unsupported port state: %(port_state)s") @@ -108,13 +112,13 @@ class MacAddressInUse(InUse): "The mac address %(mac)s is in use.") -class HostRoutesExhausted(QuantumException): +class HostRoutesExhausted(BadRequest): # NOTE(xchenum): probably make sense to use quota exceeded exception? message = _("Unable to complete operation for %(subnet_id)s. " "The number of host routes exceeds the limit %(quota)s.") -class DNSNameServersExhausted(QuantumException): +class DNSNameServersExhausted(BadRequest): # NOTE(xchenum): probably make sense to use quota exceeded exception? message = _("Unable to complete operation for %(subnet_id)s. " "The number of DNS nameservers exceeds the limit %(quota)s.") @@ -141,11 +145,11 @@ class TunnelIdInUse(InUse): "The tunnel ID %(tunnel_id)s is in use.") -class TenantNetworksDisabled(QuantumException): +class TenantNetworksDisabled(ServiceUnavailable): message = _("Tenant network creation is not enabled.") -class ResourceExhausted(QuantumException): +class ResourceExhausted(ServiceUnavailable): pass @@ -154,7 +158,7 @@ class NoNetworkAvailable(ResourceExhausted): "No tenant network is available for allocation.") -class AlreadyAttached(QuantumException): +class AlreadyAttached(Conflict): message = _("Unable to plug the attachment %(att_id)s into port " "%(port_id)s for network %(net_id)s. The attachment is " "already plugged into port %(att_port_id)s") @@ -165,7 +169,7 @@ class SubnetMismatchForPort(Conflict): "the requested subnet %(subnet_id)s") -class MalformedRequestBody(QuantumException): +class MalformedRequestBody(BadRequest): message = _("Malformed request body: %(reason)s") @@ -173,7 +177,7 @@ class Invalid(Error): pass -class InvalidInput(QuantumException): +class InvalidInput(BadRequest): message = _("Invalid input for operation: %(error_message)s.") @@ -181,16 +185,16 @@ class InvalidContentType(Invalid): message = _("Invalid content type %(content_type)s.") -class InvalidAllocationPool(QuantumException): +class InvalidAllocationPool(BadRequest): message = _("The allocation pool %(pool)s is not valid.") -class OverlappingAllocationPools(QuantumException): +class OverlappingAllocationPools(Conflict): message = _("Found overlapping allocation pools:" "%(pool_1)s %(pool_2)s for subnet %(subnet_cidr)s.") -class OutOfBoundsAllocationPool(QuantumException): +class OutOfBoundsAllocationPool(BadRequest): message = _("The allocation pool %(pool)s spans " "beyond the subnet cidr %(subnet_cidr)s.") @@ -199,16 +203,11 @@ class NotImplementedError(Error): pass -class FixedIPNotAvailable(QuantumException): - message = _("Fixed IP (%(ip)s) unavailable for network " - "%(network_uuid)s") - - -class MacAddressGenerationFailure(QuantumException): +class MacAddressGenerationFailure(ServiceUnavailable): message = _("Unable to generate unique mac on network %(net_id)s.") -class IpAddressGenerationFailure(QuantumException): +class IpAddressGenerationFailure(Conflict): message = _("No more IP addresses available on network %(net_id)s.") @@ -224,25 +223,25 @@ class SudoRequired(QuantumException): message = _("Sudo priviledge is required to run this command.") -class QuotaResourceUnknown(QuantumException): +class QuotaResourceUnknown(NotFound): message = _("Unknown quota resources %(unknown)s.") -class OverQuota(QuantumException): +class OverQuota(Conflict): message = _("Quota exceeded for resources: %(overs)s") -class InvalidQuotaValue(QuantumException): +class InvalidQuotaValue(Conflict): message = _("Change would make usage less than 0 for the following " "resources: %(unders)s") -class InvalidSharedSetting(QuantumException): +class InvalidSharedSetting(Conflict): message = _("Unable to reconfigure sharing settings for network " "%(network)s. Multiple tenants are using it") -class InvalidExtenstionEnv(QuantumException): +class InvalidExtenstionEnv(BadRequest): message = _("Invalid extension environment: %(reason)s") diff --git a/quantum/extensions/securitygroup.py b/quantum/extensions/securitygroup.py index e47c155ff..a887da429 100644 --- a/quantum/extensions/securitygroup.py +++ b/quantum/extensions/securitygroup.py @@ -104,7 +104,7 @@ class SecurityGroupNotProxyMode(qexception.InUse): message = _("Recieve external id and not in proxy mode") -class SecurityGroupProxyModeNotAdmin(qexception.InvalidExtenstionEnv): +class SecurityGroupProxyModeNotAdmin(qexception.NotAuthorized): message = _("In Proxy Mode and not from admin") diff --git a/quantum/plugins/ryu/db/api_v2.py b/quantum/plugins/ryu/db/api_v2.py index 18dd46555..ca7fd1a03 100644 --- a/quantum/plugins/ryu/db/api_v2.py +++ b/quantum/plugins/ryu/db/api_v2.py @@ -171,8 +171,8 @@ class TunnelKey(object): count += 1 if count > self._TRANSACTION_RETRY_MAX: # if this happens too often, increase _TRANSACTION_RETRY_MAX - LOG.warn(_("Transaction retry reaches to %d. " - "abandan to allocate tunnel key."), count) + LOG.warn(_("Transaction retry exhausted (%d). " + "Abandoned tunnel key allocation."), count) raise q_exc.ResourceExhausted() return new_key diff --git a/quantum/tests/unit/test_extension_security_group.py b/quantum/tests/unit/test_extension_security_group.py index 9d3fa756d..b71915996 100644 --- a/quantum/tests/unit/test_extension_security_group.py +++ b/quantum/tests/unit/test_extension_security_group.py @@ -240,7 +240,7 @@ class TestSecurityGroups(SecurityGroupDBTestCase): tenant_id='bad_tenant', set_context=True) self.deserialize('json', res) - self.assertEqual(res.status_int, 500) + self.assertEqual(res.status_int, 403) def test_create_security_group_no_external_id_proxy_mode(self): cfg.CONF.SECURITYGROUP.proxy_mode = True @@ -463,7 +463,7 @@ class TestSecurityGroups(SecurityGroupDBTestCase): tenant_id='bad_tenant', set_context=True) self.deserialize('json', res) - self.assertEqual(res.status_int, 500) + self.assertEqual(res.status_int, 403) def test_create_security_group_rule_bad_tenant_source_group_id(self): with self.security_group() as sg: -- 2.45.2