From 92980e4972356f16389539b4cc926d7b4ec1dc3c Mon Sep 17 00:00:00 2001 From: poojajadhav Date: Wed, 4 Jan 2017 15:59:44 +0530 Subject: [PATCH] Extracted HTTP response codes to constants There are several places in the source code where HTTP response codes are used as numeric values. Status codes 200, 202, 204, 300, 400, 401, 403, 404, 405, 409, 413, 415, 500, 501, 503 under api/v1 and api/v2 are replaced with symbolic constants from six.moves.http_client thus improves code readability. More patches will be submitted to address other status codes. Partial-Bug: #1520159 Change-Id: I7c61122a6b043d7d238bea95ef39d8fa97817df4 --- cinder/api/middleware/auth.py | 3 ++- cinder/api/middleware/fault.py | 5 +++-- cinder/api/openstack/wsgi.py | 25 +++++++++++++------------ cinder/api/v1/snapshots.py | 4 +++- cinder/api/v1/volumes.py | 3 ++- cinder/api/v2/limits.py | 2 +- cinder/api/v2/snapshot_metadata.py | 3 ++- cinder/api/v2/snapshots.py | 5 +++-- cinder/api/v2/volume_metadata.py | 3 ++- cinder/api/v2/volumes.py | 5 +++-- 10 files changed, 34 insertions(+), 24 deletions(-) diff --git a/cinder/api/middleware/auth.py b/cinder/api/middleware/auth.py index d95e481b83d..8d145701d89 100644 --- a/cinder/api/middleware/auth.py +++ b/cinder/api/middleware/auth.py @@ -24,6 +24,7 @@ from oslo_config import cfg from oslo_log import log as logging from oslo_middleware import request_id from oslo_serialization import jsonutils +from six.moves import http_client import webob.dec import webob.exc @@ -129,7 +130,7 @@ class NoAuthMiddleware(base_wsgi.Middleware): res.headers['X-Auth-Token'] = '%s:%s' % (user_id, project_id) res.headers['X-Server-Management-Url'] = os_url res.content_type = 'text/plain' - res.status = '204' + res.status_int = http_client.NO_CONTENT return res token = req.headers['X-Auth-Token'] diff --git a/cinder/api/middleware/fault.py b/cinder/api/middleware/fault.py index d2158b47b34..e25de79569b 100644 --- a/cinder/api/middleware/fault.py +++ b/cinder/api/middleware/fault.py @@ -16,6 +16,7 @@ from oslo_log import log as logging import six +from six.moves import http_client import webob.dec import webob.exc @@ -49,9 +50,9 @@ class FaultWrapper(base_wsgi.Middleware): 'error': inner}) safe = getattr(inner, 'safe', False) headers = getattr(inner, 'headers', None) - status = getattr(inner, 'code', 500) + status = getattr(inner, 'code', http_client.INTERNAL_SERVER_ERROR) if status is None: - status = 500 + status = http_client.INTERNAL_SERVER_ERROR msg_dict = dict(url=req.url, status=status) LOG.info("%(url)s returned with HTTP %(status)d", msg_dict) diff --git a/cinder/api/openstack/wsgi.py b/cinder/api/openstack/wsgi.py index 2a0040a068d..f0a5f78017c 100644 --- a/cinder/api/openstack/wsgi.py +++ b/cinder/api/openstack/wsgi.py @@ -25,6 +25,7 @@ from oslo_utils import encodeutils from oslo_utils import excutils from oslo_utils import strutils import six +from six.moves import http_client import webob import webob.exc @@ -441,7 +442,7 @@ class ResponseObject(object): self.obj = obj self.serializers = serializers - self._default_code = 200 + self._default_code = http_client.OK self._code = code self._headers = headers or {} self.serializer = None @@ -1316,16 +1317,16 @@ class Controller(object): class Fault(webob.exc.HTTPException): """Wrap webob.exc.HTTPException to provide API friendly response.""" - _fault_names = {400: "badRequest", - 401: "unauthorized", - 403: "forbidden", - 404: "itemNotFound", - 405: "badMethod", - 409: "conflictingRequest", - 413: "overLimit", - 415: "badMediaType", - 501: "notImplemented", - 503: "serviceUnavailable"} + _fault_names = {http_client.BAD_REQUEST: "badRequest", + http_client.UNAUTHORIZED: "unauthorized", + http_client.FORBIDDEN: "forbidden", + http_client.NOT_FOUND: "itemNotFound", + http_client.METHOD_NOT_ALLOWED: "badMethod", + http_client.CONFLICT: "conflictingRequest", + http_client.REQUEST_ENTITY_TOO_LARGE: "overLimit", + http_client.UNSUPPORTED_MEDIA_TYPE: "badMediaType", + http_client.NOT_IMPLEMENTED: "notImplemented", + http_client.SERVICE_UNAVAILABLE: "serviceUnavailable"} def __init__(self, exception): """Create a Fault for the given webob.exc.exception.""" @@ -1344,7 +1345,7 @@ class Fault(webob.exc.HTTPException): fault_name: { 'code': code, 'message': i18n.translate(explanation, locale)}} - if code == 413: + if code == http_client.REQUEST_ENTITY_TOO_LARGE: retry = self.wrapped_exc.headers.get('Retry-After', None) if retry: fault_data[fault_name]['retryAfter'] = retry diff --git a/cinder/api/v1/snapshots.py b/cinder/api/v1/snapshots.py index 4fb9358729c..76c44165f8b 100644 --- a/cinder/api/v1/snapshots.py +++ b/cinder/api/v1/snapshots.py @@ -15,6 +15,8 @@ """The volumes snapshots api.""" + +from six.moves import http_client from webob import exc from cinder.api.openstack import wsgi @@ -70,7 +72,7 @@ class SnapshotsController(snapshots_v2.SnapshotsController): super(SnapshotsController, self).detail( _update_search_opts(req))) - @wsgi.response(200) + @wsgi.response(http_client.OK) def create(self, req, body): """Creates a new snapshot.""" if (body is None or not body.get('snapshot') or diff --git a/cinder/api/v1/volumes.py b/cinder/api/v1/volumes.py index c94ad689b2c..61dea6698ef 100644 --- a/cinder/api/v1/volumes.py +++ b/cinder/api/v1/volumes.py @@ -16,6 +16,7 @@ """The volumes api.""" from oslo_log import log as logging +from six.moves import http_client from webob import exc from cinder.api.openstack import wsgi @@ -102,7 +103,7 @@ class VolumeController(volumes_v2.VolumeController): return _volume_v2_to_v1( super(VolumeController, self).detail(req)) - @wsgi.response(200) + @wsgi.response(http_client.OK) def create(self, req, body): """Creates a new volume.""" if (body is None or not body.get('volume') or diff --git a/cinder/api/v2/limits.py b/cinder/api/v2/limits.py index 2578acb55cc..e306fd5dd2a 100644 --- a/cinder/api/v2/limits.py +++ b/cinder/api/v2/limits.py @@ -415,7 +415,7 @@ class WsgiLimiterProxy(object): resp = conn.getresponse() - if 200 >= resp.status < 300: + if http_client.OK >= resp.status < http_client.MULTIPLE_CHOICES: return None, None return resp.getheader("X-Wait-Seconds"), resp.read() or None diff --git a/cinder/api/v2/snapshot_metadata.py b/cinder/api/v2/snapshot_metadata.py index 6e94c3f309d..84c61be3a70 100644 --- a/cinder/api/v2/snapshot_metadata.py +++ b/cinder/api/v2/snapshot_metadata.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +from six.moves import http_client import webob from webob import exc @@ -128,7 +129,7 @@ class Controller(wsgi.Controller): # Not found exception will be handled at the wsgi level snapshot = self.volume_api.get_snapshot(context, snapshot_id) self.volume_api.delete_snapshot_metadata(context, snapshot, id) - return webob.Response(status_int=200) + return webob.Response(status_int=http_client.OK) def create_resource(): diff --git a/cinder/api/v2/snapshots.py b/cinder/api/v2/snapshots.py index 467c911a408..903096a8478 100644 --- a/cinder/api/v2/snapshots.py +++ b/cinder/api/v2/snapshots.py @@ -18,6 +18,7 @@ from oslo_log import log as logging from oslo_utils import encodeutils from oslo_utils import strutils +from six.moves import http_client import webob from webob import exc @@ -64,7 +65,7 @@ class SnapshotsController(wsgi.Controller): snapshot = self.volume_api.get_snapshot(context, id) self.volume_api.delete_snapshot(context, snapshot) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) def index(self, req): """Returns a summary list of snapshots.""" @@ -108,7 +109,7 @@ class SnapshotsController(wsgi.Controller): snapshots = self._view_builder.summary_list(req, snapshots.objects) return snapshots - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): """Creates a new snapshot.""" kwargs = {} diff --git a/cinder/api/v2/volume_metadata.py b/cinder/api/v2/volume_metadata.py index 37c3e52d5c1..5799494f65b 100644 --- a/cinder/api/v2/volume_metadata.py +++ b/cinder/api/v2/volume_metadata.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +from six.moves import http_client import webob from cinder.api import common @@ -136,7 +137,7 @@ class Controller(wsgi.Controller): volume, id, meta_type=common.METADATA_TYPES.user) - return webob.Response(status_int=200) + return webob.Response(status_int=http_client.OK) def create_resource(): diff --git a/cinder/api/v2/volumes.py b/cinder/api/v2/volumes.py index c09ad0c1966..0b6d4ff5f48 100644 --- a/cinder/api/v2/volumes.py +++ b/cinder/api/v2/volumes.py @@ -19,6 +19,7 @@ from oslo_config import cfg from oslo_log import log as logging from oslo_utils import uuidutils +from six.moves import http_client import webob from webob import exc @@ -75,7 +76,7 @@ class VolumeController(wsgi.Controller): # Not found exception will be handled at the wsgi level volume = self.volume_api.get(context, id) self.volume_api.delete(context, volume, cascade=cascade) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) def index(self, req): """Returns a summary list of volumes.""" @@ -171,7 +172,7 @@ class VolumeController(wsgi.Controller): "access requested image.") raise exc.HTTPBadRequest(explanation=msg) - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): """Creates a new volume.""" self.assert_valid_body(body, 'volume')