diff --git a/manila/api/v1/shares.py b/manila/api/v1/shares.py index 245bcda235..48d2797ab8 100644 --- a/manila/api/v1/shares.py +++ b/manila/api/v1/shares.py @@ -374,6 +374,8 @@ class ShareMixin(object): exception.ShareTypeNotFoundByName): msg = _("Share type not found.") raise exc.HTTPNotFound(explanation=msg) + except exception.InvalidShareType as e: + raise exc.HTTPBadRequest(explanation=e.message) elif not snapshot: def_share_type = share_types.get_default_share_type() if def_share_type: diff --git a/manila/share/share_types.py b/manila/share/share_types.py index 3eff44ce91..09fb578616 100644 --- a/manila/share/share_types.py +++ b/manila/share/share_types.py @@ -199,7 +199,9 @@ def get_share_type_by_name(context, name): if name is None: msg = _("name cannot be None") raise exception.InvalidShareType(reason=msg) - + if not isinstance(name, str): + msg = _("the share type's name parameter was badly formatted") + raise exception.InvalidShareType(reason=msg) return db.share_type_get_by_name(context, name) diff --git a/manila/tests/api/v1/test_shares.py b/manila/tests/api/v1/test_shares.py index 58c9259ddf..30dbdb9621 100644 --- a/manila/tests/api/v1/test_shares.py +++ b/manila/tests/api/v1/test_shares.py @@ -533,6 +533,26 @@ class ShareAPITest(test.TestCase): self.mock_policy_check.assert_called_once_with( req.environ['manila.context'], self.resource_name, 'create') + def test_share_creation_fails_with_invalid_share_type(self): + shr = { + "size": 1, + "name": "Share Test Name", + "description": "Share Test Desc", + "share_proto": "fakeproto", + "availability_zone": "zone1:host1", + "share_type": "Invalid share type" + } + body = {"share": shr} + req = fakes.HTTPRequest.blank('/fake/shares') + with mock.patch('manila.share.share_types.get_share_type_by_name', + side_effect=exception.InvalidShareType(reason='')): + self.assertRaises(webob.exc.HTTPBadRequest, + self.controller.create, + req, + body) + self.mock_policy_check.assert_called_once_with( + req.environ['manila.context'], self.resource_name, 'create') + def test_share_create_invalid_availability_zone(self): self.mock_object( db, diff --git a/releasenotes/notes/bug-1944478-change-status-for-shares-api-5dbc4986d032c8e1.yaml b/releasenotes/notes/bug-1944478-change-status-for-shares-api-5dbc4986d032c8e1.yaml new file mode 100644 index 0000000000..81d49cb095 --- /dev/null +++ b/releasenotes/notes/bug-1944478-change-status-for-shares-api-5dbc4986d032c8e1.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - | + Changed the error and status code that was raised + when share types are not handled in shares api \ No newline at end of file