From 853d4fbceb1fffbe4fe4ae91846ac8531aa5d516 Mon Sep 17 00:00:00 2001 From: maaritamm Date: Sat, 9 May 2020 10:18:02 +0000 Subject: [PATCH] Add user message when share shrinking fails at the driver Not all drivers support share shrinking. This patch adds a user message when shrinking fails due to operation not being supported. Change-Id: I5137cd972ec7737d70865038d7b82cfe13b9566c Closes-Bug: #1802424 --- manila/message/message_field.py | 7 ++- manila/share/manager.py | 10 ++++ manila/tests/share/test_manager.py | 46 +++++++++++++++++++ ...when-shrinking-fails-83d0f60ead6f4a4b.yaml | 5 ++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1802424-add-user-message-when-shrinking-fails-83d0f60ead6f4a4b.yaml diff --git a/manila/message/message_field.py b/manila/message/message_field.py index 85f9ac5378..19d18e53e9 100644 --- a/manila/message/message_field.py +++ b/manila/message/message_field.py @@ -94,6 +94,10 @@ class Detail(object): " smaller than the current used space. The share status has been" " set to available. Please select a size greater than the current" " used space.")) + DRIVER_FAILED_SHRINK = ( + '019', + _("Share Driver does not support shrinking shares." + " Shrinking share operation failed.")) ALL = (UNKNOWN_ERROR, NO_VALID_HOST, @@ -112,7 +116,8 @@ class Detail(object): DRIVER_FAILED_EXTEND, FILTER_CREATE_FROM_SNAPSHOT, DRIVER_FAILED_CREATING_FROM_SNAP, - DRIVER_REFUSED_SHRINK) + DRIVER_REFUSED_SHRINK, + DRIVER_FAILED_SHRINK) # Exception and detail mappings EXCEPTION_DETAIL_MAPPINGS = { diff --git a/manila/share/manager.py b/manila/share/manager.py index e97a1c72ae..07f9817c32 100644 --- a/manila/share/manager.py +++ b/manila/share/manager.py @@ -3945,6 +3945,16 @@ class ShareManager(manager.SchedulerDependentManager): share_instance, "shrink.start") def error_occurred(exc, msg, status=constants.STATUS_SHRINKING_ERROR): + if isinstance(exc, NotImplementedError): + msg = _("Shrink share operation not supported.") + status = constants.STATUS_AVAILABLE + self.message_api.create( + context, + message_field.Action.SHRINK, + share['project_id'], + resource_type=message_field.Resource.SHARE, + resource_id=share['id'], + detail=message_field.Detail.DRIVER_FAILED_SHRINK) LOG.exception(msg, resource=share) self.db.share_update(context, share['id'], {'status': status}) diff --git a/manila/tests/share/test_manager.py b/manila/tests/share/test_manager.py index 7de2bdcbff..91d5ec90a6 100644 --- a/manila/tests/share/test_manager.py +++ b/manila/tests/share/test_manager.py @@ -3681,6 +3681,52 @@ class ShareManagerTestCase(test.TestCase): (['INFO', 'share.extend.start'], ['INFO', 'share.extend.end'])) + def test_shrink_share_not_supported(self): + share = db_utils.create_share(size=2) + new_size = 1 + share_id = share['id'] + + self.mock_object(self.share_manager.db, 'share_get', + mock.Mock(return_value=share)) + self.mock_object(self.share_manager, 'driver') + self.mock_object(self.share_manager.db, 'share_update') + + self.mock_object(quota.QUOTAS, 'reserve') + self.mock_object(quota.QUOTAS, 'rollback') + self.mock_object(self.share_manager.driver, 'shrink_share', + mock.Mock(side_effect=NotImplementedError)) + + self.assertRaises( + exception.ShareShrinkingError, + self.share_manager.shrink_share, self.context, share_id, new_size) + + self.share_manager.driver.shrink_share.assert_called_once_with( + utils.IsAMatcher(models.ShareInstance), + new_size, share_server=None + ) + + self.share_manager.db.share_update.assert_called_once_with( + mock.ANY, share_id, {'status': constants.STATUS_AVAILABLE} + ) + + quota.QUOTAS.reserve.assert_called_once_with( + mock.ANY, gigabytes=-1, project_id=share['project_id'], + share_type_id=None, user_id=share['user_id'], + ) + quota.QUOTAS.rollback.assert_called_once_with( + mock.ANY, mock.ANY, project_id=share['project_id'], + share_type_id=None, user_id=share['user_id'], + ) + self.assertTrue(self.share_manager.db.share_get.called) + + self.share_manager.message_api.create.assert_called_once_with( + utils.IsAMatcher(context.RequestContext), + message_field.Action.SHRINK, + share['project_id'], + resource_type=message_field.Resource.SHARE, + resource_id=share_id, + detail=message_field.Detail.DRIVER_FAILED_SHRINK) + @ddt.data((True, [{'id': 'fake'}]), (False, [])) @ddt.unpack def test_shrink_share_quota_error(self, supports_replication, diff --git a/releasenotes/notes/bug-1802424-add-user-message-when-shrinking-fails-83d0f60ead6f4a4b.yaml b/releasenotes/notes/bug-1802424-add-user-message-when-shrinking-fails-83d0f60ead6f4a4b.yaml new file mode 100644 index 0000000000..b18521e91d --- /dev/null +++ b/releasenotes/notes/bug-1802424-add-user-message-when-shrinking-fails-83d0f60ead6f4a4b.yaml @@ -0,0 +1,5 @@ + +fixes: + - | + Added a new user message when share shrinking fails due to operation not being + supported by the driver.