From 2075cba0a9c1c1e798653d53b4a9351f0abcc089 Mon Sep 17 00:00:00 2001 From: Franca Mgbogu Date: Fri, 22 Oct 2021 22:26:27 +0100 Subject: [PATCH] Add --wait flag to the share access rule This patch-set adds the --wait flag to the share access rule operation. This will make the CLI to wait for the operation to be completed before returning to the prompt. Closes-Bug: #1898311 Change-Id: I2ee7921875bd8834100659ff458d0fee9da7d50d --- manilaclient/tests/unit/v2/test_shell.py | 25 +++++++++++++++++ manilaclient/v2/shell.py | 34 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/manilaclient/tests/unit/v2/test_shell.py b/manilaclient/tests/unit/v2/test_shell.py index f6955eca2..0126de665 100644 --- a/manilaclient/tests/unit/v2/test_shell.py +++ b/manilaclient/tests/unit/v2/test_shell.py @@ -34,6 +34,7 @@ from manilaclient.tests.unit.v2 import fakes from manilaclient import utils from manilaclient.v2 import messages from manilaclient.v2 import security_services +from manilaclient.v2 import share_access_rules from manilaclient.v2 import share_group_types from manilaclient.v2 import share_groups from manilaclient.v2 import share_instances @@ -2347,6 +2348,30 @@ class ShellTest(test_utils.TestCase): cliutils.print_dict.assert_called_with( {'access_type': 'ip', 'access_to': '1.1.1.1'}) + @ddt.data(*set(["2.45", api_versions.MAX_VERSION])) + def test_allow_access_wait(self, version): + fake_access_rule = {'id': 'fake_id'} + fake_access = mock.Mock() + fake_access._info = fake_access_rule + fake_share = mock.Mock() + fake_share.name = 'fake_share' + fake_share.allow = mock.Mock(return_value=fake_access_rule) + self.mock_object(shell_v2, '_wait_for_resource_status', + mock.Mock(return_value=fake_access)) + self.mock_object(share_access_rules.ShareAccessRuleManager, 'get', + mock.Mock(return_value=fake_access_rule)) + with mock.patch.object(apiclient_utils, 'find_resource', + mock.Mock(return_value=fake_share)): + is_default_in_api = (api_versions.APIVersion(version) >= + api_versions.APIVersion('2.45')) + if is_default_in_api: + self.run_command("access-allow fake_share ip 10.0.0.1 --wait", + version=version) + shell_v2._wait_for_resource_status.assert_has_calls([ + mock.call(self.shell.cs, fake_access_rule, + resource_type='share_access_rule', + expected_status='active', status_attr='state')]) + def test_snapshot_access_deny(self): self.run_command("snapshot-access-deny 1234 fake_id") diff --git a/manilaclient/v2/shell.py b/manilaclient/v2/shell.py index 7ecf1dcac..ebf1549c6 100644 --- a/manilaclient/v2/shell.py +++ b/manilaclient/v2/shell.py @@ -57,6 +57,7 @@ def _wait_for_resource_status(cs, 'share_group_snapshot': _find_share_group_snapshot, 'share_instance': _find_share_instance, 'share_server': _find_share_server, + 'share_access_rule': _find_share_access_rule, } print_resource = { @@ -66,6 +67,7 @@ def _wait_for_resource_status(cs, 'share_group': _print_share_group, 'share_group_snapshot': _print_share_group_snapshot, 'share_instance': _print_share_instance, + 'share_access_rule': _print_share_access_rule, } expected_status = expected_status or ('available', ) @@ -248,6 +250,17 @@ def _print_share_instance(cs, instance): # noqa cliutils.print_dict(info) +def _find_share_access_rule(cs, access_rule): + """Get share access rule state""" + return apiclient_utils.find_resource(cs.share_access_rules, access_rule) + + +def _print_share_access_rule(cs, access_rule): + info = access_rule._info.copy() + + cliutils.print_dict(info) + + def _find_share_replica(cs, replica): """Get a replica by ID.""" return apiclient_utils.find_resource(cs.share_replicas, replica) @@ -1968,6 +1981,10 @@ def do_show(cs, args): # noqa help='Space Separated list of key=value pairs of metadata items. ' 'OPTIONAL: Default=None. Available only for microversion >= 2.45.', default=None) +@cliutils.arg( + '--wait', + action='store_true', + help='Wait for share access to become active') def do_access_allow(cs, args): """Allow access to a given share.""" access_metadata = None @@ -1982,6 +1999,23 @@ def do_access_allow(cs, args): share = _find_share(cs, args.share) access = share.allow(args.access_type, args.access_to, args.access_level, access_metadata) + if args.wait: + try: + if not cs.api_version.matches(api_versions.APIVersion("2.45"), + api_versions.APIVersion()): + raise exceptions.CommandError( + "Waiting on the allowing access operation is only " + "available for API versions equal to or greater than 2.45." + ) + access_id = access.get('id') + share_access_rule = cs.share_access_rules.get(access_id) + access = _wait_for_resource_status( + cs, share_access_rule, + resource_type='share_access_rule', + expected_status='active', + status_attr='state')._info + except exceptions.CommandError as e: + print(e, file=sys.stderr) cliutils.print_dict(access)