From e72072adc3b62b5ef8e3076169fed19dea9995f7 Mon Sep 17 00:00:00 2001
From: Terry Howe <terrylhowe@gmail.com>
Date: Mon, 31 Mar 2014 14:48:42 -0600
Subject: [PATCH] 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
---
 openstackclient/identity/v2_0/user.py         | 19 +++++++++++++++++--
 .../tests/identity/v2_0/test_user.py          | 12 +++++++++---
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/openstackclient/identity/v2_0/user.py b/openstackclient/identity/v2_0/user.py
index 628be4b85c..688306baa1 100644
--- a/openstackclient/identity/v2_0/user.py
+++ b/openstackclient/identity/v2_0/user.py
@@ -156,6 +156,7 @@ class ListUser(lister.Lister):
 
     def take_action(self, parsed_args):
         self.log.debug('take_action(%s)' % parsed_args)
+        identity_client = self.app.client_manager.identity
 
         def _format_project(project):
             if not project:
@@ -165,6 +166,14 @@ class ListUser(lister.Lister):
             else:
                 return project
 
+        project = None
+        if parsed_args.project:
+            project = utils.find_resource(
+                identity_client.tenants,
+                parsed_args.project,
+            )
+            project = project.id
+
         if parsed_args.long:
             columns = (
                 'ID',
@@ -183,14 +192,20 @@ class ListUser(lister.Lister):
             # Cache the project list
             project_cache = {}
             try:
-                for p in self.app.client_manager.identity.tenants.list():
+                for p in identity_client.tenants.list():
                     project_cache[p.id] = p
             except Exception:
                 # Just forget it if there's any trouble
                 pass
         else:
             columns = column_headers = ('ID', 'Name')
-        data = self.app.client_manager.identity.users.list()
+        data = identity_client.users.list(tenant_id=project)
+
+        if parsed_args.project:
+            d = {}
+            for s in data:
+                d[s.id] = s
+            data = d.values()
 
         if parsed_args.long:
             # FIXME(dtroyer): Sometimes user objects have 'tenant_id' instead
diff --git a/openstackclient/tests/identity/v2_0/test_user.py b/openstackclient/tests/identity/v2_0/test_user.py
index dfb358ffd0..e191431c94 100644
--- a/openstackclient/tests/identity/v2_0/test_user.py
+++ b/openstackclient/tests/identity/v2_0/test_user.py
@@ -381,6 +381,11 @@ class TestUserList(TestUser):
     def setUp(self):
         super(TestUserList, self).setUp()
 
+        self.projects_mock.get.return_value = fakes.FakeResource(
+            None,
+            copy.deepcopy(identity_fakes.PROJECT_2),
+            loaded=True,
+        )
         self.projects_mock.list.return_value = [
             fakes.FakeResource(
                 None,
@@ -408,7 +413,7 @@ class TestUserList(TestUser):
         # DisplayCommandBase.take_action() returns two tuples
         columns, data = self.cmd.take_action(parsed_args)
 
-        self.users_mock.list.assert_called_with()
+        self.users_mock.list.assert_called_with(tenant_id=None)
 
         collist = ('ID', 'Name')
         self.assertEqual(columns, collist)
@@ -426,11 +431,12 @@ class TestUserList(TestUser):
             ('project', identity_fakes.project_id),
         ]
         parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+        project_id = identity_fakes.PROJECT_2['id']
 
         # DisplayCommandBase.take_action() returns two tuples
         columns, data = self.cmd.take_action(parsed_args)
 
-        self.users_mock.list.assert_called_with()
+        self.users_mock.list.assert_called_with(tenant_id=project_id)
 
         collist = ('ID', 'Name')
         self.assertEqual(columns, collist)
@@ -452,7 +458,7 @@ class TestUserList(TestUser):
         # DisplayCommandBase.take_action() returns two tuples
         columns, data = self.cmd.take_action(parsed_args)
 
-        self.users_mock.list.assert_called_with()
+        self.users_mock.list.assert_called_with(tenant_id=None)
 
         collist = ('ID', 'Name', 'Project', 'Email', 'Enabled')
         self.assertEqual(columns, collist)