From ede80c4f365072d53a82ef3f4840cc57a663732d Mon Sep 17 00:00:00 2001 From: melakualehegn Date: Thu, 26 Oct 2023 03:11:03 +0300 Subject: [PATCH] Change status and error handling for /shares API This change modifies the status and error handling logic of the /shares API when it fails to handle the specified share_type in the request. The updated logic ensures that appropriate responses are generated to handle this scenario effectively. Closes-Bug: #1944478 Change-Id: I8d4b30daae2fe8c88c30d93d402bf2e5a558f804 (cherry picked from commit b24ef91f2c6e1e63c6bae8e52b778bba22ff15ff) (cherry picked from commit 2aa760011d4dd1ea8f17dad9df94db4d4156692a) (cherry picked from commit 7918f1d4bb4a66497fc1c76085100dd7f5e82231) (cherry picked from commit c0fc23a39f87629b59fae7bbf46f70e3e1b459cd) (cherry picked from commit 84111ad7cb4496d1083703fba8f0e55002347363) --- manila/api/v1/shares.py | 2 ++ manila/share/share_types.py | 4 +++- manila/tests/api/v1/test_shares.py | 20 +++++++++++++++++++ ...tatus-for-shares-api-5dbc4986d032c8e1.yaml | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1944478-change-status-for-shares-api-5dbc4986d032c8e1.yaml 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