Fixed detecting controller for non-superuser
authorCory Johns <johnsca@gmail.com>
Thu, 22 Dec 2016 16:42:29 +0000 (11:42 -0500)
committerCory Johns <johnsca@gmail.com>
Thu, 22 Dec 2016 16:45:20 +0000 (11:45 -0500)
The `show-controller` command can only be used by superusers, so usually
fails on shared controllers.

https://bugs.launchpad.net/juju/+bug/1652106

juju/client/connection.py
juju/errors.py

index 452754e..5ed073f 100644 (file)
@@ -12,7 +12,7 @@ import websockets
 
 import yaml
 
-from juju.errors import JujuAPIError
+from juju.errors import JujuAPIError, JujuConnectionError
 
 log = logging.getLogger("websocket")
 
@@ -180,6 +180,8 @@ class Connection:
         """
         jujudata = JujuData()
         controller_name = jujudata.current_controller()
+        if not controller_name:
+            raise JujuConnectionError('No current controller')
 
         return await cls.connect_controller(controller_name)
 
@@ -271,10 +273,10 @@ class JujuData:
         self.path = os.path.abspath(os.path.expanduser(self.path))
 
     def current_controller(self):
-        cmd = shlex.split('juju show-controller --format yaml')
+        cmd = shlex.split('juju list-controllers --format yaml')
         output = subprocess.check_output(cmd)
         output = yaml.safe_load(output)
-        return list(output.keys())[0]
+        return output.get('current-controller', '')
 
     def controllers(self):
         return self._load_yaml('controllers.yaml', 'controllers')
index 9295267..6bb5530 100644 (file)
@@ -5,3 +5,7 @@ class JujuAPIError(Exception):
         self.response = result['response']
         self.request_id = result['request-id']
         super().__init__(self.message)
+
+
+class JujuConnectionError(ConnectionError):
+    pass