From 9a29099b31b8234a9f015ca7d7355ba9ad6ea4b5 Mon Sep 17 00:00:00 2001 From: tspyderboy Date: Sun, 24 Mar 2024 02:25:08 +0530 Subject: [PATCH] Added "--wait" option for creating/deleting a share group snapshot 1) Creating a share group snapshot $ manila share-group-snapshot-create --name share-group-snap-1 --wait 2) Deleting a share group $ manila share-group-snapshot-delete share-group-snap-1 --wait Closes-Bug: #1898319 Change-Id: Id8e4a7c4808152a41b35ec2e6c09f1d439cf14e2 --- manilaclient/tests/unit/v2/test_shell.py | 33 +++++++++++++++++++ manilaclient/v2/shell.py | 18 ++++++++++ ...share-group-snapshot-f789e324f66ad1b6.yaml | 7 ++++ 3 files changed, 58 insertions(+) create mode 100644 releasenotes/notes/bug-1898319-add-wait-to-create-delete-a-share-group-snapshot-f789e324f66ad1b6.yaml diff --git a/manilaclient/tests/unit/v2/test_shell.py b/manilaclient/tests/unit/v2/test_shell.py index 33a03480f..0f09fcd1a 100644 --- a/manilaclient/tests/unit/v2/test_shell.py +++ b/manilaclient/tests/unit/v2/test_shell.py @@ -3066,6 +3066,7 @@ class ShellTest(test_utils.TestCase): '--description my_fake_description --name fake_name fake-sg-id', ) @mock.patch.object(shell_v2, '_find_share_group', mock.Mock()) + @mock.patch.object(shell_v2, '_wait_for_resource_status', mock.Mock()) def test_share_group_snapshot_create(self, data): fake_sg = type('FakeShareGroup', (object,), {'id': '1234'}) shell_v2._find_share_group.return_value = fake_sg @@ -3074,6 +3075,21 @@ class ShellTest(test_utils.TestCase): shell_v2._find_share_group.assert_called_with(mock.ANY, 'fake-sg-id') self.assert_called('POST', '/share-group-snapshots') + self.assertEqual(0, shell_v2._wait_for_resource_status.call_count) + + @mock.patch.object(shell_v2, '_find_share_group', mock.Mock()) + @mock.patch.object(shell_v2, '_wait_for_resource_status', mock.Mock()) + def test_share_group_snapshot_create_with_wait(self): + fake_sg = type('FakeShareGroup', (object,), {'id': '1234'}) + shell_v2._find_share_group.return_value = fake_sg + self.run_command('share-group-snapshot-create fake-sg-id --wait') + + shell_v2._find_share_group.assert_called_with(mock.ANY, 'fake-sg-id') + self.assert_called('POST', '/share-group-snapshots') + # _wait_for_resource_status should be triggered once + shell_v2._wait_for_resource_status.assert_called_once_with( + self.shell.cs, mock.ANY, resource_type='share_group_snapshot', + expected_status='available') @mock.patch.object(cliutils, 'print_list', mock.Mock()) def test_share_group_snapshot_list(self): @@ -3175,6 +3191,7 @@ class ShellTest(test_utils.TestCase): self.run_command, 'share-group-snapshot-update 1234') @mock.patch.object(shell_v2, '_find_share_group_snapshot', mock.Mock()) + @mock.patch.object(shell_v2, '_wait_for_resource_status', mock.Mock()) def test_share_group_snapshot_delete(self): fake_sg_snapshot = type('FakeSGSnapshot', (object,), {'id': '1234'}) shell_v2._find_share_group_snapshot.return_value = fake_sg_snapshot @@ -3182,6 +3199,22 @@ class ShellTest(test_utils.TestCase): self.run_command('share-group-snapshot-delete fake-group-snapshot') self.assert_called('DELETE', '/share-group-snapshots/1234') + self.assertEqual(0, shell_v2._wait_for_resource_status.call_count) + + @mock.patch.object(shell_v2, '_find_share_group_snapshot', mock.Mock()) + @mock.patch.object(shell_v2, '_wait_for_resource_status', mock.Mock()) + def test_share_group_snapshot_delete_with_wait(self): + fake_sg_snapshot = type('FakeSGSnapshot', (object,), {'id': '1234'}) + shell_v2._find_share_group_snapshot.return_value = fake_sg_snapshot + + self.run_command('share-group-snapshot-delete fake-group-snapshot' + ' --wait') + + self.assert_called('DELETE', '/share-group-snapshots/1234') + # _wait_for_resource_status should be triggered once + shell_v2._wait_for_resource_status.assert_called_once_with( + self.shell.cs, mock.ANY, resource_type='share_group_snapshot', + expected_status='deleted') @mock.patch.object(shell_v2, '_find_share_group_snapshot', mock.Mock()) def test_share_group_snapshot_delete_force(self): diff --git a/manilaclient/v2/shell.py b/manilaclient/v2/shell.py index d00ab0b75..be7939501 100644 --- a/manilaclient/v2/shell.py +++ b/manilaclient/v2/shell.py @@ -6039,12 +6039,21 @@ def do_share_group_reset_state(cs, args): metavar='', help='Optional share group snapshot description. (Default=None)', default=None) +@cliutils.arg( + '--wait', + action='store_true', + default=False, + help='Wait for share group snapshot to be created') @cliutils.service_type('sharev2') def do_share_group_snapshot_create(cs, args): """Creates a new share group snapshot.""" kwargs = {'name': args.name, 'description': args.description} share_group = _find_share_group(cs, args.share_group) sg_snapshot = cs.share_group_snapshots.create(share_group.id, **kwargs) + if args.wait: + _wait_for_resource_status( + cs, sg_snapshot, resource_type='share_group_snapshot', + expected_status='available') _print_share_group_snapshot(cs, sg_snapshot) @@ -6245,6 +6254,11 @@ def do_share_group_snapshot_update(cs, args): default=False, help='Attempt to force delete the share group snapshot(s) (Default=False)' ' (Admin only).') +@cliutils.arg( + '--wait', + action='store_true', + default=False, + help='Wait for share group snapshot to be deleted') @cliutils.service_type('sharev2') def do_share_group_snapshot_delete(cs, args): """Remove one or more share group snapshots.""" @@ -6257,6 +6271,10 @@ def do_share_group_snapshot_delete(cs, args): try: sg_snapshot_ref = _find_share_group_snapshot(cs, sg_snapshot) cs.share_group_snapshots.delete(sg_snapshot_ref, **kwargs) + if args.wait: + _wait_for_resource_status( + cs, sg_snapshot, resource_type='share_group_snapshot', + expected_status='deleted') except Exception as e: failure_count += 1 print("Delete for share group snapshot %s failed: %s" % ( diff --git a/releasenotes/notes/bug-1898319-add-wait-to-create-delete-a-share-group-snapshot-f789e324f66ad1b6.yaml b/releasenotes/notes/bug-1898319-add-wait-to-create-delete-a-share-group-snapshot-f789e324f66ad1b6.yaml new file mode 100644 index 000000000..fe4a5e812 --- /dev/null +++ b/releasenotes/notes/bug-1898319-add-wait-to-create-delete-a-share-group-snapshot-f789e324f66ad1b6.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + The commands "share-group-snapshot-create" and + "share-group-snapshot-delete" now accept an optional "--wait" + option that allows users to let the client poll for the completion + of the operation. \ No newline at end of file