def assert_min_max(in_ver, in_min, in_max, in_type, out_min, out_max):
     self.assertEqual(
         (out_min, out_max),
         discover._normalize_version_args(in_ver,
                                          in_min,
                                          in_max,
                                          service_type=in_type))
Beispiel #2
0
 def assert_min_max(in_ver, in_min, in_max, out_min, out_max):
     self.assertEqual(
         (out_min, out_max),
         discover._normalize_version_args(in_ver, in_min, in_max))
Beispiel #3
0
    def get_api_major_version(self, session, service_type=None, interface=None,
                              region_name=None, service_name=None,
                              version=None, allow=None,
                              allow_version_hack=True, skip_discovery=False,
                              discover_versions=False, min_version=None,
                              max_version=None, **kwargs):
        """Return the major API version for a service.

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

        version, min_version and max_version can all be given either as a
        string or a tuple.

        Valid interface types: `public` or `publicURL`,
                               `internal` or `internalURL`,
                               `admin` or 'adminURL`

        :param session: A session object that can be used for communication.
        :type session: keystoneauth1.session.Session
        :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 interface: Type of endpoint. Can be a single value or a list
                          of values. If it's a list of values, they will be
                          looked for in order of preference. Can also be
                          `keystoneauth1.plugin.AUTH_INTERFACE` to indicate
                          that the auth_url should be used instead of the
                          value in the catalog. (optional, 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 version: The minimum version number required for this
                        endpoint. (optional)
        :param dict allow: Extra filters to pass when discovering API
                           versions. (optional)
        :param bool allow_version_hack: Allow keystoneauth to hack up catalog
                                        URLS to support older schemes.
                                        (optional, default True)
        :param bool skip_discovery: Whether to skip version discovery even
                                    if a version has been given. This is useful
                                    if endpoint_override or similar has been
                                    given and grabbing additional information
                                    about the endpoint is not useful.
        :param bool discover_versions: Whether to get version metadata from
                                       the version discovery document even
                                       if it's not neccessary to fulfill the
                                       major version request. Defaults to False
                                       because get_endpoint doesn't need
                                       metadata. (optional, defaults to False)
        :param min_version: The minimum version that is acceptable. Mutually
                            exclusive with version. If min_version is given
                            with no max_version it is as if max version is
                            'latest'. (optional)
        :param max_version: The maximum version that is acceptable. Mutually
                            exclusive with version. If min_version is given
                            with no max_version it is as if max version is
                            'latest'. (optional)

        :raises keystoneauth1.exceptions.http.HttpError: An error from an
                                                         invalid HTTP response.

        :return: The major version of the API of the service discovered.
        :rtype: tuple or None

        .. note:: Implementation notes follow. Users should not need to wrap
                  their head around these implementation notes.
                  `get_api_major_version` should do what is expected with the
                  least possible cost while still consistently returning a
                  value if possible.

        There are many cases when major version can be satisfied
        without actually calling the discovery endpoint (like when the version
        is in the url). If the user has a cloud with the versioned endpoint
        ``https://volume.example.com/v3`` in the catalog for the
        ``block-storage`` service and they do::

          client = adapter.Adapter(
              session, service_type='block-storage', min_version=2,
              max_version=3)
          volume_version = client.get_api_major_version()

        The version actually be returned with no api calls other than getting
        the token. For that reason, :meth:`.get_api_major_version` first
        calls :meth:`.get_endpoint_data` with ``discover_versions=False``.

        If their catalog has an unversioned endpoint
        ``https://volume.example.com`` for the ``block-storage`` service
        and they do this::

          client = adapter.Adapter(session, service_type='block-storage')

        client is now set up to "use whatever is in the catalog". Since the
        url doesn't have a version, :meth:`.get_endpoint_data` with
        ``discover_versions=False`` will result in ``api_version=None``.
        (No version was requested so it didn't need to do the round trip)

        In order to find out what version the endpoint actually is, we must
        make a round trip. Therefore, if ``api_version`` is ``None`` after
        the first call, :meth:`.get_api_major_version` will make a second
        call to :meth:`.get_endpoint_data` with ``discover_versions=True``.

        """
        allow = allow or {}
        # Explode `version` into min_version and max_version - everything below
        # here uses the latter rather than the former.
        min_version, max_version = discover._normalize_version_args(
            version, min_version, max_version, service_type=service_type)
        # Using functools.partial here just to reduce copy-pasta of params
        get_endpoint_data = functools.partial(
            self.get_endpoint_data,
            session, service_type=service_type, interface=interface,
            region_name=region_name, service_name=service_name,
            allow=allow, min_version=min_version, max_version=max_version,
            skip_discovery=skip_discovery,
            allow_version_hack=allow_version_hack, **kwargs)
        data = get_endpoint_data(discover_versions=discover_versions)
        if (not data or not data.api_version) and not discover_versions:
            # It's possible that no version was requested and the endpoint
            # in the catalog has no version in the URL. A version has been
            # requested, so now it's ok to run discovery.

            data = get_endpoint_data(discover_versions=True)
        if not data:
            return None
        return data.api_version
