Skip to content

Commit e72072a

Browse files
committed
Fix the project option to user list so it filters
The --project option to the user list command was not implemented * Allow users to be filted by project * Support id or name of project with the find_resource command * Make sure the report does not contain duplicates Change-Id: Ic0e10cccd7749d38a7d4b80bbdc68e61a660084b Closes-Bug: #1177255
1 parent ee22070 commit e72072a

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎openstackclient/identity/v2_0/user.py‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def get_parser(self, prog_name):
156156

157157
def take_action(self, parsed_args):
158158
self.log.debug('take_action(%s)' % parsed_args)
159+
identity_client = self.app.client_manager.identity
159160

160161
def _format_project(project):
161162
if not project:
@@ -165,6 +166,14 @@ def _format_project(project):
165166
else:
166167
return project
167168

169+
project = None
170+
if parsed_args.project:
171+
project = utils.find_resource(
172+
identity_client.tenants,
173+
parsed_args.project,
174+
)
175+
project = project.id
176+
168177
if parsed_args.long:
169178
columns = (
170179
'ID',
@@ -183,14 +192,20 @@ def _format_project(project):
183192
# Cache the project list
184193
project_cache = {}
185194
try:
186-
for p in self.app.client_manager.identity.tenants.list():
195+
for p in identity_client.tenants.list():
187196
project_cache[p.id] = p
188197
except Exception:
189198
# Just forget it if there's any trouble
190199
pass
191200
else:
192201
columns = column_headers = ('ID', 'Name')
193-
data = self.app.client_manager.identity.users.list()
202+
data = identity_client.users.list(tenant_id=project)
203+
204+
if parsed_args.project:
205+
d = {}
206+
for s in data:
207+
d[s.id] = s
208+
data = d.values()
194209

195210
if parsed_args.long:
196211
# FIXME(dtroyer): Sometimes user objects have 'tenant_id' instead

‎openstackclient/tests/identity/v2_0/test_user.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,11 @@ class TestUserList(TestUser):
381381
def setUp(self):
382382
super(TestUserList, self).setUp()
383383

384+
self.projects_mock.get.return_value = fakes.FakeResource(
385+
None,
386+
copy.deepcopy(identity_fakes.PROJECT_2),
387+
loaded=True,
388+
)
384389
self.projects_mock.list.return_value = [
385390
fakes.FakeResource(
386391
None,
@@ -408,7 +413,7 @@ def test_user_list_no_options(self):
408413
# DisplayCommandBase.take_action() returns two tuples
409414
columns, data = self.cmd.take_action(parsed_args)
410415

411-
self.users_mock.list.assert_called_with()
416+
self.users_mock.list.assert_called_with(tenant_id=None)
412417

413418
collist = ('ID', 'Name')
414419
self.assertEqual(columns, collist)
@@ -426,11 +431,12 @@ def test_user_list_project(self):
426431
('project', identity_fakes.project_id),
427432
]
428433
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
434+
project_id = identity_fakes.PROJECT_2['id']
429435

430436
# DisplayCommandBase.take_action() returns two tuples
431437
columns, data = self.cmd.take_action(parsed_args)
432438

433-
self.users_mock.list.assert_called_with()
439+
self.users_mock.list.assert_called_with(tenant_id=project_id)
434440

435441
collist = ('ID', 'Name')
436442
self.assertEqual(columns, collist)
@@ -452,7 +458,7 @@ def test_user_list_long(self):
452458
# DisplayCommandBase.take_action() returns two tuples
453459
columns, data = self.cmd.take_action(parsed_args)
454460

455-
self.users_mock.list.assert_called_with()
461+
self.users_mock.list.assert_called_with(tenant_id=None)
456462

457463
collist = ('ID', 'Name', 'Project', 'Email', 'Enabled')
458464
self.assertEqual(columns, collist)

0 commit comments

Comments
 (0)