Merge "Add --project param to stack list in OSC plugin"

This commit is contained in:
Zuul
2025-05-07 11:08:58 +00:00
committed by Gerrit Code Review
4 changed files with 42 additions and 2 deletions

View File

@@ -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

View File

@@ -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
@@ -522,6 +523,13 @@ class ListStack(command.Lister):
'filter on multiple properties)'),
action='append'
)
parser.add_argument(
'--project',
dest='project',
metavar='<project>',
help=_('Filter stacks by project (name or ID) (admin only)'),
)
identity_common.add_project_domain_option_to_parser(parser)
parser.add_argument(
'--tags',
metavar='<tag1,tag2...>',
@@ -575,7 +583,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):
@@ -639,7 +656,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',
@@ -688,6 +705,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:

View File

@@ -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)]

View File

@@ -0,0 +1,4 @@
---
features:
- |
Added ``--project`` filter to ``stack list`` command in the OSC plugin.