Example #1
0
        def version_select(*args, **kwargs):
            """Select and call the matching version of the specified method.

            Look for the method which matches the name supplied and version
            constraints and calls it with the supplied arguments.

            :returns: Returns the result of the method called
            :raises: VersionNotFoundForAPIMethod if there is no method which
                 matches the name and version constraints
            """

            # The first arg to all versioned methods is always the request
            # object. The version for the request is attached to the
            # request object
            if len(args) == 0:
                version_request = kwargs['req'].api_version_request
            else:
                version_request = args[0].api_version_request

            func_list = self.versioned_methods[key]
            for func in func_list:
                if version_request.matches_versioned_method(func):
                    # Update the version_select wrapper function so
                    # other decorator attributes like wsgi.response
                    # are still respected.
                    functools.update_wrapper(version_select, func.func)
                    return func.func(self, *args, **kwargs)

            # No version match
            raise exception.VersionNotFoundForAPIMethod(
                version=version_request)
Example #2
0
    def set_api_version_request(self, url):
        """Set API version request based on the request header information.

        Microversions starts with /v3, so if a client sends a request for
        version 1.0 or 2.0 with the /v3 endpoint, throw an exception.
        Sending a header with any microversion to a /v1 or /v2 endpoint will
        be ignored.
        Note that a microversion must be set for the legacy endpoints. This
        will appear as 1.0 and 2.0 for /v1 and /v2.
        """
        if API_VERSION_REQUEST_HEADER in self.headers and 'v3' in url:
            hdr_string = self.headers[API_VERSION_REQUEST_HEADER]
            # 'latest' is a special keyword which is equivalent to requesting
            # the maximum version of the API supported
            hdr_string_list = hdr_string.split(",")
            volume_version = None
            for hdr in hdr_string_list:
                if VOLUME_SERVICE in hdr:
                    service, volume_version = hdr.split()
                    break
            if not volume_version:
                raise exception.VersionNotFoundForAPIMethod(
                    version=volume_version)
            if volume_version == 'latest':
                self.api_version_request = api_version.max_api_version()
            else:
                self.api_version_request = api_version.APIVersionRequest(
                    volume_version)

                # Check that the version requested is within the global
                # minimum/maximum of supported API versions
                if not self.api_version_request.matches(
                        api_version.min_api_version(),
                        api_version.max_api_version()):
                    raise exception.InvalidGlobalAPIVersion(
                        req_ver=self.api_version_request.get_string(),
                        min_ver=api_version.min_api_version().get_string(),
                        max_ver=api_version.max_api_version().get_string())

        else:
            if 'v1' in url:
                self.api_version_request = api_version.legacy_api_version1()
            elif 'v2' in url:
                self.api_version_request = api_version.legacy_api_version2()
            else:
                self.api_version_request = api_version.APIVersionRequest(
                    api_version._MIN_API_VERSION)
Example #3
0
 def extension2(req, resp_obj):
     raise exception.VersionNotFoundForAPIMethod(version='fake_version')
 def _ensure_min_version(self, req, allowed_version):
     version = req.api_version_request
     if not version.matches(allowed_version, None):
         raise exception.VersionNotFoundForAPIMethod(version=version)