Skip to content

Commit

Permalink
Throw an error when controllers.yaml is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielArndt committed Nov 13, 2023
1 parent 4a287ee commit 747dc77
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
10 changes: 5 additions & 5 deletions juju/client/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import logging

import macaroonbakery.httpbakery as httpbakery

from juju.client import client
from juju.client.connection import Connection
from juju.client.gocookies import GoCookieJar, go_to_py_cookie
from juju.client.jujudata import FileJujuData, API_ENDPOINTS_KEY
from juju.client.jujudata import API_ENDPOINTS_KEY, FileJujuData
from juju.client.proxy.factory import proxy_from_config
from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError
from juju.client import client
from juju.errors import (JujuConnectionError, JujuError,
PylibjujuProgrammingError)
from juju.version import SUPPORTED_MAJOR_VERSION, TARGET_JUJU_VERSION

log = logging.getLogger('connector')
Expand Down Expand Up @@ -113,8 +115,6 @@ async def connect_controller(self, controller_name=None, specified_facades=None)
"""
if not controller_name:
controller_name = self.jujudata.current_controller()
if not controller_name:
raise JujuConnectionError('No current controller. Is Juju bootstrapped?')

controller = self.jujudata.controllers()[controller_name]
endpoints = controller[API_ENDPOINTS_KEY]
Expand Down
8 changes: 5 additions & 3 deletions juju/client/jujudata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import os
import pathlib

from juju.client import client as jujuclient
import yaml

from juju import tag
from juju.client import client as jujuclient
from juju.client.gocookies import GoCookieJar
from juju.errors import JujuError, PylibjujuProgrammingError
from juju.errors import (JujuControllerNotFoundError, JujuError,
PylibjujuProgrammingError)
from juju.utils import juju_config_dir

API_ENDPOINTS_KEY = 'api-endpoints'
Expand Down Expand Up @@ -80,7 +82,7 @@ def current_controller(self):
try:
return self._load_yaml('controllers.yaml', 'current-controller')
except FileNotFoundError:
return None
raise JujuControllerNotFoundError('No controllers.yaml file found. Did you forget to bootstrap Juju?')

def current_model(self, controller_name=None, model_only=False):
'''Return the current model, qualified by its controller name.
Expand Down
4 changes: 4 additions & 0 deletions juju/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class JujuModelConfigError(JujuConfigError):
pass


class JujuControllerNotFoundError(JujuError):
pass


class AbstractMethodError(Exception):
pass

Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_jujudata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import unittest

import mock
import pytest

from juju.client.jujudata import FileJujuData
from juju.errors import JujuControllerNotFoundError


class TestJujuData(unittest.IsolatedAsyncioTestCase):
@mock.patch("io.open")
async def test_verify_controller_uninitialized(self, mock_io_open):
mock_io_open.side_effect = FileNotFoundError()
jujudata = FileJujuData()
assert jujudata.current_controller() is None
with pytest.raises(JujuControllerNotFoundError):
jujudata.current_controller()

0 comments on commit 747dc77

Please sign in to comment.