From f35ae2227919436ac5f10eb5570274c4b220981e Mon Sep 17 00:00:00 2001 From: Tobias Urdin Date: Fri, 14 Mar 2025 11:42:50 +0100 Subject: [PATCH] Add --project param to stack list in OSC plugin This adds the --project parameter for the `stack list` command in the OSC plugin so that we can retrieve the stacks for a single project. Change-Id: Ia1a198526292f4f8a6bccaa6235755525230e73e --- doc/requirements.txt | 1 + heatclient/osc/v1/stack.py | 24 +++++++++++++++++-- heatclient/tests/unit/osc/v1/test_stack.py | 15 ++++++++++++ ...-list-filter-project-cefce0d0a4d4467b.yaml | 4 ++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/osc-stack-list-filter-project-cefce0d0a4d4467b.yaml diff --git a/doc/requirements.txt b/doc/requirements.txt index 303fc44f..e179cd5a 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -2,3 +2,4 @@ openstackdocstheme>=2.2.1 # Apache-2.0 reno>=3.1.0 # Apache-2.0 sphinx>=2.0.0 # BSD sphinxcontrib-httpdomain>=1.3.0 # BSD +python-openstackclient>=3.12.0 # Apache-2.0 diff --git a/heatclient/osc/v1/stack.py b/heatclient/osc/v1/stack.py index e12ed55f..19530c37 100644 --- a/heatclient/osc/v1/stack.py +++ b/heatclient/osc/v1/stack.py @@ -16,6 +16,7 @@ import logging import sys +from openstackclient.identity import common as identity_common from osc_lib.command import command from osc_lib import exceptions as exc from osc_lib import utils @@ -521,6 +522,13 @@ class ListStack(command.Lister): 'filter on multiple properties)'), action='append' ) + parser.add_argument( + '--project', + dest='project', + metavar='', + help=_('Filter stacks by project (name or ID) (admin only)'), + ) + identity_common.add_project_domain_option_to_parser(parser) parser.add_argument( '--tags', metavar='', @@ -574,7 +582,16 @@ class ListStack(command.Lister): self.log.debug("take_action(%s)", parsed_args) client = self.app.client_manager.orchestration - return _list(client, args=parsed_args) + identity_client = self.app.client_manager.identity + + project_id = None + if parsed_args.project: + project_id = identity_common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain).id + + return _list(client, args=parsed_args, project_id=project_id) class EnvironmentShowStack(format_utils.YamlFormat): @@ -638,7 +655,7 @@ class ListFileStack(format_utils.YamlFormat): return ['files'], [files] -def _list(client, args=None): +def _list(client, args=None, project_id=None): kwargs = {} columns = [ 'ID', @@ -687,6 +704,9 @@ def _list(client, args=None): if args.deleted: columns.append('Deletion Time') + if project_id: + kwargs['tenant'] = project_id + data = client.stacks.list(**kwargs) data = list(data) for stk in data: diff --git a/heatclient/tests/unit/osc/v1/test_stack.py b/heatclient/tests/unit/osc/v1/test_stack.py index 49dc8e97..3caa5bd1 100644 --- a/heatclient/tests/unit/osc/v1/test_stack.py +++ b/heatclient/tests/unit/osc/v1/test_stack.py @@ -37,6 +37,8 @@ class TestStack(orchestration_fakes.TestOrchestrationv1): super(TestStack, self).setUp() self.mock_client = self.app.client_manager.orchestration self.stack_client = self.app.client_manager.orchestration.stacks + self.projects_mock = self.app.client_manager.identity.projects + self.projects_mock.reset_mock() class TestStackCreate(TestStack): @@ -576,6 +578,19 @@ class TestStackList(TestStack): self.stack_client.list.assert_called_with(**kwargs) self.assertEqual(cols, columns) + def test_stack_list_filter_project(self): + kwargs = copy.deepcopy(self.defaults) + project_id = '123456' + self.projects_mock.get.return_value = ( + mock.MagicMock(id=project_id)) + kwargs['tenant'] = project_id + arglist = ['--project', project_id] + parsed_args = self.check_parser(self.cmd, arglist, []) + + columns, data = self.cmd.take_action(parsed_args) + + self.stack_client.list.assert_called_with(**kwargs) + def test_stack_list_long(self): self.stack_client.list.return_value = [ stacks.Stack(None, self.data_with_project)] diff --git a/releasenotes/notes/osc-stack-list-filter-project-cefce0d0a4d4467b.yaml b/releasenotes/notes/osc-stack-list-filter-project-cefce0d0a4d4467b.yaml new file mode 100644 index 00000000..8c077ba9 --- /dev/null +++ b/releasenotes/notes/osc-stack-list-filter-project-cefce0d0a4d4467b.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + Added ``--project`` filter to ``stack list`` command in the OSC plugin.