From 37861371d37b20bb166a6eb95a4583b09b8402eb Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Wed, 17 Jan 2024 12:27:00 -0700 Subject: [PATCH 1/2] Remove dependency to juju cli for controller_name controllers.yaml is read for the controller_name after a connection is established, and this creates a depends on the juju-cli to be installed in the system, which is not required for pylibjuju. --- juju/client/connector.py | 8 ++++++-- juju/client/jujudata.py | 8 ++++++-- juju/errors.py | 4 ++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/juju/client/connector.py b/juju/client/connector.py index a5998c6a3..6f6161523 100644 --- a/juju/client/connector.py +++ b/juju/client/connector.py @@ -9,7 +9,7 @@ from juju.client.gocookies import GoCookieJar, go_to_py_cookie from juju.client.jujudata import FileJujuData, API_ENDPOINTS_KEY from juju.client.proxy.factory import proxy_from_config -from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError +from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError, ControllerNameNotFound from juju.client import client log = logging.getLogger('connector') @@ -97,7 +97,11 @@ async def connect(self, **kwargs): if not self.controller_name: if 'endpoint' not in kwargs: raise PylibjujuProgrammingError("Please report this error to the maintainers.") - self.controller_name = self.jujudata.controller_name_by_endpoint(kwargs['endpoint']) + try: + self.controller_name = self.jujudata.controller_name_by_endpoint(kwargs['endpoint']) + except ControllerNameNotFound: + # It's ok because we might not have the juju cli (controllers.yaml) + pass # Check if we support the target controller if not self._connection.info['server-version'].startswith(SUPPORTED_JUJU_API_PREFIX): diff --git a/juju/client/jujudata.py b/juju/client/jujudata.py index b34c64a8f..4d74a254a 100644 --- a/juju/client/jujudata.py +++ b/juju/client/jujudata.py @@ -10,7 +10,7 @@ import yaml from juju import tag from juju.client.gocookies import GoCookieJar -from juju.errors import JujuError, PylibjujuProgrammingError +from juju.errors import JujuError, PylibjujuProgrammingError, ControllerNameNotFound from juju.utils import juju_config_dir API_ENDPOINTS_KEY = 'api-endpoints' @@ -133,7 +133,11 @@ def controller_name_by_endpoint(self, endpoint): :param str endpoint: The endpoint of the controller we're looking for """ - for controller_name, controller in self.controllers().items(): + try: + contrs = self.controllers() + except FileNotFoundError: + raise ControllerNameNotFound() + for controller_name, controller in contrs.items(): if isinstance(endpoint, str): if endpoint in controller[API_ENDPOINTS_KEY]: return controller_name diff --git a/juju/errors.py b/juju/errors.py index a246aaa9c..e2d6e7e6c 100644 --- a/juju/errors.py +++ b/juju/errors.py @@ -118,3 +118,7 @@ class JujuModelConfigError(JujuConfigError): class AbstractMethodError(Exception): pass + + +class ControllerNameNotFound(Exception): + pass From cb70f3581a78399b9f20cb2c6ee044e67c66e9bd Mon Sep 17 00:00:00 2001 From: Caner Derici Date: Wed, 17 Jan 2024 12:29:10 -0700 Subject: [PATCH 2/2] Add integration test for happy path for controller connection --- tests/integration/test_connection.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/integration/test_connection.py b/tests/integration/test_connection.py index eba584e58..314c7efa9 100644 --- a/tests/integration/test_connection.py +++ b/tests/integration/test_connection.py @@ -24,6 +24,20 @@ logger = logging.getLogger(__name__) +@base.bootstrapped +@pytest.mark.asyncio +async def test_connection_happy_path(event_loop): + async with base.CleanController() as contr: + conn = contr.connection() + new_cont = Controller() + await new_cont.connect(endpoint=conn.endpoint, + username=conn.username, + password=conn.password, + cacert=conn.cacert, + ) + await new_cont.disconnect() + + @base.bootstrapped @pytest.mark.asyncio async def test_monitor(event_loop):