Пример #1
0
    def get_discovery(self, session, url, authenticated=None):
        """Return the discovery object for a URL.

        Check the session and the plugin cache to see if we have already
        performed discovery on the URL and if so return it, otherwise create
        a new discovery object, cache it and return it.

        This function is expected to be used by subclasses and should not
        be needed by users.

        :param session: A session object to discover with.
        :type session: keystoneclient.session.Session
        :param str url: The url to lookup.
        :param bool authenticated: Include a token in the discovery call.
                                   (optional) Defaults to None (use a token
                                   if a plugin is installed).

        :raises keystoneclient.exceptions.DiscoveryFailure: if for some reason
                                                            the lookup fails.
        :raises keystoneclient.exceptions.HttpError: An error from an invalid
                                                     HTTP response.

        :returns: A discovery object with the results of looking up that URL.
        """
        # NOTE(jamielennox): we want to cache endpoints on the session as well
        # so that they maintain sharing between auth plugins. Create a cache on
        # the session if it doesn't exist already.
        try:
            session_endpoint_cache = session._identity_endpoint_cache
        except AttributeError:
            session_endpoint_cache = session._identity_endpoint_cache = {}

        # NOTE(jamielennox): There is a cache located on both the session
        # object and the auth plugin object so that they can be shared and the
        # cache is still usable
        for cache in (self._endpoint_cache, session_endpoint_cache):
            disc = cache.get(url)

            if disc:
                break
        else:
            disc = _discover.Discover(session,
                                      url,
                                      authenticated=authenticated)
            self._endpoint_cache[url] = disc
            session_endpoint_cache[url] = disc

        return disc
Пример #2
0
    def get_endpoint(self,
                     session,
                     service_type=None,
                     interface=None,
                     region_name=None,
                     service_name=None,
                     version=None,
                     **kwargs):
        """Return a valid endpoint for a service.

        If a valid token is not present then a new one will be fetched using
        the session and kwargs.

        :param string service_type: The type of service to lookup the endpoint
                                    for. This plugin will return None (failure)
                                    if service_type is not provided.
        :param string interface: The exposure of the endpoint. Should be
                                 `public`, `internal` or `admin`.
                                 Defaults to `public`.
        :param string region_name: The region the endpoint should exist in.
                                   (optional)
        :param string service_name: The name of the service in the catalog.
                                   (optional)
        :param tuple version: The minimum version number required for this
                              endpoint. (optional)

        :raises HttpError: An error from an invalid HTTP response.

        :return string or None: A valid endpoint URL or None if not available.
        """
        if not service_type:
            LOG.warn('Plugin cannot return an endpoint without knowing the '
                     'service type that is required. Add service_type to '
                     'endpoint filtering data.')
            return None

        if not interface:
            interface = 'public'

        service_catalog = self.get_access(session).service_catalog
        sc_url = service_catalog.url_for(service_type=service_type,
                                         endpoint_type=interface,
                                         region_name=region_name,
                                         service_name=service_name)

        if not version:
            # NOTE(jamielennox): This may not be the best thing to default to
            # but is here for backwards compatibility. It may be worth
            # defaulting to the most recent version.
            return sc_url

        disc = None

        # NOTE(jamielennox): we want to cache endpoints on the session as well
        # so that they maintain sharing between auth plugins. Create a cache on
        # the session if it doesn't exist already.
        try:
            session_endpoint_cache = session._identity_endpoint_cache
        except AttributeError:
            session_endpoint_cache = session._identity_endpoint_cache = {}

        # NOTE(jamielennox): There is a cache located on both the session
        # object and the auth plugin object so that they can be shared and the
        # cache is still usable
        for cache in (self._endpoint_cache, session_endpoint_cache):
            disc = cache.get(sc_url)

            if disc:
                break
        else:
            try:
                disc = _discover.Discover(session, sc_url)
            except (exceptions.HTTPError, exceptions.ConnectionError):
                LOG.warn(
                    'Failed to contact the endpoint at %s for discovery. '
                    'Fallback to using that endpoint as the '
                    'base url.', sc_url)

                return sc_url
            else:
                self._endpoint_cache[sc_url] = disc
                session_endpoint_cache[sc_url] = disc

        return disc.url_for(version)