|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | +import six |
| 13 | + |
| 14 | +from openstack import exceptions |
| 15 | +from openstack import resource |
| 16 | + |
| 17 | + |
| 18 | +class Resource(resource.Resource): |
| 19 | + |
| 20 | + @classmethod |
| 21 | + def find(cls, session, name_or_id, ignore_missing=True, **params): |
| 22 | + """Find a resource by its name or id. |
| 23 | +
|
| 24 | + :param session: The session to use for making this request. |
| 25 | + :type session: :class:`~keystoneauth1.adapter.Adapter` |
| 26 | + :param name_or_id: This resource's identifier, if needed by |
| 27 | + the request. The default is ``None``. |
| 28 | + :param bool ignore_missing: When set to ``False`` |
| 29 | + :class:`~openstack.exceptions.ResourceNotFound` will be |
| 30 | + raised when the resource does not exist. |
| 31 | + When set to ``True``, None will be returned when |
| 32 | + attempting to find a nonexistent resource. |
| 33 | + :param dict params: Any additional parameters to be passed into |
| 34 | + underlying methods, such as to |
| 35 | + :meth:`~openstack.resource.Resource.existing` |
| 36 | + in order to pass on URI parameters. |
| 37 | +
|
| 38 | + :return: The :class:`Resource` object matching the given name or id |
| 39 | + or None if nothing matches. |
| 40 | + :raises: :class:`openstack.exceptions.DuplicateResource` if more |
| 41 | + than one resource is found for this request. |
| 42 | + :raises: :class:`openstack.exceptions.ResourceNotFound` if nothing |
| 43 | + is found and ignore_missing is ``False``. |
| 44 | + """ |
| 45 | + session = cls._get_session(session) |
| 46 | + # Try to short-circuit by looking directly for a matching ID. |
| 47 | + try: |
| 48 | + match = cls.existing( |
| 49 | + id=name_or_id, |
| 50 | + connection=session._get_connection(), |
| 51 | + **params) |
| 52 | + return match.fetch(session, **params) |
| 53 | + except exceptions.SDKException: |
| 54 | + # DNS may return 400 when we try to do GET with name |
| 55 | + pass |
| 56 | + |
| 57 | + if ('name' in cls._query_mapping._mapping.keys() |
| 58 | + and 'name' not in params): |
| 59 | + params['name'] = name_or_id |
| 60 | + |
| 61 | + data = cls.list(session, **params) |
| 62 | + |
| 63 | + result = cls._get_one_match(name_or_id, data) |
| 64 | + if result is not None: |
| 65 | + return result |
| 66 | + |
| 67 | + if ignore_missing: |
| 68 | + return None |
| 69 | + raise exceptions.ResourceNotFound( |
| 70 | + "No %s found for %s" % (cls.__name__, name_or_id)) |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def _get_next_link(cls, uri, response, data, marker, limit, total_yielded): |
| 74 | + next_link = None |
| 75 | + params = {} |
| 76 | + if isinstance(data, dict): |
| 77 | + links = data.get('links') |
| 78 | + if links: |
| 79 | + next_link = links.get('next') |
| 80 | + |
| 81 | + total = data.get('metadata', {}).get('total_count') |
| 82 | + if total: |
| 83 | + # We have a kill switch |
| 84 | + total_count = int(total) |
| 85 | + if total_count <= total_yielded: |
| 86 | + return None, params |
| 87 | + |
| 88 | + # Parse params from Link (next page URL) into params. |
| 89 | + # This prevents duplication of query parameters that with large |
| 90 | + # number of pages result in HTTP 414 error eventually. |
| 91 | + if next_link: |
| 92 | + parts = six.moves.urllib.parse.urlparse(next_link) |
| 93 | + query_params = six.moves.urllib.parse.parse_qs(parts.query) |
| 94 | + params.update(query_params) |
| 95 | + next_link = six.moves.urllib.parse.urljoin(next_link, |
| 96 | + parts.path) |
| 97 | + |
| 98 | + # If we still have no link, and limit was given and is non-zero, |
| 99 | + # and the number of records yielded equals the limit, then the user |
| 100 | + # is playing pagination ball so we should go ahead and try once more. |
| 101 | + if not next_link and limit: |
| 102 | + next_link = uri |
| 103 | + params['marker'] = marker |
| 104 | + params['limit'] = limit |
| 105 | + return next_link, params |
0 commit comments