Support for environment list filtering by project id

Added an optional parameter to the 'list' method of
'EnvironmentManager' class to support the filtering of environment
list by the owner tenant (project)

Change-Id: I9195a30c287d56dedd12099843f2c131882079a5
Implements-blueprint: list-environments-of-a-given-project
Depends-on: I90b0dd672d5a431f63e52753d7f517fd3d0ec6eb
This commit is contained in:
Alexander Tivelkov 2016-10-05 15:40:05 +03:00 committed by Stan Lagun
parent f8d340a778
commit a6b573f42d
6 changed files with 56 additions and 8 deletions

View File

@ -39,13 +39,20 @@ class ListEnvironments(command.Lister):
default=False,
help='List environments from all tenants (admin only).',
)
parser.add_argument(
'--tenant',
metavar='<TENANT_ID>',
default=None,
help='Allows to list environments for a given tenant (admin only).'
)
return parser
def take_action(self, parsed_args):
LOG.debug("take_action({0})".format(parsed_args))
client = self.app.client_manager.application_catalog
data = client.environments.list(parsed_args.all_tenants)
data = client.environments.list(
parsed_args.all_tenants, parsed_args.tenant)
columns = ('id', 'name', 'status', 'created', 'updated')
column_headers = [c.capitalize() for c in columns]

View File

@ -77,9 +77,9 @@ class TestListEnvironment(TestEnvironment):
self.assertEqual(expected_data, data)
@mock.patch('osc_lib.utils.get_item_properties')
def test_environment_list_with_options(self, mock_util):
def test_environment_list_with_all_tenants(self, mock_util):
arglist = ['--all-tenants']
verifylist = [('all_tenants', True)]
verifylist = [('all_tenants', True), ('tenant', None)]
mock_util.return_value = ('1234', 'Environment of all tenants',
'fake deployed', '2015-12-16T17:31:54',
@ -99,6 +99,32 @@ class TestListEnvironment(TestEnvironment):
'fake deployed', '2015-12-16T17:31:54',
'2015-12-16T17:31:54')]
self.assertEqual(expected_data, data)
self.environment_mock.list.assert_called_once_with(True, None)
@mock.patch('osc_lib.utils.get_item_properties')
def test_environment_list_with_tenant(self, mock_util):
arglist = ['--tenant=ABC']
verifylist = [('all_tenants', False), ('tenant', 'ABC')]
mock_util.return_value = ('1234', 'Environment of tenant ABC',
'fake deployed', '2015-12-16T17:31:54',
'2015-12-16T17:31:54'
)
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args)
# Check that columns are correct
expected_columns = ['Id', 'Name', 'Status', 'Created', 'Updated']
self.assertEqual(expected_columns, columns)
# Check that data is correct
expected_data = [('1234', 'Environment of tenant ABC',
'fake deployed', '2015-12-16T17:31:54',
'2015-12-16T17:31:54')]
self.assertEqual(expected_data, data)
self.environment_mock.list.assert_called_once_with(False, 'ABC')
class TestShowEnvironment(TestEnvironment):

View File

@ -408,11 +408,15 @@ class ShellCommandTest(ShellTest):
self.register_keystone_token_fixture(m_requests)
self.shell('environment-list')
self.client.environments.list.assert_called_once_with(False)
self.client.environments.list.assert_called_once_with(False, None)
self.client.environments.list.reset_mock()
self.shell('environment-list --all-tenants')
self.client.environments.list.assert_called_once_with(True)
self.client.environments.list.assert_called_once_with(True, None)
self.client.environments.list.reset_mock()
self.shell('environment-list --tenant ABC')
self.client.environments.list.assert_called_once_with(False, 'ABC')
@mock.patch('muranoclient.v1.environments.EnvironmentManager')
@requests_mock.mock()

View File

@ -36,9 +36,12 @@ class Status(base.Resource):
class EnvironmentManager(base.ManagerWithFind):
resource_class = Environment
def list(self, all_tenants=False):
def list(self, all_tenants=False, tenant_id=None):
params = {'all_tenants': all_tenants}
if tenant_id:
params['tenant'] = tenant_id
path = '/v1/environments?{query}'.format(
query=urllib.parse.urlencode({'all_tenants': all_tenants}))
query=urllib.parse.urlencode(params))
return self._list(path, 'environments')
def create(self, data):

View File

@ -41,12 +41,16 @@ _bool_from_str_strict = functools.partial(
@utils.arg('--all-tenants', action='store_true', default=False,
help='Allows to list environments from all tenants'
' (admin only).')
@utils.arg('--tenant', metavar="<TENANT_ID>", default=None,
help='Allows to list environments for a given tenant'
' (admin only).')
def do_environment_list(mc, args=None):
"""List the environments."""
if args is None:
args = {}
all_tenants = getattr(args, 'all_tenants', False)
environments = mc.environments.list(all_tenants)
tenant = getattr(args, 'tenant', None)
environments = mc.environments.list(all_tenants, tenant)
_print_environment_list(environments)

View File

@ -0,0 +1,4 @@
---
features:
- The client now is able to consume the updated `List Environments` API call,
which may be used to filter environments by an owner project (tenant).