Skip to content

Commit 606fd13

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "identity: Normalise output of application credentials commands"
2 parents 88b59d8 + f1cd38a commit 606fd13

File tree

5 files changed

+142
-135
lines changed

5 files changed

+142
-135
lines changed

openstackclient/identity/v3/application_credential.py

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,99 @@
2020
import logging
2121
import uuid
2222

23+
from cliff import columns as cliff_columns
2324
from osc_lib.command import command
2425
from osc_lib import exceptions
2526
from osc_lib import utils
2627

2728
from openstackclient.i18n import _
2829
from openstackclient.identity import common
2930

30-
3131
LOG = logging.getLogger(__name__)
3232

3333

34+
class RolesColumn(cliff_columns.FormattableColumn):
35+
"""Generate a formatted string of role names."""
36+
37+
def human_readable(self):
38+
return utils.format_list(r['name'] for r in self._value)
39+
40+
41+
def _format_application_credential(
42+
application_credential, *, include_secret=False
43+
):
44+
column_headers: tuple[str, ...] = (
45+
'ID',
46+
'Name',
47+
'Description',
48+
'Project ID',
49+
'Roles',
50+
'Unrestricted',
51+
'Access Rules',
52+
'Expires At',
53+
)
54+
columns: tuple[str, ...] = (
55+
'id',
56+
'name',
57+
'description',
58+
'project_id',
59+
'roles',
60+
'unrestricted',
61+
'access_rules',
62+
'expires_at',
63+
)
64+
if include_secret:
65+
column_headers += ('Secret',)
66+
columns += ('secret',)
67+
68+
return (
69+
column_headers,
70+
utils.get_item_properties(
71+
application_credential, columns, formatters={'roles': RolesColumn}
72+
),
73+
)
74+
75+
76+
def _format_application_credentials(application_credentials):
77+
column_headers = (
78+
'ID',
79+
'Name',
80+
'Description',
81+
'Project ID',
82+
'Roles',
83+
'Unrestricted',
84+
'Access Rules',
85+
'Expires At',
86+
)
87+
columns = (
88+
'id',
89+
'name',
90+
'description',
91+
'project_id',
92+
'roles',
93+
'unrestricted',
94+
'access_rules',
95+
'expires_at',
96+
)
97+
98+
return (
99+
column_headers,
100+
(
101+
utils.get_item_properties(
102+
x, columns, formatters={'roles': RolesColumn}
103+
)
104+
for x in application_credentials
105+
),
106+
)
107+
108+
34109
# TODO(stephenfin): Move this to osc_lib since it's useful elsewhere
35110
def is_uuid_like(value) -> bool:
36111
"""Returns validation of a value as a UUID.
37112
38113
:param val: Value to verify
39114
:type val: string
40115
:returns: bool
41-
42-
.. versionchanged:: 1.1.1
43-
Support non-lowercase UUIDs.
44116
"""
45117
try:
46118
formatted_value = (
@@ -179,31 +251,8 @@ def take_action(self, parsed_args):
179251
access_rules=access_rules,
180252
)
181253

182-
# Format roles into something sensible
183-
if application_credential['roles']:
184-
roles = application_credential['roles']
185-
msg = ' '.join(r['name'] for r in roles)
186-
application_credential['roles'] = msg
187-
188-
columns = (
189-
'id',
190-
'name',
191-
'description',
192-
'project_id',
193-
'roles',
194-
'unrestricted',
195-
'access_rules',
196-
'expires_at',
197-
'secret',
198-
)
199-
return (
200-
columns,
201-
(
202-
utils.get_dict_properties(
203-
application_credential,
204-
columns,
205-
)
206-
),
254+
return _format_application_credential(
255+
application_credential, include_secret=True
207256
)
208257

209258

@@ -252,6 +301,8 @@ def take_action(self, parsed_args):
252301
) % {'errors': errors, 'total': total}
253302
raise exceptions.CommandError(msg)
254303

304+
return None
305+
255306

256307
class ListApplicationCredential(command.Lister):
257308
_description = _("List application credentials")
@@ -276,39 +327,12 @@ def take_action(self, parsed_args):
276327
conn = self.app.client_manager.sdk_connection
277328
user_id = conn.config.get_auth().get_user_id(conn.identity)
278329

279-
data = identity_client.application_credentials(user=user_id)
280-
281-
data_formatted = []
282-
for ac in data:
283-
# Format roles into something sensible
284-
roles = ac['roles']
285-
msg = ' '.join(r['name'] for r in roles)
286-
ac['roles'] = msg
287-
288-
data_formatted.append(ac)
289-
290-
columns = (
291-
'ID',
292-
'Name',
293-
'Description',
294-
'Project ID',
295-
'Roles',
296-
'Unrestricted',
297-
'Access Rules',
298-
'Expires At',
299-
)
300-
return (
301-
columns,
302-
(
303-
utils.get_item_properties(
304-
s,
305-
columns,
306-
formatters={},
307-
)
308-
for s in data_formatted
309-
),
330+
application_credentials = identity_client.application_credentials(
331+
user=user_id
310332
)
311333

334+
return _format_application_credentials(application_credentials)
335+
312336

313337
class ShowApplicationCredential(command.ShowOne):
314338
_description = _("Display application credential details")
@@ -327,31 +351,8 @@ def take_action(self, parsed_args):
327351
conn = self.app.client_manager.sdk_connection
328352
user_id = conn.config.get_auth().get_user_id(conn.identity)
329353

330-
app_cred = identity_client.find_application_credential(
354+
application_credential = identity_client.find_application_credential(
331355
user_id, parsed_args.application_credential
332356
)
333357

334-
# Format roles into something sensible
335-
roles = app_cred['roles']
336-
msg = ' '.join(r['name'] for r in roles)
337-
app_cred['roles'] = msg
338-
339-
columns = (
340-
'id',
341-
'name',
342-
'description',
343-
'project_id',
344-
'roles',
345-
'unrestricted',
346-
'access_rules',
347-
'expires_at',
348-
)
349-
return (
350-
columns,
351-
(
352-
utils.get_dict_properties(
353-
app_cred,
354-
columns,
355-
)
356-
),
357-
)
358+
return _format_application_credential(application_credential)

openstackclient/tests/functional/identity/v3/test_access_rule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def setUp(self):
6262

6363
items = self.parse_show_as_object(raw_output)
6464
self.access_rule_ids = [
65-
x['id'] for x in ast.literal_eval(items['access_rules'])
65+
x['id'] for x in ast.literal_eval(items['Access Rules'])
6666
]
6767
self.addCleanup(
6868
self.openstack,

openstackclient/tests/functional/identity/v3/test_application_credential.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
class ApplicationCredentialTests(common.IdentityTests):
2323
APPLICATION_CREDENTIAL_FIELDS = [
24-
'id',
25-
'name',
26-
'project_id',
27-
'description',
28-
'roles',
29-
'expires_at',
30-
'unrestricted',
24+
'ID',
25+
'Name',
26+
'Project ID',
27+
'Description',
28+
'Roles',
29+
'Expires At',
30+
'Unrestricted',
3131
]
3232
APPLICATION_CREDENTIAL_LIST_HEADERS = [
3333
'ID',

0 commit comments

Comments
 (0)