2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
3 # not use this file except in compliance with the License. You may obtain
4 # a copy of the License at
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 # License for the specific language governing permissions and limitations
14 ########################################################################
16 # THIS MODULE IS DEPRECATED
19 # https://etherpad.openstack.org/p/kilo-escalatorclient-library-proposals for
20 # the discussion leading to this deprecation.
22 # We recommend checking out the python-openstacksdk project
23 # (https://launchpad.net/python-openstacksdk) instead.
25 ########################################################################
27 from oslo_utils import encodeutils
28 from oslo_utils import uuidutils
31 from escalatorclient.openstack.common._i18n import _
32 from escalatorclient.openstack.common.apiclient import exceptions
35 def find_resource(manager, name_or_id, **find_args):
36 """Look for resource in a given manager.
38 Used as a helper for the _find_* methods.
41 .. code-block:: python
43 def _find_hypervisor(cs, hypervisor):
44 #Get a hypervisor by name or ID.
45 return cliutils.find_resource(cs.hypervisors, hypervisor)
47 # first try to get entity as integer id
49 return manager.get(int(name_or_id))
50 except (TypeError, ValueError, exceptions.NotFound):
53 # now try to get entity as uuid
56 tmp_id = encodeutils.safe_encode(name_or_id)
58 tmp_id = encodeutils.safe_decode(name_or_id)
60 if uuidutils.is_uuid_like(tmp_id):
61 return manager.get(tmp_id)
62 except (TypeError, ValueError, exceptions.NotFound):
65 # for str id which is not uuid
66 if getattr(manager, 'is_alphanum_id_allowed', False):
68 return manager.get(name_or_id)
69 except exceptions.NotFound:
74 return manager.find(human_id=name_or_id, **find_args)
75 except exceptions.NotFound:
78 # finally try to find entity by name
80 resource = getattr(manager, 'resource_class', None)
81 name_attr = resource.NAME_ATTR if resource else 'name'
82 kwargs = {name_attr: name_or_id}
83 kwargs.update(find_args)
84 return manager.find(**kwargs)
85 except exceptions.NotFound:
86 msg = _("No %(name)s with a name or "
87 "ID of '%(name_or_id)s' exists.") % \
89 "name": manager.resource_class.__name__.lower(),
90 "name_or_id": name_or_id
92 raise exceptions.CommandError(msg)
93 except exceptions.NoUniqueMatch:
94 msg = _("Multiple %(name)s matches found for "
95 "'%(name_or_id)s', use an ID to be more specific.") % \
97 "name": manager.resource_class.__name__.lower(),
98 "name_or_id": name_or_id
100 raise exceptions.CommandError(msg)