diff options
| author | David Rosales <darosale@us.ibm.com> | 2016-04-29 12:12:52 -0500 |
|---|---|---|
| committer | Steve Martinelli <s.martinelli@gmail.com> | 2016-06-22 21:55:46 -0700 |
| commit | 337d013c94378a4b3f0e8f90e4f5bd745448658f (patch) | |
| tree | 4dc43c30de328f0b83147e078faee22560b88bac /openstackclient/identity/common.py | |
| parent | a60f16b9385d7ee2322a25b5728363fbf8f5154b (diff) | |
| download | python-openstackclient-337d013c94378a4b3f0e8f90e4f5bd745448658f.tar.gz | |
Use resource id when name given for identity show
Currently a user is allowed to specify either a resource ID or name
when running openstack identity comands. In some cases, when a name
is specified instead of an ID, the command will return as not able
to find the resource when it in fact does exist.
The changes here are to check the client against the token on such
requests and to extract the ID of the resource specified if enough
information exists between the two. We then use the ID associated
with the resource to complete the user requests.
Change-Id: I40713b0ded42063b786dc21247e854224b9d2fe2
Closes-Bug: #1561599
Diffstat (limited to 'openstackclient/identity/common.py')
| -rw-r--r-- | openstackclient/identity/common.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/openstackclient/identity/common.py b/openstackclient/identity/common.py index cd767d2f..379f4114 100644 --- a/openstackclient/identity/common.py +++ b/openstackclient/identity/common.py @@ -47,6 +47,39 @@ def find_service(identity_client, name_type_or_id): raise exceptions.CommandError(msg % name_type_or_id) +def _get_token_resource(client, resource, parsed_name): + """Peek into the user's auth token to get resource IDs + + Look into a user's token to try and find the ID of a domain, project or + user, when given the name. Typically non-admin users will interact with + the CLI using names. However, by default, keystone does not allow look up + by name since it would involve listing all entities. Instead opt to use + the correct ID (from the token) instead. + :param client: An identity client + :param resource: A resource to look at in the token, this may be `domain`, + `project_domain`, `user_domain`, `project`, or `user`. + :param parsed_name: This is input from parsed_args that the user is hoping + to find in the token. + + :returns: The ID of the resource from the token, or the original value from + parsed_args if it does not match. + """ + + try: + token = client.auth.client.get_token() + token_data = client.tokens.get_token_data(token) + token_dict = token_data['token'] + + # NOTE(stevemar): If domain is passed, just look at the project domain. + if resource == 'domain': + token_dict = token_dict['project'] + obj = token_dict[resource] + return obj['id'] if obj['name'] == parsed_name else parsed_name + # diaper defense in case parsing the token fails + except Exception: # noqa + return parsed_name + + def _get_domain_id_if_requested(identity_client, domain_name_or_id): if not domain_name_or_id: return None |
