Exemplo n.º 1
0
    def __init__(self, version_str=None):
        """Create an API version object.

        :param version_str: String representation of APIVersionRequest.
                            Correct format is 'X.Y', where 'X' and 'Y'
                            are int values. None value should be used
                            to create Null APIVersionRequest, which is
                            equal to 0.0
        """
        self.ver_major = 0
        self.ver_minor = 0

        if version_str is not None:
            match = re.match(r"^([1-9]\d*)\.([1-9]\d*|0|latest)$", version_str)
            if match:
                self.ver_major = int(match.group(1))
                if match.group(2) == "latest":
                    # NOTE(andreykurilin): Infinity allows to easily determine
                    # latest version and doesn't require any additional checks
                    # in comparison methods.
                    self.ver_minor = float("inf")
                else:
                    self.ver_minor = int(match.group(2))
            else:
                msg = _("Invalid format of client version '%s'. "
                        "Expected format 'X.Y', where X is a major part and Y "
                        "is a minor part of version.") % version_str
                raise exceptions.UnsupportedVersion(msg)
Exemplo n.º 2
0
def discover_version(client, requested_version):
    server_start_version, server_end_version = _get_server_version_range(
        client)

    if (not requested_version.is_latest()
            and requested_version != APIVersion('1.1')):
        if server_start_version.is_null() and server_end_version.is_null():
            raise exceptions.UnsupportedVersion(
                _("Server doesn't support microversions"))
        if not requested_version.matches(server_start_version,
                                         server_end_version):
            raise exceptions.UnsupportedVersion(
                _("The specified version isn't supported by server. The valid "
                  "version range is '%(min)s' to '%(max)s'") % {
                      "min": server_start_version.get_string(),
                      "max": server_end_version.get_string()
                  })
        return requested_version

    min_version = APIVersion(MIN_API_VERSION)
    max_version = APIVersion(MAX_API_VERSION)
    if server_start_version.is_null() and server_end_version.is_null():
        return APIVersion("1.1")
    elif min_version > server_end_version:
        raise exceptions.UnsupportedVersion(
            _("Server version is too old. The client valid version range is "
              "'%(client_min)s' to '%(client_max)s'. The server valid version "
              "range is '%(server_min)s' to '%(server_max)s'.") % {
                  'client_min': min_version.get_string(),
                  'client_max': max_version.get_string(),
                  'server_min': server_start_version.get_string(),
                  'server_max': server_end_version.get_string()
              })
    elif max_version < server_start_version:
        raise exceptions.UnsupportedVersion(
            _("Server version is too new. The client valid version range is "
              "'%(client_min)s' to '%(client_max)s'. The server valid version "
              "range is '%(server_min)s' to '%(server_max)s'.") % {
                  'client_min': min_version.get_string(),
                  'client_max': max_version.get_string(),
                  'server_min': server_start_version.get_string(),
                  'server_max': server_end_version.get_string()
              })
    elif max_version <= server_end_version:
        return max_version
    elif server_end_version < max_version:
        return server_end_version
Exemplo n.º 3
0
def _get_client_class_and_version(version):
    if not isinstance(version, api_versions.APIVersion):
        version = api_versions.get_api_version(version)
    else:
        api_versions.check_major_version(version)
    if version.is_latest():
        raise exceptions.UnsupportedVersion(
            _('The version should be explicit, not latest.'))
    return version, importutils.import_class('zunclient.v%s.client.Client' %
                                             version.ver_major)
Exemplo n.º 4
0
def check_major_version(api_version):
    """Checks major part of ``APIVersion`` obj is supported.

    :raises exceptions.UnsupportedVersion: if major part is not supported
    """
    available_versions = get_available_major_versions()
    if (not api_version.is_null() and
            str(api_version.ver_major) not in available_versions):
        if len(available_versions) == 1:
            msg = _("Invalid client version '%(version)s'. "
                    "Major part should be '%(major)s'") % {
                "version": api_version.get_string(),
                "major": available_versions[0]}
        else:
            msg = _("Invalid client version '%(version)s'. "
                    "Major part must be one of: '%(major)s'") % {
                "version": api_version.get_string(),
                "major": ", ".join(available_versions)}
        raise exceptions.UnsupportedVersion(msg)