From 9f874a9ca05721228549f0be62a348cd1d1e8d90 Mon Sep 17 00:00:00 2001 From: poojajadhav Date: Mon, 23 Jan 2017 19:20:33 +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 202, 204, 300 and 412 under api/v3 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: I60d8d99827bfa1a6fc5b6dbb67d1d1fb3a3b95fe --- cinder/api/v3/consistencygroups.py | 3 ++- cinder/api/v3/group_snapshots.py | 7 ++++--- cinder/api/v3/group_specs.py | 5 +++-- cinder/api/v3/group_types.py | 5 +++-- cinder/api/v3/groups.py | 11 ++++++----- cinder/api/v3/messages.py | 3 ++- cinder/api/v3/snapshot_manage.py | 4 +++- cinder/api/v3/volume_manage.py | 4 +++- cinder/api/v3/volume_metadata.py | 5 +++-- cinder/api/v3/volumes.py | 3 ++- cinder/api/versions.py | 4 +++- 11 files changed, 34 insertions(+), 20 deletions(-) diff --git a/cinder/api/v3/consistencygroups.py b/cinder/api/v3/consistencygroups.py index b74d290aab1..b0e974e268e 100644 --- a/cinder/api/v3/consistencygroups.py +++ b/cinder/api/v3/consistencygroups.py @@ -14,6 +14,7 @@ """The consistencygroups V3 api.""" from oslo_log import log as logging +from six.moves import http_client import webob from webob import exc @@ -82,7 +83,7 @@ class ConsistencyGroupsController(cg_v2.ConsistencyGroupsController): remove_volumes) self._update(context, id, name, description, add_volumes, remove_volumes, allow_empty) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) def create_resource(): diff --git a/cinder/api/v3/group_snapshots.py b/cinder/api/v3/group_snapshots.py index 09f38d87d00..56487fdae8c 100644 --- a/cinder/api/v3/group_snapshots.py +++ b/cinder/api/v3/group_snapshots.py @@ -17,6 +17,7 @@ from oslo_log import log as logging import six +from six.moves import http_client import webob from webob import exc @@ -90,7 +91,7 @@ class GroupSnapshotsController(wsgi.Controller): LOG.exception(msg) raise exc.HTTPBadRequest(explanation=msg) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) @wsgi.Controller.api_version(GROUP_SNAPSHOT_API_VERSION) def index(self, req): @@ -130,7 +131,7 @@ class GroupSnapshotsController(wsgi.Controller): return {'group_snapshots': new_group_snapshots} @wsgi.Controller.api_version(GROUP_SNAPSHOT_API_VERSION) - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): """Create a new group_snapshot.""" LOG.debug('Creating new group_snapshot %s', body) @@ -208,7 +209,7 @@ class GroupSnapshotsController(wsgi.Controller): {'error_message': error.msg, 'id': id}) raise exc.HTTPBadRequest(explanation=error.msg) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) def create_resource(): diff --git a/cinder/api/v3/group_specs.py b/cinder/api/v3/group_specs.py index 5fdbbc592f8..ee89cfe0815 100644 --- a/cinder/api/v3/group_specs.py +++ b/cinder/api/v3/group_specs.py @@ -14,6 +14,7 @@ """The group types specs controller""" +from six.moves import http_client import webob from cinder.api import common @@ -59,7 +60,7 @@ class GroupTypeSpecsController(wsgi.Controller): return self._get_group_specs(context, group_type_id) @wsgi.Controller.api_version('3.11') - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, group_type_id, body=None): context = req.environ['cinder.context'] self._check_policy(context) @@ -140,7 +141,7 @@ class GroupTypeSpecsController(wsgi.Controller): notifier.info(context, 'group_type_specs.delete', notifier_info) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) def _check_key_names(self, keys): if not common.validate_key_names(keys): diff --git a/cinder/api/v3/group_types.py b/cinder/api/v3/group_types.py index e916abe8554..30e3fc9972b 100644 --- a/cinder/api/v3/group_types.py +++ b/cinder/api/v3/group_types.py @@ -16,6 +16,7 @@ from oslo_utils import strutils import six +from six.moves import http_client import webob from webob import exc @@ -53,7 +54,7 @@ class GroupTypesController(wsgi.Controller): rpc.get_notifier('groupType').info(context, method, payload) @wsgi.Controller.api_version('3.11') - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): """Creates a new group type.""" context = req.environ['cinder.context'] @@ -183,7 +184,7 @@ class GroupTypesController(wsgi.Controller): context, 'group_type.delete', err, id=id) raise webob.exc.HTTPNotFound(explanation=err.msg) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) @wsgi.Controller.api_version('3.11') def index(self, req): diff --git a/cinder/api/v3/groups.py b/cinder/api/v3/groups.py index 84ca342394e..ad681df3e74 100644 --- a/cinder/api/v3/groups.py +++ b/cinder/api/v3/groups.py @@ -17,6 +17,7 @@ from oslo_log import log as logging from oslo_utils import strutils from oslo_utils import uuidutils +from six.moves import http_client import webob from webob import exc @@ -105,7 +106,7 @@ class GroupsController(wsgi.Controller): {'error_message': error.msg, 'id': id}) raise exc.HTTPBadRequest(explanation=error.msg) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) @wsgi.Controller.api_version(GROUP_API_VERSION) @wsgi.action("delete") @@ -146,7 +147,7 @@ class GroupsController(wsgi.Controller): except exception.InvalidGroup as error: raise exc.HTTPBadRequest(explanation=error.msg) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) @wsgi.Controller.api_version(GROUP_API_VERSION) def index(self, req): @@ -188,7 +189,7 @@ class GroupsController(wsgi.Controller): return groups @wsgi.Controller.api_version(GROUP_API_VERSION) - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): """Create a new group.""" LOG.debug('Creating new group %s', body) @@ -235,7 +236,7 @@ class GroupsController(wsgi.Controller): @wsgi.Controller.api_version(GROUP_CREATE_FROM_SRC_API_VERSION) @wsgi.action("create-from-src") - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create_from_src(self, req, body): """Create a new group from a source. @@ -361,7 +362,7 @@ class GroupsController(wsgi.Controller): except exception.InvalidGroup as error: raise exc.HTTPBadRequest(explanation=error.msg) - return webob.Response(status_int=202) + return webob.Response(status_int=http_client.ACCEPTED) def create_resource(): diff --git a/cinder/api/v3/messages.py b/cinder/api/v3/messages.py index 00b43272bc8..72e8e0eeb77 100644 --- a/cinder/api/v3/messages.py +++ b/cinder/api/v3/messages.py @@ -13,6 +13,7 @@ """The messages API.""" +from six.moves import http_client import webob from cinder.api import common @@ -73,7 +74,7 @@ class MessagesController(wsgi.Controller): check_policy(context, 'delete', message) self.message_api.delete(context, message) - return webob.Response(status_int=204) + return webob.Response(status_int=http_client.NO_CONTENT) @wsgi.Controller.api_version(MESSAGES_BASE_MICRO_VERSION) def index(self, req): diff --git a/cinder/api/v3/snapshot_manage.py b/cinder/api/v3/snapshot_manage.py index 4c842e32158..ae9a54d60bf 100644 --- a/cinder/api/v3/snapshot_manage.py +++ b/cinder/api/v3/snapshot_manage.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +from six.moves import http_client + from cinder.api.contrib import snapshot_manage as snapshot_manage_v2 from cinder.api.openstack import wsgi from cinder.api.v3 import resource_common_manage as common @@ -23,7 +25,7 @@ class SnapshotManageController(common.ManageResource, super(SnapshotManageController, self).__init__(*args, **kwargs) self._set_resource_type('snapshot') - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): self._ensure_min_version(req, "3.8") return super(SnapshotManageController, self).create(req, body) diff --git a/cinder/api/v3/volume_manage.py b/cinder/api/v3/volume_manage.py index d494c7576ef..4b2c5677e91 100644 --- a/cinder/api/v3/volume_manage.py +++ b/cinder/api/v3/volume_manage.py @@ -12,6 +12,8 @@ # License for the specific language governing permissions and limitations # under the License. +from six.moves import http_client + from cinder.api.contrib import volume_manage as volume_manage_v2 from cinder.api.openstack import wsgi from cinder.api.v3 import resource_common_manage as common @@ -23,7 +25,7 @@ class VolumeManageController(common.ManageResource, super(VolumeManageController, self).__init__(*args, **kwargs) self._set_resource_type('volume') - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): self._ensure_min_version(req, "3.8") return super(VolumeManageController, self).create(req, body) diff --git a/cinder/api/v3/volume_metadata.py b/cinder/api/v3/volume_metadata.py index d248618b41c..37cd7787c9d 100644 --- a/cinder/api/v3/volume_metadata.py +++ b/cinder/api/v3/volume_metadata.py @@ -19,6 +19,7 @@ import hashlib from oslo_serialization import jsonutils import six +from six.moves import http_client import webob from cinder.api.openstack import wsgi @@ -63,7 +64,7 @@ class Controller(volume_meta_v2.Controller): def update(self, req, volume_id, id, body): self._ensure_min_version(req, METADATA_MICRO_VERSION) if not self._validate_etag(req, volume_id): - return webob.Response(status_int=412) + return webob.Response(status_int=http_client.PRECONDITION_FAILED) return super(Controller, self).update(req, volume_id, id, body) @@ -71,7 +72,7 @@ class Controller(volume_meta_v2.Controller): def update_all(self, req, volume_id, body): self._ensure_min_version(req, METADATA_MICRO_VERSION) if not self._validate_etag(req, volume_id): - return webob.Response(status_int=412) + return webob.Response(status_int=http_client.PRECONDITION_FAILED) return super(Controller, self).update_all(req, volume_id, body) diff --git a/cinder/api/v3/volumes.py b/cinder/api/v3/volumes.py index dec83abf15b..7b7573689f3 100644 --- a/cinder/api/v3/volumes.py +++ b/cinder/api/v3/volumes.py @@ -15,6 +15,7 @@ from oslo_log import log as logging from oslo_utils import uuidutils +from six.moves import http_client import webob from webob import exc @@ -146,7 +147,7 @@ class VolumeController(volumes_v2.VolumeController): volumes = self.volume_api.get_volume_summary(context, filters=filters) return view_builder_v3.quick_summary(volumes[0], int(volumes[1])) - @wsgi.response(202) + @wsgi.response(http_client.ACCEPTED) def create(self, req, body): """Creates a new volume. diff --git a/cinder/api/versions.py b/cinder/api/versions.py index 01d31b310c9..ef7e15aaf57 100644 --- a/cinder/api/versions.py +++ b/cinder/api/versions.py @@ -17,6 +17,8 @@ import copy +from six.moves import http_client + from cinder.api import extensions from cinder.api import openstack from cinder.api.openstack import api_version_request @@ -118,7 +120,7 @@ class VersionsController(wsgi.Controller): # /v1, /v2, or /v3 in the URL will lead to this unversioned # method, which should always return info about all # available versions. - @wsgi.response(300) + @wsgi.response(http_client.MULTIPLE_CHOICES) def all(self, req): """Return all known versions.""" builder = views_versions.get_view_builder(req)