Skip to content

Commit 26946d5

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add trustor and trustee filtering to trusts list"
2 parents 5043626 + 64e4520 commit 26946d5

3 files changed

Lines changed: 202 additions & 2 deletions

File tree

openstackclient/identity/v3/trust.py

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,95 @@ def take_action(self, parsed_args):
176176
class ListTrust(command.Lister):
177177
_description = _("List trusts")
178178

179+
def get_parser(self, prog_name):
180+
parser = super().get_parser(prog_name)
181+
parser.add_argument(
182+
'--trustor',
183+
metavar='<trustor-user>',
184+
help=_('Trustor user to filter (name or ID)'),
185+
)
186+
parser.add_argument(
187+
'--trustee',
188+
metavar='<trustee-user>',
189+
help=_('Trustee user to filter (name or ID)'),
190+
)
191+
parser.add_argument(
192+
'--trustor-domain',
193+
metavar='<trustor-domain>',
194+
help=_('Domain that contains <trustor> (name or ID)'),
195+
)
196+
parser.add_argument(
197+
'--trustee-domain',
198+
metavar='<trustee-domain>',
199+
help=_('Domain that contains <trustee> (name or ID)'),
200+
)
201+
parser.add_argument(
202+
'--auth-user',
203+
action="store_true",
204+
dest='authuser',
205+
help=_('Only list trusts related to the authenticated user'),
206+
)
207+
return parser
208+
179209
def take_action(self, parsed_args):
210+
identity_client = self.app.client_manager.identity
211+
auth_ref = self.app.client_manager.auth_ref
212+
213+
if parsed_args.authuser and any([
214+
parsed_args.trustor,
215+
parsed_args.trustor_domain,
216+
parsed_args.trustee,
217+
parsed_args.trustee_domain,
218+
]):
219+
msg = _("--authuser cannot be used with --trustee or --trustor")
220+
raise exceptions.CommandError(msg)
221+
222+
if parsed_args.trustee_domain and not parsed_args.trustee:
223+
msg = _("Using --trustee-domain mandates the use of --trustee")
224+
raise exceptions.CommandError(msg)
225+
226+
if parsed_args.trustor_domain and not parsed_args.trustor:
227+
msg = _("Using --trustor-domain mandates the use of --trustor")
228+
raise exceptions.CommandError(msg)
229+
230+
if parsed_args.authuser:
231+
if auth_ref:
232+
user = common.find_user(
233+
identity_client,
234+
auth_ref.user_id
235+
)
236+
# We need two calls here as we want trusts with
237+
# either the trustor or the trustee set to current user
238+
# using a single call would give us trusts with both
239+
# trustee and trustor set to current user
240+
data1 = identity_client.trusts.list(trustor_user=user)
241+
data2 = identity_client.trusts.list(trustee_user=user)
242+
data = set(data1 + data2)
243+
else:
244+
trustor = None
245+
if parsed_args.trustor:
246+
trustor = common.find_user(
247+
identity_client,
248+
parsed_args.trustor,
249+
parsed_args.trustor_domain,
250+
)
251+
252+
trustee = None
253+
if parsed_args.trustee:
254+
trustee = common.find_user(
255+
identity_client,
256+
parsed_args.trustor,
257+
parsed_args.trustor_domain,
258+
)
259+
260+
data = self.app.client_manager.identity.trusts.list(
261+
trustor_user=trustor,
262+
trustee_user=trustee,
263+
)
264+
180265
columns = ('ID', 'Expires At', 'Impersonation', 'Project ID',
181266
'Trustee User ID', 'Trustor User ID')
182-
data = self.app.client_manager.identity.trusts.list()
267+
183268
return (columns,
184269
(utils.get_item_properties(
185270
s, columns,

openstackclient/tests/unit/identity/v3/test_trust.py

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,113 @@ def test_trust_list_no_options(self):
206206
# containing the data to be listed.
207207
columns, data = self.cmd.take_action(parsed_args)
208208

209-
self.trusts_mock.list.assert_called_with()
209+
self.trusts_mock.list.assert_called_with(
210+
trustor_user=None,
211+
trustee_user=None,
212+
)
213+
214+
collist = ('ID', 'Expires At', 'Impersonation', 'Project ID',
215+
'Trustee User ID', 'Trustor User ID')
216+
self.assertEqual(collist, columns)
217+
datalist = ((
218+
identity_fakes.trust_id,
219+
identity_fakes.trust_expires,
220+
identity_fakes.trust_impersonation,
221+
identity_fakes.project_id,
222+
identity_fakes.user_id,
223+
identity_fakes.user_id
224+
), )
225+
self.assertEqual(datalist, tuple(data))
226+
227+
def test_trust_list_auth_user(self):
228+
auth_ref = self.app.client_manager.auth_ref = mock.Mock()
229+
auth_ref.user_id.return_value = identity_fakes.user_id
230+
231+
arglist = ['--auth-user']
232+
verifylist = [
233+
('trustor', None),
234+
('trustee', None),
235+
('authuser', True),
236+
]
237+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
238+
239+
# In base command class Lister in cliff, abstract method take_action()
240+
# returns a tuple containing the column names and an iterable
241+
# containing the data to be listed.
242+
columns, data = self.cmd.take_action(parsed_args)
243+
244+
self.trusts_mock.list.assert_any_call(
245+
trustor_user=self.users_mock.get()
246+
)
247+
self.trusts_mock.list.assert_any_call(
248+
trustee_user=self.users_mock.get()
249+
)
250+
251+
collist = ('ID', 'Expires At', 'Impersonation', 'Project ID',
252+
'Trustee User ID', 'Trustor User ID')
253+
self.assertEqual(collist, columns)
254+
datalist = ((
255+
identity_fakes.trust_id,
256+
identity_fakes.trust_expires,
257+
identity_fakes.trust_impersonation,
258+
identity_fakes.project_id,
259+
identity_fakes.user_id,
260+
identity_fakes.user_id
261+
), )
262+
self.assertEqual(datalist, tuple(data))
263+
264+
def test_trust_list_trustee(self):
265+
arglist = ['--trustee', identity_fakes.user_name]
266+
verifylist = [
267+
('trustor', None),
268+
('trustee', identity_fakes.user_name),
269+
('authuser', False),
270+
]
271+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
272+
273+
# In base command class Lister in cliff, abstract method take_action()
274+
# returns a tuple containing the column names and an iterable
275+
# containing the data to be listed.
276+
columns, data = self.cmd.take_action(parsed_args)
277+
278+
print(self.trusts_mock.list.call_args_list)
279+
self.trusts_mock.list.assert_any_call(
280+
trustee_user=self.users_mock.get(),
281+
trustor_user=None,
282+
)
283+
284+
collist = ('ID', 'Expires At', 'Impersonation', 'Project ID',
285+
'Trustee User ID', 'Trustor User ID')
286+
self.assertEqual(collist, columns)
287+
datalist = ((
288+
identity_fakes.trust_id,
289+
identity_fakes.trust_expires,
290+
identity_fakes.trust_impersonation,
291+
identity_fakes.project_id,
292+
identity_fakes.user_id,
293+
identity_fakes.user_id
294+
), )
295+
self.assertEqual(datalist, tuple(data))
296+
297+
def test_trust_list_trustor(self):
298+
arglist = ['--trustor', identity_fakes.user_name]
299+
verifylist = [
300+
('trustee', None),
301+
('trustor', identity_fakes.user_name),
302+
('authuser', False),
303+
]
304+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
305+
306+
# In base command class Lister in cliff, abstract method take_action()
307+
# returns a tuple containing the column names and an iterable
308+
# containing the data to be listed.
309+
columns, data = self.cmd.take_action(parsed_args)
310+
311+
print(self.trusts_mock.list.call_args_list)
312+
self.trusts_mock.list.assert_any_call(
313+
trustor_user=self.users_mock.get(),
314+
trustee_user=None,
315+
)
210316

211317
collist = ('ID', 'Expires At', 'Impersonation', 'Project ID',
212318
'Trustee User ID', 'Trustor User ID')
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Add missing ``--trustee``, ``--trustee-domain``, ``--trustor``,
5+
``--trustor-domain`` options to ``trust list`` command, to allow
6+
filtering trusts by trustee and trustor.
7+
- |
8+
Add ``--authuser`` option to ``trust list`` command, to allow
9+
displaying only trusts related to current authenticated user

0 commit comments

Comments
 (0)