From b9f60891cb25d6850c6c8b1cce09b42464d9a736 Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Tue, 23 Jan 2024 13:53:55 -0700 Subject: [PATCH] Handle non-standard version strings coming from api --- juju/client/connector.py | 16 +++++++++++++--- juju/errors.py | 4 ++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/juju/client/connector.py b/juju/client/connector.py index a4cd9dad..1654a834 100644 --- a/juju/client/connector.py +++ b/juju/client/connector.py @@ -4,7 +4,7 @@ import copy import logging -from packaging import version +from packaging import version, InvalidVersion import macaroonbakery.httpbakery as httpbakery @@ -13,7 +13,7 @@ from juju.client.gocookies import GoCookieJar, go_to_py_cookie from juju.client.jujudata import API_ENDPOINTS_KEY, FileJujuData from juju.client.proxy.factory import proxy_from_config -from juju.errors import JujuConnectionError, JujuError +from juju.errors import JujuConnectionError, JujuError, JujuUnknownVersion from juju.version import CLIENT_VERSION log = logging.getLogger("connector") @@ -88,7 +88,17 @@ async def connect(self, **kwargs): self._connection = await Connection.connect(**kwargs) # Check if we support the target controller - juju_server_version = version.parse(self._connection.info["server-version"]) + server_version = self._connection.info["server-version"] + try: + juju_server_version = version.parse(server_version) + except InvalidVersion as err: + # We're only interested in the major version, so + # we attempt to clean up versions such as 3.4-rc1.2 as just 3.4 + if '-' not in server_version: + raise JujuUnknownVersion(err) + juju_server_version = version.parse(server_version.split('-')[0]) + + # CLIENT_VERSION statically comes from the VERSION file in the repo client_version = version.parse(CLIENT_VERSION) if juju_server_version.major != client_version.major: diff --git a/juju/errors.py b/juju/errors.py index b996ea7f..d0284158 100644 --- a/juju/errors.py +++ b/juju/errors.py @@ -137,3 +137,7 @@ class AbstractMethodError(Exception): class PylibjujuError(JujuError): pass + + +class JujuUnknownVersion(PylibjujuError): + pass