From 364071a90bfe9dcec1d02a349c33dc8422fc14f3 Mon Sep 17 00:00:00 2001 From: Steve Martinelli Date: Tue, 7 Oct 2014 02:15:15 -0400 Subject: [PATCH] Add domain parameters to user show for Identity V3 Update `user show` for Identity V3 to account for a domain argument, in doing so, also update `find resource` to be more flexible by allowing **kwargs. Also update `group show` and `project show` since they follow the same logic as a user within a group. Change-Id: Ib828e4dbeb0bd31164396069ce8a64c873179779 Closes-Bug: #1378165 --- openstackclient/common/utils.py | 28 +++++++++++++++++++++++--- openstackclient/identity/v3/group.py | 23 ++++++++++++++++----- openstackclient/identity/v3/project.py | 23 +++++++++++++++------ openstackclient/identity/v3/user.py | 22 +++++++++++++------- 4 files changed, 75 insertions(+), 21 deletions(-) diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py index 818f8d4771..9ad3823cc8 100644 --- a/openstackclient/common/utils.py +++ b/openstackclient/common/utils.py @@ -26,8 +26,27 @@ from oslo.utils import importutils from openstackclient.common import exceptions -def find_resource(manager, name_or_id): - """Helper for the _find_* methods.""" +def find_resource(manager, name_or_id, **kwargs): + """Helper for the _find_* methods. + + :param manager: A client manager class + :param name_or_id: The resource we are trying to find + :param kwargs: To be used in calling .find() + :rtype: The found resource + + This method will attempt to find a resource in a variety of ways. + Primarily .get() methods will be called with `name_or_id` as an integer + value, and tried again as a string value. + + If both fail, then a .find() is attempted, which is essentially calling + a .list() function with a 'name' query parameter that is set to + `name_or_id`. + + Lastly, if any kwargs are passed in, they will be treated as additional + query parameters. This is particularly handy in the case of finding + resources in a domain. + + """ # Try to get entity as integer id try: @@ -49,7 +68,10 @@ def find_resource(manager, name_or_id): except Exception: pass - kwargs = {} + if len(kwargs) == 0: + kwargs = {} + + # Prepare the kwargs for calling find if 'NAME_ATTR' in manager.resource_class.__dict__: # novaclient does this for oddball resources kwargs[manager.resource_class.NAME_ATTR] = name_or_id diff --git a/openstackclient/identity/v3/group.py b/openstackclient/identity/v3/group.py index 4eb144896d..fdb9501ad6 100644 --- a/openstackclient/identity/v3/group.py +++ b/openstackclient/identity/v3/group.py @@ -24,6 +24,7 @@ from cliff import lister from cliff import show from openstackclient.common import utils +from openstackclient.identity import common class AddUserToGroup(command.Command): @@ -321,14 +322,26 @@ class ShowGroup(show.ShowOne): parser.add_argument( 'group', metavar='', - help='Name or ID of group to display') + help='Name or ID of group to display', + ) + parser.add_argument( + '--domain', + metavar='', + help='Domain where group resides (name or ID)', + ) return parser def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) identity_client = self.app.client_manager.identity - group = utils.find_resource(identity_client.groups, parsed_args.group) - info = {} - info.update(group._info) - return zip(*sorted(six.iteritems(info))) + if parsed_args.domain: + domain = common.find_domain(identity_client, parsed_args.domain) + group = utils.find_resource(identity_client.groups, + parsed_args.group, + domain_id=domain.id) + else: + group = utils.find_resource(identity_client.groups, + parsed_args.group) + + return zip(*sorted(six.iteritems(group._info))) diff --git a/openstackclient/identity/v3/project.py b/openstackclient/identity/v3/project.py index fa935f0bf5..ec8e5a3b1a 100644 --- a/openstackclient/identity/v3/project.py +++ b/openstackclient/identity/v3/project.py @@ -264,15 +264,26 @@ class ShowProject(show.ShowOne): parser.add_argument( 'project', metavar='', - help='Name or ID of project to display') + help='Name or ID of project to display', + ) + parser.add_argument( + '--domain', + metavar='', + help='Domain where project resides (name or ID)', + ) return parser def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) identity_client = self.app.client_manager.identity - project = utils.find_resource(identity_client.projects, - parsed_args.project) - info = {} - info.update(project._info) - return zip(*sorted(six.iteritems(info))) + if parsed_args.domain: + domain = common.find_domain(identity_client, parsed_args.domain) + project = utils.find_resource(identity_client.projects, + parsed_args.project, + domain_id=domain.id) + else: + project = utils.find_resource(identity_client.projects, + parsed_args.project) + + return zip(*sorted(six.iteritems(project._info))) diff --git a/openstackclient/identity/v3/user.py b/openstackclient/identity/v3/user.py index e4eb7526c2..73bb7f1308 100644 --- a/openstackclient/identity/v3/user.py +++ b/openstackclient/identity/v3/user.py @@ -23,6 +23,7 @@ from cliff import lister from cliff import show from openstackclient.common import utils +from openstackclient.identity import common class CreateUser(show.ShowOne): @@ -364,17 +365,24 @@ class ShowUser(show.ShowOne): metavar='', help='User to display (name or ID)', ) + parser.add_argument( + '--domain', + metavar='', + help='Domain where user resides (name or ID)', + ) return parser def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) identity_client = self.app.client_manager.identity - user = utils.find_resource( - identity_client.users, - parsed_args.user, - ) + if parsed_args.domain: + domain = common.find_domain(identity_client, parsed_args.domain) + user = utils.find_resource(identity_client.users, + parsed_args.user, + domain_id=domain.id) + else: + user = utils.find_resource(identity_client.users, + parsed_args.user) - info = {} - info.update(user._info) - return zip(*sorted(six.iteritems(info))) + return zip(*sorted(six.iteritems(user._info)))