From 95233d740aba4a8c8281906f1973978e6873cd1c Mon Sep 17 00:00:00 2001 From: liyingjun Date: Wed, 29 Jul 2015 09:42:00 +0800 Subject: [PATCH] Validate 'is_public' when creating volume type is_public should be checked when creating volume type, otherwise DBError will be raised in cinder-api, and the user will receive a 500 Internal server error. Change-Id: Id0695fbb05613f3655a7af5b5ab10e08ed5e606b Closes-bug: #1479170 --- cinder/api/contrib/types_manage.py | 5 +++++ cinder/tests/unit/api/contrib/test_types_manage.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/cinder/api/contrib/types_manage.py b/cinder/api/contrib/types_manage.py index 5a908933a..9c32528d7 100644 --- a/cinder/api/contrib/types_manage.py +++ b/cinder/api/contrib/types_manage.py @@ -73,6 +73,11 @@ class VolumeTypesManageController(wsgi.Controller): utils.check_string_length(description, 'Type description', min_length=0, max_length=255) + if not utils.is_valid_boolstr(is_public): + msg = _("Invalid value '%s' for is_public. Accepted values: " + "True or False.") % is_public + raise webob.exc.HTTPBadRequest(explanation=msg) + try: volume_types.create(context, name, diff --git a/cinder/tests/unit/api/contrib/test_types_manage.py b/cinder/tests/unit/api/contrib/test_types_manage.py index 3bdaa5d16..f613f7487 100644 --- a/cinder/tests/unit/api/contrib/test_types_manage.py +++ b/cinder/tests/unit/api/contrib/test_types_manage.py @@ -186,6 +186,7 @@ class VolumeTypesManageApiTest(test.TestCase): return_volume_types_get_by_name) body = {"volume_type": {"name": "vol_type_1", + "os-volume-type-access:is_public": True, "extra_specs": {"key1": "value1"}}} req = fakes.HTTPRequest.blank('/v2/fake/types') @@ -246,6 +247,15 @@ class VolumeTypesManageApiTest(test.TestCase): self.assertRaises(webob.exc.HTTPConflict, self.controller._create, req, body) + def test_create_type_with_invalid_is_public(self): + body = {"volume_type": {"name": "vol_type_1", + "os-volume-type-access:is_public": "fake", + "description": "test description", + "extra_specs": {"key1": "value1"}}} + req = fakes.HTTPRequest.blank('/v2/fake/types') + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller._create, req, body) + def _create_volume_type_bad_body(self, body): req = fakes.HTTPRequest.blank('/v2/fake/types') req.method = 'POST' -- 2.45.2