Skip to content

Commit 41351d7

Browse files
committed
Improve jujudata to find controller name by endpoint
Fixes #771
1 parent be013d7 commit 41351d7

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

juju/client/connector.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import macaroonbakery.httpbakery as httpbakery
88
from juju.client.connection import Connection
99
from juju.client.gocookies import GoCookieJar, go_to_py_cookie
10-
from juju.client.jujudata import FileJujuData
10+
from juju.client.jujudata import FileJujuData, API_ENDPOINTS_KEY
1111
from juju.client.proxy.factory import proxy_from_config
12-
from juju.errors import JujuConnectionError, JujuError
12+
from juju.errors import JujuConnectionError, JujuError, PylibjujuProgrammingError
1313
from juju.client import client
1414
from juju.version import SUPPORTED_MAJOR_VERSION, TARGET_JUJU_VERSION
1515

@@ -83,6 +83,11 @@ async def connect(self, **kwargs):
8383
await self._connection.close()
8484
self._connection = await Connection.connect(**kwargs)
8585

86+
if not self.controller_name:
87+
if 'endpoint' not in kwargs:
88+
raise PylibjujuProgrammingError("Please report this error to the maintainers.")
89+
self.controller_name = self.jujudata.controller_name_by_endpoint(kwargs['endpoint'])
90+
8691
# Check if we support the target controller
8792
juju_server_version = self._connection.info['server-version']
8893
if not juju_server_version.startswith(TARGET_JUJU_VERSION):
@@ -112,7 +117,7 @@ async def connect_controller(self, controller_name=None, specified_facades=None)
112117
raise JujuConnectionError('No current controller')
113118

114119
controller = self.jujudata.controllers()[controller_name]
115-
endpoints = controller['api-endpoints']
120+
endpoints = controller[API_ENDPOINTS_KEY]
116121
accounts = self.jujudata.accounts().get(controller_name, {})
117122

118123
proxy = proxy_from_config(controller.get('proxy-config', None))
@@ -146,7 +151,7 @@ async def connect_model(self, _model_name=None, **kwargs):
146151
if controller is None:
147152
raise JujuConnectionError('Controller {} not found'.format(
148153
controller_name))
149-
endpoints = controller['api-endpoints']
154+
endpoints = controller[API_ENDPOINTS_KEY]
150155
account = self.jujudata.accounts().get(controller_name, {})
151156
models = self.jujudata.models().get(controller_name, {}).get('models',
152157
{})

juju/client/jujudata.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
import yaml
1111
from juju import tag
1212
from juju.client.gocookies import GoCookieJar
13-
from juju.errors import JujuError
13+
from juju.errors import JujuError, PylibjujuProgrammingError
1414
from juju.utils import juju_config_dir
1515

16+
API_ENDPOINTS_KEY = 'api-endpoints'
1617

1718
class NoModelException(Exception):
1819
pass
@@ -126,6 +127,23 @@ def load_credential(self, cloud, name=None):
126127
except (KeyError, FileNotFoundError):
127128
return None, None
128129

130+
def controller_name_by_endpoint(self, endpoint):
131+
"""Finds the controller that has the given endpoints, returns the name.
132+
133+
:param str endpoint: The endpoint of the controller we're looking for
134+
"""
135+
for controller_name, controller in self.controllers().items():
136+
if isinstance(endpoint, str):
137+
if endpoint in controller[API_ENDPOINTS_KEY]:
138+
return controller_name
139+
elif isinstance(endpoint, list):
140+
for e in endpoint:
141+
if e in controller[API_ENDPOINTS_KEY]:
142+
return controller_name
143+
else:
144+
raise PylibjujuProgrammingError()
145+
raise JujuError(f'Unable to find controller with endpoint {endpoint}')
146+
129147
def controllers(self):
130148
return self._load_yaml('controllers.yaml', 'controllers')
131149

juju/errors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class JujuUnitError(JujuError):
8989
class JujuBackupError(JujuError):
9090
pass
9191

92+
class PylibjujuProgrammingError(Exception):
93+
pass
9294

9395
class JujuNotValid(JujuError):
9496
def __init__(self, entity_type, entity_name):

0 commit comments

Comments
 (0)