From c45b1d7b230e900d0416a4953607e5d4e1dc9cfd Mon Sep 17 00:00:00 2001 From: sunyajing Date: Fri, 24 Jun 2016 12:40:29 +0800 Subject: Fix error for find_service() in identity if there are more than one services be found with one name, a NoUniqueMatch exception should be raised but we can see a NotFound Exception raised instead. It is because in "find_service()", we use "find_resource()" first, if "find_resource()" return a exception, we just think it is a NotFound Exception and continue to find by type but ignore a NoUniqueMatch exception of "find_resource()". This patch refactor the "find_service()" method to solve this problem. Change-Id: Id4619092c57f276ae0698c89df0d5503b7423a4e Co-Authored-By: Huanxuan Ao Closes-Bug:#1597296 --- openstackclient/identity/common.py | 41 ++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'openstackclient/identity/common.py') diff --git a/openstackclient/identity/common.py b/openstackclient/identity/common.py index 379f4114..1e40f396 100644 --- a/openstackclient/identity/common.py +++ b/openstackclient/identity/common.py @@ -30,21 +30,32 @@ def find_service(identity_client, name_type_or_id): """Find a service by id, name or type.""" try: - # search for the usual ID or name - return utils.find_resource(identity_client.services, name_type_or_id) - except exceptions.CommandError: - try: - # search for service type - return identity_client.services.find(type=name_type_or_id) - # FIXME(dtroyer): This exception should eventually come from - # common client exceptions - except identity_exc.NotFound: - msg = _("No service with a type, name or ID of '%s' exists.") - raise exceptions.CommandError(msg % name_type_or_id) - except identity_exc.NoUniqueMatch: - msg = _("Multiple service matches found for '%s', " - "use an ID to be more specific.") - raise exceptions.CommandError(msg % name_type_or_id) + # search for service id + return identity_client.services.get(name_type_or_id) + except identity_exc.NotFound: + # ignore NotFound exception, raise others + pass + + try: + # search for service name + return identity_client.services.find(name=name_type_or_id) + except identity_exc.NotFound: + pass + except identity_exc.NoUniqueMatch: + msg = _("Multiple service matches found for '%s', " + "use an ID to be more specific.") + raise exceptions.CommandError(msg % name_type_or_id) + + try: + # search for service type + return identity_client.services.find(type=name_type_or_id) + except identity_exc.NotFound: + msg = _("No service with a type, name or ID of '%s' exists.") + raise exceptions.CommandError(msg % name_type_or_id) + except identity_exc.NoUniqueMatch: + msg = _("Multiple service matches found for '%s', " + "use an ID to be more specific.") + raise exceptions.CommandError(msg % name_type_or_id) def _get_token_resource(client, resource, parsed_name): -- cgit v1.2.1