Ejemplo n.º 1
0
        def decorator(f):
            obj_min_ver = api_version.APIVersionRequest(min_ver)
            if max_ver:
                obj_max_ver = api_version.APIVersionRequest(max_ver)
            else:
                obj_max_ver = api_version.APIVersionRequest.max_version()

            # Add to list of versioned methods registered
            func_name = f.__name__
            new_func = VersionedMethod(func_name, obj_min_ver, obj_max_ver, f)

            versioned_attr = cls.VER_METHODS_ATTR_PREFIX + cls.__name__
            func_dict = getattr(cls, versioned_attr, {})
            if not func_dict:
                setattr(cls, versioned_attr, func_dict)

            func_list = func_dict.get(func_name, [])
            if not func_list:
                func_dict[func_name] = func_list
                func_list.append(new_func)

            # Ensure the list is sorted by minimum version (reversed)
            # so later when we work through the list in order we find
            # the method which has the latest version which supports
            # the version requested.
            is_intersect = cls.check_for_versions_intersection(func_list)

            if is_intersect:
                raise exc.ApiVersionsIntersect(
                    name=new_func.name,
                    min_ver=new_func.start_version,
                    max_ver=new_func.end_version,
                )

            func_list.sort(key=lambda vf: vf.start_version, reverse=True)

            return f
Ejemplo n.º 2
0
    def _get_api_version_request(req_version):
        """Set API version for request based on the version header string."""
        if req_version is None:
            LOG.debug("No API version in request header. Use default version.")
            cur_ver = api_version.APIVersionRequest.default_version()
        elif req_version == 'latest':
            # 'latest' is a special keyword which is equivalent to
            # requesting the maximum version of the API supported
            cur_ver = api_version.APIVersionRequest.max_version()
        else:
            cur_ver = api_version.APIVersionRequest(req_version)

        # Check that the version requested is within the global
        # minimum/maximum of supported API versions
        if not cur_ver.matches(cur_ver.min_version(), cur_ver.max_version()):
            raise exception.InvalidGlobalAPIVersion(
                req_ver=cur_ver.get_string(),
                min_ver=cur_ver.min_version().get_string(),
                max_ver=cur_ver.max_version().get_string())
        return cur_ver