Merge "Remove API check is_valid_body"

This commit is contained in:
Jenkins 2017-10-10 04:24:07 +00:00 committed by Gerrit Code Review
commit 512ed5218b
6 changed files with 28 additions and 64 deletions

View File

@ -59,10 +59,7 @@ class ConsistencyGroupsController(wsgi.Controller):
context = req.environ['cinder.context']
force = False
if body:
if not self.is_valid_body(body, 'consistencygroup'):
msg = _("Missing required element 'consistencygroup' in "
"request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'consistencygroup')
cg_body = body['consistencygroup']
try:

View File

@ -19,7 +19,6 @@ from cinder.api import extensions
from cinder.api.openstack import wsgi
from cinder import db
from cinder import exception
from cinder.i18n import _
from cinder import quota
from cinder import utils
@ -60,10 +59,7 @@ class QuotaClassSetsController(wsgi.Controller):
min_length=1, max_length=255)
quota_class = id
if not self.is_valid_body(body, 'quota_class_set'):
msg = (_("Missing required element quota_class_set"
" in request body."))
raise webob.exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'quota_class_set')
for key, value in body['quota_class_set'].items():
if key in QUOTAS or key in GROUP_QUOTAS:

View File

@ -88,9 +88,7 @@ class SnapshotManageController(wsgi.Controller):
context = req.environ['cinder.context']
authorize_manage(context)
if not self.is_valid_body(body, 'snapshot'):
msg = _("Missing required element snapshot in request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'snapshot')
snapshot = body['snapshot']

View File

@ -1233,9 +1233,12 @@ class Controller(object):
return decorator
@staticmethod
def is_valid_body(body, entity_name):
def assert_valid_body(body, entity_name):
fail_msg = _(
"Missing required element '%s' in request body.") % entity_name
if not (body and entity_name in body):
return False
raise webob.exc.HTTPBadRequest(explanation=fail_msg)
def is_dict(d):
try:
@ -1245,22 +1248,7 @@ class Controller(object):
return False
if not is_dict(body[entity_name]):
return False
return True
@staticmethod
def assert_valid_body(body, entity_name):
# NOTE: After v1 api is deprecated need to merge 'is_valid_body' and
# 'assert_valid_body' in to one method. Right now it is not
# possible to modify 'is_valid_body' to raise exception because
# in case of V1 api when 'is_valid_body' return False,
# 'HTTPUnprocessableEntity' exception is getting raised and in
# V2 api 'HTTPBadRequest' exception is getting raised.
if not Controller.is_valid_body(body, entity_name):
raise webob.exc.HTTPBadRequest(
explanation=_("Missing required element '%s' in "
"request body.") % entity_name)
raise webob.exc.HTTPBadRequest(explanation=fail_msg)
@staticmethod
def validate_name_and_description(body):

View File

@ -117,10 +117,7 @@ class GroupsController(wsgi.Controller):
context = req.environ['cinder.context']
del_vol = False
if body:
if not self.is_valid_body(body, 'delete'):
msg = _("Missing required element 'delete' in "
"request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'delete')
grp_body = body['delete']
try:
@ -376,10 +373,7 @@ class GroupsController(wsgi.Controller):
"""Enables replications for a group."""
context = req.environ['cinder.context']
if body:
if not self.is_valid_body(body, 'enable_replication'):
msg = _("Missing required element 'enable_replication' in "
"request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'enable_replication')
LOG.info('Enable replication group with id: %s.', id,
context=context)
@ -400,10 +394,7 @@ class GroupsController(wsgi.Controller):
"""Disables replications for a group."""
context = req.environ['cinder.context']
if body:
if not self.is_valid_body(body, 'disable_replication'):
msg = _("Missing required element 'disable_replication' in "
"request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'disable_replication')
LOG.info('Disable replication group with id: %s.', id,
context=context)
@ -424,10 +415,7 @@ class GroupsController(wsgi.Controller):
"""Fails over replications for a group."""
context = req.environ['cinder.context']
if body:
if not self.is_valid_body(body, 'failover_replication'):
msg = _("Missing required element 'failover_replication' in "
"request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'failover_replication')
grp_body = body['failover_replication']
try:
@ -460,10 +448,7 @@ class GroupsController(wsgi.Controller):
"""List replication targets for a group."""
context = req.environ['cinder.context']
if body:
if not self.is_valid_body(body, 'list_replication_targets'):
msg = _("Missing required element 'list_replication_targets' "
"in request body.")
raise exc.HTTPBadRequest(explanation=msg)
self.assert_valid_body(body, 'list_replication_targets')
LOG.info('List replication targets for group with id: %s.', id,
context=context)

View File

@ -829,27 +829,27 @@ class ValidBodyTest(test.TestCase):
super(ValidBodyTest, self).setUp()
self.controller = wsgi.Controller()
def test_is_valid_body(self):
def test_assert_valid_body(self):
body = {'foo': {}}
self.assertTrue(self.controller.is_valid_body(body, 'foo'))
self.controller.assert_valid_body(body, 'foo')
def test_is_valid_body_none(self):
wsgi.Resource(controller=None)
self.assertFalse(self.controller.is_valid_body(None, 'foo'))
def test_assert_valid_body_none(self):
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.assert_valid_body(None, 'foo'))
def test_is_valid_body_empty(self):
wsgi.Resource(controller=None)
self.assertFalse(self.controller.is_valid_body({}, 'foo'))
def test_assert_valid_body_empty(self):
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.assert_valid_body({}, 'foo'))
def test_is_valid_body_no_entity(self):
wsgi.Resource(controller=None)
def test_assert_valid_body_no_entity(self):
body = {'bar': {}}
self.assertFalse(self.controller.is_valid_body(body, 'foo'))
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.assert_valid_body(body, 'foo'))
def test_is_valid_body_malformed_entity(self):
wsgi.Resource(controller=None)
def test_assert_valid_body_malformed_entity(self):
body = {'foo': 'bar'}
self.assertFalse(self.controller.is_valid_body(body, 'foo'))
self.assertRaises(webob.exc.HTTPBadRequest,
self.controller.assert_valid_body(body, 'foo'))
def test_validate_string_length_with_name_too_long(self):
name = 'a' * 256