Beispiel #4
0
    def get_endpoint(self, session, service_type=None, interface=None,
                     region_name=None, service_name=None, version=None,
                     allow=None, allow_version_hack=True,
                     skip_discovery=False,
                     min_version=None, max_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.

        version, min_version and max_version can all be given either as a
        string or a tuple.

        Valid interface types: `public` or `publicURL`,
                               `internal` or `internalURL`,
                               `admin` or 'adminURL`

        :param session: A session object that can be used for communication.
        :type session: keystoneauth1.session.Session
        :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 interface: Type of endpoint. Can be a single value or a list
                          of values. If it's a list of values, they will be
                          looked for in order of preference. Can also be
                          `keystoneauth1.plugin.AUTH_INTERFACE` to indicate
                          that the auth_url should be used instead of the
                          value in the catalog. (optional, 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 version: The minimum version number required for this
                        endpoint. (optional)
        :param dict allow: Extra filters to pass when discovering API
                           versions. (optional)
        :param bool allow_version_hack: Allow keystoneauth to hack up catalog
                                        URLS to support older schemes.
                                        (optional, default True)
        :param bool skip_discovery: Whether to skip version discovery even
                                    if a version has been given. This is useful
                                    if endpoint_override or similar has been
                                    given and grabbing additional information
                                    about the endpoint is not useful.
        :param min_version: The minimum version that is acceptable. Mutually
                            exclusive with version. If min_version is given
                            with no max_version it is as if max version is
                            'latest'. (optional)
        :param max_version: The maximum version that is acceptable. Mutually
                            exclusive with version. If min_version is given
                            with no max_version it is as if max version is
                            'latest'. (optional)

        :raises keystoneauth1.exceptions.http.HttpError: An error from an
                                                         invalid HTTP response.

        :return: A valid endpoint URL or None if not available.
        :rtype: string or None
        """
        # Explode `version` into min_version and max_version - everything below
        # here uses the latter rather than the former.
        min_version, max_version = discover._normalize_version_args(
            version, min_version, max_version, service_type=service_type)
        # Set discover_versions to False since we're only going to return
        # a URL. Fetching the microversion data would be needlessly
        # expensive in the common case. However, discover_versions=False
        # will still run discovery if the version requested is not the
        # version in the catalog.
        endpoint_data = self.get_endpoint_data(
            session, service_type=service_type, interface=interface,
            region_name=region_name, service_name=service_name,
            allow=allow, min_version=min_version, max_version=max_version,
            discover_versions=False, skip_discovery=skip_discovery,
            allow_version_hack=allow_version_hack, **kwargs)
        return endpoint_data.url if endpoint_data else None
Beispiel #5
0
    def get_endpoint_data(self, session, service_type=None, interface=None,
                          region_name=None, service_name=None, allow=None,
                          allow_version_hack=True, discover_versions=True,
                          skip_discovery=False, min_version=None,
                          max_version=None, endpoint_override=None, **kwargs):
        """Return a valid endpoint data for a service.

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

        version, min_version and max_version can all be given either as a
        string or a tuple.

        Valid interface types: `public` or `publicURL`,
                               `internal` or `internalURL`,
                               `admin` or 'adminURL`

        :param session: A session object that can be used for communication.
        :type session: keystoneauth1.session.Session
        :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 interface: Type of endpoint. Can be a single value or a list
                          of values. If it's a list of values, they will be
                          looked for in order of preference. Can also be
                          `keystoneauth1.plugin.AUTH_INTERFACE` to indicate
                          that the auth_url should be used instead of the
                          value in the catalog. (optional, 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 dict allow: Extra filters to pass when discovering API
                           versions. (optional)
        :param bool allow_version_hack: Allow keystoneauth to hack up catalog
                                        URLS to support older schemes.
                                        (optional, default True)
        :param bool discover_versions: Whether to get version metadata from
                                       the version discovery document even
                                       if it's not neccessary to fulfill the
                                       major version request. (optional,
                                       defaults to True)
        :param bool skip_discovery: Whether to skip version discovery even
                                    if a version has been given. This is useful
                                    if endpoint_override or similar has been
                                    given and grabbing additional information
                                    about the endpoint is not useful.
        :param min_version: The minimum version that is acceptable. Mutually
                            exclusive with version. If min_version is given
                            with no max_version it is as if max version is
                            'latest'. (optional)
        :param max_version: The maximum version that is acceptable. Mutually
                            exclusive with version. If min_version is given
                            with no max_version it is as if max version is
                            'latest'. (optional)
        :param str endpoint_override: URL to use instead of looking in the
                                      catalog. Catalog lookup will be skipped,
                                      but version discovery will be run.
                                      Sets allow_version_hack to False
                                      (optional)
        :param kwargs: Ignored.

        :raises keystoneauth1.exceptions.http.HttpError: An error from an
                                                         invalid HTTP response.

        :return: Valid EndpointData or None if not available.
        :rtype: `keystoneauth1.discover.EndpointData` or None
        """
        allow = allow or {}

        min_version, max_version = discover._normalize_version_args(
            None, min_version, max_version, service_type=service_type)

        # NOTE(jamielennox): if you specifically ask for requests to be sent to
        # the auth url then we can ignore many of the checks. Typically if you
        # are asking for the auth endpoint it means that there is no catalog to
        # query however we still need to support asking for a specific version
        # of the auth_url for generic plugins.
        if interface is plugin.AUTH_INTERFACE:
            endpoint_data = discover.EndpointData(
                service_url=self.auth_url,
                service_type=service_type or 'identity')
            project_id = None
        elif endpoint_override:
            # TODO(mordred) Make a code path that will look for a
            #               matching entry in the catalog if the catalog
            #               exists and fill in the interface, region_name, etc.
            #               For now, just use any information the use has
            #               provided.
            endpoint_data = discover.EndpointData(
                service_url=endpoint_override,
                catalog_url=endpoint_override,
                interface=interface,
                region_name=region_name,
                service_name=service_name)
            # Setting an endpoint_override then calling get_endpoint_data means
            # you absolutely want the discovery info for the URL in question.
            # There are no code flows where this will happen for any other
            # reasons.
            allow_version_hack = False
            project_id = self.get_project_id(session)
        else:
            if not service_type:
                LOG.warning('Plugin cannot return an endpoint without '
                            'knowing the service type that is required. Add '
                            'service_type to endpoint filtering data.')
                return None

            # It's possible for things higher in the stack, because of
            # defaults, to explicitly pass None.
            if not interface:
                interface = 'public'

            service_catalog = self.get_access(session).service_catalog
            project_id = self.get_project_id(session)
            # NOTE(mordred): service_catalog.url_data_for raises if it can't
            # find a match, so this will always be a valid object.
            endpoint_data = service_catalog.endpoint_data_for(
                service_type=service_type,
                interface=interface,
                region_name=region_name,
                service_name=service_name)
            if not endpoint_data:
                return None

        if skip_discovery:
            return endpoint_data

        try:
            return endpoint_data.get_versioned_data(
                session,
                project_id=project_id,
                min_version=min_version,
                max_version=max_version,
                cache=self._discovery_cache,
                discover_versions=discover_versions,
                allow_version_hack=allow_version_hack, allow=allow)
        except (exceptions.DiscoveryFailure,
                exceptions.HttpError,
                exceptions.ConnectionError):
            # If a version was requested, we didn't find it, return
            # None.
            if max_version or min_version:
                return None
            # If one wasn't, then the endpoint_data we already have
            # should be fine
            return endpoint_data
 def assert_min_max(in_ver, in_min, in_max, in_type, out_min, out_max):
     self.assertEqual(
         (out_min, out_max),
         discover._normalize_version_args(
             in_ver, in_min, in_max, service_type=in_type))