Skip to content

Commit 680e3e3

Browse files
committed
identity: Migrate 'user' commands to SDK
Change-Id: I06f3848812bce60c65909f1311f36b70eba427d4
1 parent c86b9d8 commit 680e3e3

3 files changed

Lines changed: 425 additions & 392 deletions

File tree

openstackclient/identity/v3/user.py

Lines changed: 133 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import copy
1919
import logging
2020

21-
from keystoneauth1 import exceptions as ks_exc
21+
from openstack import exceptions as sdk_exc
2222
from osc_lib.command import command
2323
from osc_lib import exceptions
2424
from osc_lib import utils
@@ -30,6 +30,33 @@
3030
LOG = logging.getLogger(__name__)
3131

3232

33+
def _format_user(user):
34+
columns = (
35+
'default_project_id',
36+
'domain_id',
37+
'email',
38+
'is_enabled',
39+
'id',
40+
'name',
41+
'description',
42+
'password_expires_at',
43+
)
44+
column_headers = (
45+
'default_project_id',
46+
'domain_id',
47+
'email',
48+
'enabled',
49+
'id',
50+
'name',
51+
'description',
52+
'password_expires_at',
53+
)
54+
return (
55+
column_headers,
56+
utils.get_item_properties(user, columns),
57+
)
58+
59+
3360
def _get_options_for_user(identity_client, parsed_args):
3461
options = {}
3562
if parsed_args.ignore_lockout_failure_attempts:
@@ -220,25 +247,26 @@ def get_parser(self, prog_name):
220247
return parser
221248

222249
def take_action(self, parsed_args):
223-
identity_client = self.app.client_manager.identity
224-
225-
project_id = None
226-
if parsed_args.project:
227-
project_id = common.find_project(
228-
identity_client,
229-
parsed_args.project,
230-
parsed_args.project_domain,
231-
).id
250+
identity_client = self.app.client_manager.sdk_connection.identity
232251

