From c0fc23a39f87629b59fae7bbf46f70e3e1b459cd 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) --- 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 4358710d11..4f33836f1a 100644 --- a/manila/api/v1/shares.py +++ b/manila/api/v1/shares.py @@ -392,6 +392,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 ccf7e97874..2c342da752 100644 --- a/manila/share/share_types.py +++ b/manila/share/share_types.py @@ -198,7 +198,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 567b38e092..0930867a87 100644 --- a/manila/tests/api/v1/test_shares.py +++ b/manila/tests/api/v1/test_shares.py @@ -542,6 +542,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