diff --git a/karborclient/osc/v1/checkpoints.py b/karborclient/osc/v1/checkpoints.py index 09c27e2..84e8d18 100644 --- a/karborclient/osc/v1/checkpoints.py +++ b/karborclient/osc/v1/checkpoints.py @@ -226,3 +226,58 @@ class DeleteCheckpoint(command.Command): raise exceptions.CommandError( "Unable to find and delete any of the " "specified checkpoint.") + + +class ResetCheckpointState(command.Command): + _description = "Reset checkpoint state" + + log = logging.getLogger(__name__ + ".ResetCheckpointState") + + def get_parser(self, prog_name): + parser = super(ResetCheckpointState, self).get_parser(prog_name) + parser.add_argument( + 'provider_id', + metavar='', + help=_('Id of provider.') + ) + parser.add_argument( + 'checkpoint', + metavar='', + nargs="+", + help=_('Id of checkpoint.') + ) + parser.add_argument( + '--available', + action='store_const', dest='state', + default='error', const='available', + help=_('Request the checkpoint be reset to "available" state ' + 'instead of "error" state(the default).'), + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.data_protection + failure_count = 0 + for checkpoint_id in parsed_args.checkpoint: + try: + client.checkpoints.reset_state( + parsed_args.provider_id, checkpoint_id, parsed_args.state) + except exceptions.NotFound: + failure_count += 1 + self.log.error( + "Failed to reset state of '{0}'; checkpoint " + "not found".format(checkpoint_id)) + except exceptions.Forbidden: + failure_count += 1 + self.log.error( + "Failed to reset state of '{0}'; not " + "allowed".format(checkpoint_id)) + except exceptions.BadRequest: + failure_count += 1 + self.log.error( + "Failed to reset state of '{0}'; invalid input or " + "current checkpoint state".format(checkpoint_id)) + if failure_count == len(parsed_args.checkpoint): + raise exceptions.CommandError( + "Unable to find or reset any of the specified " + "checkpoint's state.") diff --git a/karborclient/tests/unit/osc/v1/test_checkpoints.py b/karborclient/tests/unit/osc/v1/test_checkpoints.py index b1860f5..e29b9a8 100644 --- a/karborclient/tests/unit/osc/v1/test_checkpoints.py +++ b/karborclient/tests/unit/osc/v1/test_checkpoints.py @@ -216,3 +216,38 @@ class TestDeleteCheckpoint(TestCheckpoints): self.checkpoints_mock.delete.assert_called_once_with( 'cf56bd3e-97a7-4078-b6d5-f36246333fd9', 'dcb20606-ad71-40a3-80e4-ef0fafdad0c3') + + +class TestResetCheckpointState(TestCheckpoints): + def setUp(self): + super(TestResetCheckpointState, self).setUp() + self.cmd = osc_checkpoints.ResetCheckpointState(self.app, None) + + def test_reset_checkpoint_with_default_state(self): + arglist = ['cf56bd3e-97a7-4078-b6d5-f36246333fd9', + 'dcb20606-ad71-40a3-80e4-ef0fafdad0c3'] + verifylist = [('provider_id', 'cf56bd3e-97a7-4078-b6d5-f36246333fd9'), + ('checkpoint', + ['dcb20606-ad71-40a3-80e4-ef0fafdad0c3']), + ('state', 'error')] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self.checkpoints_mock.reset_state.assert_called_once_with( + 'cf56bd3e-97a7-4078-b6d5-f36246333fd9', + 'dcb20606-ad71-40a3-80e4-ef0fafdad0c3', + 'error') + + def test_reset_checkpoint(self): + arglist = ['cf56bd3e-97a7-4078-b6d5-f36246333fd9', + 'dcb20606-ad71-40a3-80e4-ef0fafdad0c3', + '--available'] + verifylist = [('provider_id', 'cf56bd3e-97a7-4078-b6d5-f36246333fd9'), + ('checkpoint', + ['dcb20606-ad71-40a3-80e4-ef0fafdad0c3']), + ('state', 'available')] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + self.cmd.take_action(parsed_args) + self.checkpoints_mock.reset_state.assert_called_once_with( + 'cf56bd3e-97a7-4078-b6d5-f36246333fd9', + 'dcb20606-ad71-40a3-80e4-ef0fafdad0c3', + 'available') diff --git a/setup.cfg b/setup.cfg index 1e82e7a..7211323 100644 --- a/setup.cfg +++ b/setup.cfg @@ -57,6 +57,7 @@ openstack.data_protection.v1 = data_protection_checkpoint_show = karborclient.osc.v1.checkpoints:ShowCheckpoint data_protection_checkpoint_create = karborclient.osc.v1.checkpoints:CreateCheckpoint data_protection_checkpoint_delete = karborclient.osc.v1.checkpoints:DeleteCheckpoint + data_protection_checkpoint_reset_state = karborclient.osc.v1.checkpoints:ResetCheckpointState data_protection_scheduledoperation_list = karborclient.osc.v1.scheduled_operations:ListScheduledOperations data_protection_scheduledoperation_show = karborclient.osc.v1.scheduled_operations:ShowScheduledOperation data_protection_scheduledoperation_create = karborclient.osc.v1.scheduled_operations:CreateScheduledOperation