Skip to content

Commit 15616c0

Browse files
authored
Merge pull request #966 from cderici/controller-name-fix-forward-port
#966 #### Description This is a forward port for the fix #964 for the issue #771 that was on 2.9, bringing it into 3.x. #### QA Steps Manual QA should follow the steps described in #771. Find the details of a controller you bootstrapped (any controller would do): ```sh $ juju show-controller --show-password ``` Grab the details there and plug them into either a script or in the repl (repl is awkward to use with the certificate): ```python c = Controller() await c.connect(endpoint="<ip>:17070", username="admin", password="admin_pass", cacert="ca_cert") # explicit connection with credential values # check the name print(c.controller_name) ``` All CI tests need to pass. #### Notes & Discussion JUJU-4781
2 parents be013d7 + 7162c6d commit 15616c0

3 files changed

Lines changed: 33 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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
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'
17+
1618

1719
class NoModelException(Exception):
1820
pass
@@ -126,6 +128,23 @@ def load_credential(self, cloud, name=None):
126128
except (KeyError, FileNotFoundError):
127129
return None, None
128130

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

juju/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ class JujuBackupError(JujuError):
9090
pass
9191

9292

93+
class PylibjujuProgrammingError(Exception):
94+
pass
95+
96+
9397
class JujuNotValid(JujuError):
9498
def __init__(self, entity_type, entity_name):
9599
self.entity_type = entity_type

0 commit comments

Comments
 (0)