From 8fedd08b278fd012c42b718a0bed502e5551dde0 Mon Sep 17 00:00:00 2001 From: namrata Date: Thu, 2 Jun 2022 16:11:02 +0000 Subject: [PATCH] [OSC] Implement Share Migration show and add argument to Share Set Command This commit adds 'openstack share migration show' and add argument '--task-state' to 'openstack share set' command, that implement the same functionality as 'manila migration-get-progress'and manila reset-task-state command. Partially-implements: bp openstack-client-support Change-Id: I288661b4294561a1a52411a0cb0fdfd1f82021ed --- doc/source/cli/osc/v2/index.rst | 2 + manilaclient/osc/v2/share.py | 47 +++++++++++++++++ manilaclient/tests/unit/osc/v2/test_share.py | 55 ++++++++++++++++++-- setup.cfg | 1 + 4 files changed, 100 insertions(+), 5 deletions(-) diff --git a/doc/source/cli/osc/v2/index.rst b/doc/source/cli/osc/v2/index.rst index 7ac5ce529..a05642d2a 100644 --- a/doc/source/cli/osc/v2/index.rst +++ b/doc/source/cli/osc/v2/index.rst @@ -74,6 +74,8 @@ share migration .. autoprogram-cliff:: openstack.share.v2 :command: share migration complete +.. autoprogram-cliff:: openstack.share.v2 + :command: share migration show ============== share networks diff --git a/manilaclient/osc/v2/share.py b/manilaclient/osc/v2/share.py index d92b1d3f3..00d69a503 100644 --- a/manilaclient/osc/v2/share.py +++ b/manilaclient/osc/v2/share.py @@ -704,6 +704,20 @@ class SetShare(command.Command): 'Examples include: available, error, creating, deleting, ' 'error_deleting.') ) + parser.add_argument( + '--task-state', + metavar="", + required=False, + default=None, + help=_("Indicate which task state to assign the share. Options " + "include migration_starting, migration_in_progress, " + "migration_completing, migration_success, migration_error, " + "migration_cancelled, migration_driver_in_progress, " + "migration_driver_phase1_done, data_copying_starting, " + "data_copying_in_progress, data_copying_completing, " + "data_copying_completed, data_copying_cancelled, " + "data_copying_error. ") + ) return parser def take_action(self, parsed_args): @@ -744,6 +758,13 @@ class SetShare(command.Command): LOG.error(_( "Failed to set status for the share: %s"), e) result += 1 + if parsed_args.task_state: + try: + share_obj.reset_task_state(parsed_args.task_state) + except Exception as e: + LOG.error(_("Failed to update share task state" + "%s"), e) + result += 1 if result > 0: raise exceptions.CommandError(_("One or more of the " @@ -1340,3 +1361,29 @@ class ShareMigrationComplete(command.Command): share = apiutils.find_resource(share_client.shares, parsed_args.share) share.migration_complete() + + +class ShareMigrationShow(command.ShowOne): + """Gets migration progress of a given share when copying + + (Admin only, Experimental). + + """ + _description = _("Gets migration progress of a given share when copying") + + def get_parser(self, prog_name): + parser = super(ShareMigrationShow, self).get_parser(prog_name) + parser.add_argument( + 'share', + metavar="", + help=_('Name or ID of the share to get share migration progress ' + 'information.') + ) + return parser + + def take_action(self, parsed_args): + share_client = self.app.client_manager.share + share = apiutils.find_resource(share_client.shares, + parsed_args.share) + result = share.migration_get_progress() + return self.dict2columns(result[1]) diff --git a/manilaclient/tests/unit/osc/v2/test_share.py b/manilaclient/tests/unit/osc/v2/test_share.py index cfb7f7e55..2e3ccba77 100644 --- a/manilaclient/tests/unit/osc/v2/test_share.py +++ b/manilaclient/tests/unit/osc/v2/test_share.py @@ -996,11 +996,9 @@ class TestShareShow(TestShare): ("share", self._share.id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) - cliutils.convert_dict_list_to_string = mock.Mock() cliutils.convert_dict_list_to_string.return_value = dict( self._export_location) - columns, data = self.cmd.take_action(parsed_args) self.shares_mock.get.assert_called_with(self._share.id) @@ -1019,7 +1017,7 @@ class TestShareSet(TestShare): super(TestShareSet, self).setUp() self._share = manila_fakes.FakeShare.create_one_share( - methods={"reset_state": None} + methods={"reset_state": None, "reset_task_state": None} ) self.shares_mock.get.return_value = self._share @@ -1175,6 +1173,22 @@ class TestShareSet(TestShare): self.assertRaises( osc_exceptions.CommandError, self.cmd.take_action, parsed_args) + def test_share_set_task_state(self): + new_task_state = 'migration_starting' + arglist = [ + self._share.id, + '--task-state', new_task_state + ] + verifylist = [ + ('share', self._share.id), + ('task_state', new_task_state) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + result = self.cmd.take_action(parsed_args) + self._share.reset_task_state.assert_called_with(new_task_state) + self.assertIsNone(result) + class TestShareUnset(TestShare): @@ -1763,7 +1777,6 @@ class TestShowShareProperties(TestShare): } ) self.shares_mock.get.return_value = self._share - self.shares_mock.get_metadata.return_value = self._share.metadata # Get the command object to test @@ -1780,7 +1793,6 @@ class TestShowShareProperties(TestShare): ("share", self._share.id) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) - columns, data = self.cmd.take_action(parsed_args) self.shares_mock.get.assert_called_with(self._share.id) self.shares_mock.get_metadata.assert_called_with(self._share) @@ -1990,3 +2002,36 @@ class TestShareMigrationComplete(TestShare): result = self.cmd.take_action(parsed_args) self._share.migration_complete.assert_called self.assertIsNone(result) + + +class TestShareMigrationShow(TestShare): + + def setUp(self): + super(TestShareMigrationShow, self).setUp() + + self._share = manila_fakes.FakeShare.create_one_share( + attrs={ + 'status': 'available', + 'task_state': 'migration_in_progress'}, + methods={'migration_get_progress': ("", + {'total_progress': 0, 'task_state': + 'migration_in_progress', + 'details': {}}) + }) + self.shares_mock.get.return_value = self._share + + self.shares_mock.manage.return_value = self._share + + # Get the command objects to test + self.cmd = osc_shares.ShareMigrationShow(self.app, None) + + def test_migration_show(self): + arglist = [ + self._share.id + ] + verifylist = [ + ('share', self._share.id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self._share.migration_get_progress.assert_called diff --git a/setup.cfg b/setup.cfg index b731c8f9a..3a216a924 100644 --- a/setup.cfg +++ b/setup.cfg @@ -47,6 +47,7 @@ openstack.share.v2 = share_migration_start = manilaclient.osc.v2.share:ShareMigrationStart share_migration_cancel = manilaclient.osc.v2.share:ShareMigrationCancel share_migration_complete = manilaclient.osc.v2.share:ShareMigrationComplete + share_migration_show = manilaclient.osc.v2.share:ShareMigrationShow share_export_location_show = manilaclient.osc.v2.share:ShareExportLocationShow share_export_location_list = manilaclient.osc.v2.share:ShareExportLocationList share_properties_show = manilaclient.osc.v2.share:ShowShareProperties