233252
domain_id = None
234253
if parsed_args.domain:
235-
domain_id = common.find_domain(
236-
identity_client, parsed_args.domain
254+
domain_id = identity_client.find_domain(
255+
name_or_id=parsed_args.domain,
256+
ignore_missing=False,
237257
).id
238258

239-
enabled = True
259+
project_id = None
260+
if parsed_args.project:
261+
project_id = identity_client.find_project(
262+
name_or_id=parsed_args.project,
263+
ignore_missing=False,
264+
domain_id=domain_id,
265+
).id
266+
267+
is_enabled = True
240268
if parsed_args.disable:
241-
enabled = False
269+
is_enabled = False
242270
if parsed_args.password_prompt:
243271
parsed_args.password = utils.get_password(self.app.stdin)
244272

@@ -252,29 +280,28 @@ def take_action(self, parsed_args):
252280
options = _get_options_for_user(identity_client, parsed_args)
253281

254282
try:
255-
user = identity_client.users.create(
283+
user = identity_client.create_user(
284+
default_project_id=project_id,
285+
description=parsed_args.description,
286+
domain_id=domain_id,
287+
email=parsed_args.email,
288+
is_enabled=is_enabled,
256289
name=parsed_args.name,
257-
domain=domain_id,
258-
default_project=project_id,
259290
password=parsed_args.password,
260-
email=parsed_args.email,
261-
description=parsed_args.description,
262-
enabled=enabled,
263291
options=options,
264292
)
265-
except ks_exc.Conflict:
293+
except sdk_exc.ConflictException:
266294
if parsed_args.or_show:
267-
user = utils.find_resource(
268-
identity_client.users,
269-
parsed_args.name,
295+
user = identity_client.find_user(
296+
name_or_id=parsed_args.name,
270297
domain_id=domain_id,
298+
ignore_missing=False,
271299
)
272300
LOG.info(_('Returning existing user %s'), user.name)
273301
else:
274302
raise
275303

276-
user._info.pop('links')
277-
return zip(*sorted(user._info.items()))
304+
return _format_user(user)
278305

279306

280307
class DeleteUser(command.Command):
@@ -296,21 +323,28 @@ def get_parser(self, prog_name):
296323
return parser
297324

298325
def take_action(self, parsed_args):
299-
identity_client = self.app.client_manager.identity
326+
identity_client = self.app.client_manager.sdk_connection.identity
300327

301328
domain = None
302329
if parsed_args.domain:
303-
domain = common.find_domain(identity_client, parsed_args.domain)
330+
domain = identity_client.find_domain(
331+
name_or_id=parsed_args.domain,
332+
ignore_missing=True,
333+
)
304334
errors = 0
305335
for user in parsed_args.users:
306336
try:
307337
if domain is not None:
308-
user_obj = utils.find_resource(
309-
identity_client.users, user, domain_id=domain.id
338+
user_obj = identity_client.find_user(
339+
name_or_id=user,
340+
domain_id=domain.id,
341+
ignore_missing=False,
310342
)
311343
else:
312-
user_obj = utils.find_resource(identity_client.users, user)
313-
identity_client.users.delete(user_obj.id)
344+
user_obj = identity_client.find_user(
345+
name_or_id=user, ignore_missing=False
346+
)
347+
identity_client.delete_user(user_obj.id, ignore_missing=False)
314348
except Exception as e:
315349
errors += 1
316350
LOG.error(
@@ -360,32 +394,36 @@ def get_parser(self, prog_name):
360394
return parser
361395

362396
def take_action(self, parsed_args):
363-
identity_client = self.app.client_manager.identity
397+
identity_client = self.app.client_manager.sdk_connection.identity
364398

365399
domain = None
366400
if parsed_args.domain:
367-
domain = common.find_domain(identity_client, parsed_args.domain).id
401+
domain = identity_client.find_domain(
402+
name_or_id=parsed_args.domain,
403+
).id
368404

369405
group = None
370406
if parsed_args.group:
371-
group = common.find_group(
372-
identity_client, parsed_args.group, parsed_args.domain
407+
group = identity_client.find_group(
408+
name_or_id=parsed_args.group,
409+
domain_id=parsed_args.domain,
410+
ignore_missing=False,
373411
).id
374412

375413
if parsed_args.project:
376414
if domain is not None:
377-
project = utils.find_resource(
378-
identity_client.projects,
379-
parsed_args.project,
415+
project = identity_client.find_project(
416+
name_or_id=parsed_args.project,
380417
domain_id=domain,
418+
ignore_missing=False,
381419
).id
382420
else:
383-
project = utils.find_resource(
384-
identity_client.projects,
385-
parsed_args.project,
421+
project = identity_client.find_project(
422+
name_or_id=parsed_args.project,
423+
ignore_missing=False,
386424
).id
387425

388-
assignments = identity_client.role_assignments.list(
426+
assignments = identity_client.role_assignments_filter(
389427
project=project
390428
)
391429

@@ -394,19 +432,19 @@ def take_action(self, parsed_args):
394432
# are looking for any role, let's just track unique user IDs.
395433
user_ids = set()
396434
for assignment in assignments:
397-
if hasattr(assignment, 'user'):
435+
if assignment.user:
398436
user_ids.add(assignment.user['id'])
399437

400438
# NOTE(stevemar): Call find_resource once we have unique IDs, so
401439
# it's fewer trips to the Identity API, then collect the data.
402440
data = []
403441
for user_id in user_ids:
404-
user = utils.find_resource(identity_client.users, user_id)
442+
user = identity_client.find_user(user_id, ignore_missing=False)
405443
data.append(user)
406444

407445
else:
408-
data = identity_client.users.list(
409-
domain=domain,
446+
data = identity_client.users(
447+
domain_id=domain,
410448
group=group,
411449
)
412450

@@ -419,11 +457,12 @@ def take_action(self, parsed_args):
419457
'Domain Id',
420458
'Description',
421459
'Email',
422-
'Enabled',
460+
'Is Enabled',
423461
]
424462
column_headers = copy.deepcopy(columns)
425463
column_headers[2] = 'Project'
426464
column_headers[3] = 'Domain'
465+
column_headers[6] = 'Enabled'
427466
else:
428467
columns = ['ID', 'Name']
429468
column_headers = columns
@@ -507,7 +546,7 @@ def get_parser(self, prog_name):
507546
return parser
508547

509548
def take_action(self, parsed_args):
510-
identity_client = self.app.client_manager.identity
549+
identity_client = self.app.client_manager.sdk_connection.identity
511550

512551
if parsed_args.password_prompt:
513552
parsed_args.password = utils.get_password(self.app.stdin)
@@ -524,14 +563,19 @@ def take_action(self, parsed_args):
524563
identity_client, 'user', parsed_args.user, parsed_args.domain
525564
)
526565
if parsed_args.domain:
527-
domain = common.find_domain(identity_client, parsed_args.domain)
528-
user = utils.find_resource(
529-
identity_client.users, user_str, domain_id=domain.id
566+
domain = identity_client.find_domain(
567+
name_or_id=parsed_args.domain,
568+
ignore_missing=False,
569+
)
570+
user = identity_client.find_user(
571+
name_or_id=user_str,
572+
domain_id=domain.id,
573+
ignore_missing=False,
530574
)
531575
else:
532-
user = utils.find_resource(
533-
identity_client.users,
534-
parsed_args.user,
576+
user = identity_client.find_user(
577+
name_or_id=parsed_args.user,
578+
ignore_missing=False,
535579
)
536580

537581
kwargs = {}
@@ -544,23 +588,27 @@ def take_action(self, parsed_args):
544588
if parsed_args.description:
545589
kwargs['description'] = parsed_args.description
546590
if parsed_args.project:
547-
project_id = common.find_project(
548-
identity_client,
549-
parsed_args.project,
550-
parsed_args.project_domain,
591+
project_domain_id = identity_client.find_domain(
592+
name_or_id=parsed_args.project_domain,
593+
ignore_missing=False,
551594
).id
552-
kwargs['default_project'] = project_id
553-
kwargs['enabled'] = user.enabled
595+
project_id = identity_client.find_project(
596+
name_or_id=parsed_args.project,
597+
ignore_missing=False,
598+
domain_id=project_domain_id,
599+
).id
600+
kwargs['default_project_id'] = project_id
601+
kwargs['is_enabled'] = user.is_enabled
554602
if parsed_args.enable:
555-
kwargs['enabled'] = True
603+
kwargs['is_enabled'] = True
556604
if parsed_args.disable:
557-
kwargs['enabled'] = False
605+
kwargs['is_enabled'] = False
558606

559607
options = _get_options_for_user(identity_client, parsed_args)
560608
if options:
561609
kwargs['options'] = options
562610

563-
identity_client.users.update(user.id, **kwargs)
611+
identity_client.update_user(user=user, **kwargs)
564612

565613

566614
class SetPasswordUser(command.Command):
@@ -583,7 +631,7 @@ def get_parser(self, prog_name):
583631
return parser
584632

585633
def take_action(self, parsed_args):
586-
identity_client = self.app.client_manager.identity
634+
identity_client = self.app.client_manager.sdk_connection.identity
587635

588636
# FIXME(gyee): there are two scenarios:
589637
#
@@ -625,7 +673,9 @@ def take_action(self, parsed_args):
625673
)
626674
)
627675

628-
identity_client.users.update_password(current_password, password)
676+
identity_client.update_user(
677+
current_password=current_password, password=password
678+
)
629679

630680

631681
class ShowUser(command.ShowOne):
@@ -646,18 +696,28 @@ def get_parser(self, prog_name):
646696
return parser
647697

648698
def take_action(self, parsed_args):
649-
identity_client = self.app.client_manager.identity
699+
identity_client = self.app.client_manager.sdk_connection.identity
650700

651701
user_str = common._get_token_resource(
652702
identity_client, 'user', parsed_args.user, parsed_args.domain
653703
)
704+
705+
domain = None
654706
if parsed_args.domain:
655-
domain = common.find_domain(identity_client, parsed_args.domain)
656-
user = utils.find_resource(
657-
identity_client.users, user_str, domain_id=domain.id
707+
domain = identity_client.find_domain(
708+
name_or_id=parsed_args.domain,
709+
ignore_missing=True,
710+
)
711+
if domain:
712+
user = identity_client.find_user(
713+
name_or_id=user_str,
714+
domain_id=domain.id,
715+
ignore_missing=False,
658716
)
659717
else:
660-
user = utils.find_resource(identity_client.users, user_str)
718+
user = identity_client.find_user(
719+
name_or_id=user_str,
720+
ignore_missing=False,
721+
)
661722

662-
user._info.pop('links')
663-
return zip(*sorted(user._info.items()))
723+
return _format_user(user)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ class IdentityTests(base.TestCase):
3333
'enabled',
3434
'id',
3535
'name',
36-
'name',
3736
'domain_id',
3837
'default_project_id',
3938
'description',

0 commit comments

Comments
 (0)