add escalator cli framework
[escalator.git] / client / escalatorclient / openstack / common / apiclient / utils.py
1 #
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
5 #
6 #         http://www.apache.org/licenses/LICENSE-2.0
7 #
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
12 #    under the License.
13
14 ########################################################################
15 #
16 # THIS MODULE IS DEPRECATED
17 #
18 # Please refer to
19 # https://etherpad.openstack.org/p/kilo-escalatorclient-library-proposals for
20 # the discussion leading to this deprecation.
21 #
22 # We recommend checking out the python-openstacksdk project
23 # (https://launchpad.net/python-openstacksdk) instead.
24 #
25 ########################################################################
26
27 from oslo_utils import encodeutils
28 from oslo_utils import uuidutils
29 import six
30
31 from escalatorclient.openstack.common._i18n import _
32 from escalatorclient.openstack.common.apiclient import exceptions
33
34
35 def find_resource(manager, name_or_id, **find_args):
36     """Look for resource in a given manager.
37
38     Used as a helper for the _find_* methods.
39     Example:
40
41     .. code-block:: python
42
43         def _find_hypervisor(cs, hypervisor):
44             #Get a hypervisor by name or ID.
45             return cliutils.find_resource(cs.hypervisors, hypervisor)
46     """
47     # first try to get entity as integer id
48     try:
49         return manager.get(int(name_or_id))
50     except (TypeError, ValueError, exceptions.NotFound):
51         pass
52
53     # now try to get entity as uuid
54     try:
55         if six.PY2:
56             tmp_id = encodeutils.safe_encode(name_or_id)
57         else:
58             tmp_id = encodeutils.safe_decode(name_or_id)
59
60         if uuidutils.is_uuid_like(tmp_id):
61             return manager.get(tmp_id)
62     except (TypeError, ValueError, exceptions.NotFound):
63         pass
64
65     # for str id which is not uuid
66     if getattr(manager, 'is_alphanum_id_allowed', False):
67         try:
68             return manager.get(name_or_id)
69         except exceptions.NotFound:
70             pass
71
72     try:
73         try:
74             return manager.find(human_id=name_or_id, **find_args)
75         except exceptions.NotFound:
76             pass
77
78         # finally try to find entity by name
79         try:
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.") % \
88                 {
89                     "name": manager.resource_class.__name__.lower(),
90                     "name_or_id": name_or_id
91             }
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.") % \
96             {
97                 "name": manager.resource_class.__name__.lower(),
98                 "name_or_id": name_or_id
99         }
100         raise exceptions.CommandError(msg)