diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 01827e7171..1ca9049fc8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,6 +12,10 @@ Fixed Contributed by @blackstrip +* Fix issue with pack option not working when running policy list cli #5534 + + Contributed by @momokuri-3 + Added ~~~~~ diff --git a/st2client/st2client/commands/policy.py b/st2client/st2client/commands/policy.py index 31d9090cfb..be9a214c84 100644 --- a/st2client/st2client/commands/policy.py +++ b/st2client/st2client/commands/policy.py @@ -109,20 +109,19 @@ def __init__(self, resource, *args, **kwargs): @resource.add_auth_token_to_kwargs_from_cli def run(self, args, **kwargs): - if args.resource_ref or args.policy_type: - filters = {} - - if args.resource_ref: - filters["resource_ref"] = args.resource_ref - - if args.policy_type: - filters["policy_type"] = args.policy_type - - filters.update(**kwargs) - - return self.manager.query(**filters) - else: - return self.manager.get_all(**kwargs) + filters = {} + if args.pack: + filters["pack"] = args.pack + if args.resource_ref: + filters["resource_ref"] = args.resource_ref + if args.policy_type: + filters["policy_type"] = args.policy_type + filters.update(**kwargs) + include_attributes = self._get_include_attributes(args=args) + if include_attributes: + include_attributes = ",".join(include_attributes) + filters["params"] = {"include_attributes": include_attributes} + return self.manager.query(**filters) class PolicyGetCommand(resource.ContentPackResourceGetCommand): diff --git a/st2client/tests/unit/test_shell.py b/st2client/tests/unit/test_shell.py index aa54839ff6..5eb27714ca 100644 --- a/st2client/tests/unit/test_shell.py +++ b/st2client/tests/unit/test_shell.py @@ -562,6 +562,21 @@ def test_dont_warn_multiple_times(self): shell.LOG.info.call_args_list[1][0][0], "Skipping parsing CLI config" ) + def test_policy_list_with_pack_option(self): + argv = ["policy", "list", "-p", "test"] + mock_obj = mock.MagicMock( + return_value=base.FakeResponse(json.dumps(base.RESOURCES), 200, "OK") + ) + with mock.patch.object(httpclient.HTTPClient, "get", mock_obj): + self.shell.run(argv) + self.assertEqual( + mock_obj.mock_calls[0], + mock.call( + "/policies/?include_attributes=ref%2Cresource_ref%2C" + "policy_type%2Cenabled&pack=test" + ), + ) + class CLITokenCachingTestCase(unittest2.TestCase): def setUp(self):