projects
/
osm
/
N2VC.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Get everything working on juju-2.0beta11
[osm/N2VC.git]
/
juju
/
client
/
connection.py
diff --git
a/juju/client/connection.py
b/juju/client/connection.py
index
1a9e8e9
..
3dcfabf
100644
(file)
--- a/
juju/client/connection.py
+++ b/
juju/client/connection.py
@@
-4,8
+4,10
@@
import json
import logging
import os
import random
import logging
import os
import random
+import shlex
import ssl
import string
import ssl
import string
+import subprocess
import websockets
import yaml
import websockets
import yaml
@@
-28,7
+30,13
@@
class Connection:
client = await Connection.connect_current()
"""
client = await Connection.connect_current()
"""
- def __init__(self):
+ def __init__(self, endpoint, uuid, username, password, cacert=None):
+ self.endpoint = endpoint
+ self.uuid = uuid
+ self.username = username
+ self.password = password
+ self.cacert = cacert
+
self.__request_id__ = 0
self.addr = None
self.ws = None
self.__request_id__ = 0
self.addr = None
self.ws = None
@@
-57,27
+65,41
@@
class Connection:
async def rpc(self, msg, encoder=None):
self.__request_id__ += 1
async def rpc(self, msg, encoder=None):
self.__request_id__ += 1
- msg['
RequestI
d'] = self.__request_id__
- if'
P
arams' not in msg:
- msg['
P
arams'] = {}
- if "
V
ersion" not in msg:
- msg['
V
ersion'] = self.facades[msg['Type']]
+ msg['
request-i
d'] = self.__request_id__
+ if'
p
arams' not in msg:
+ msg['
p
arams'] = {}
+ if "
v
ersion" not in msg:
+ msg['
v
ersion'] = self.facades[msg['Type']]
outgoing = json.dumps(msg, indent=2, cls=encoder)
await self.ws.send(outgoing)
result = await self.recv()
log.debug("send %s got %s", msg, result)
outgoing = json.dumps(msg, indent=2, cls=encoder)
await self.ws.send(outgoing)
result = await self.recv()
log.debug("send %s got %s", msg, result)
- if result and '
E
rror' in result:
+ if result and '
e
rror' in result:
raise RuntimeError(result)
return result
raise RuntimeError(result)
return result
+ async def clone(self):
+ """Return a new Connection, connected to the same websocket endpoint
+ as this one.
+
+ """
+ return await Connection.connect(
+ self.endpoint,
+ self.uuid,
+ self.username,
+ self.password,
+ self.cacert,
+ )
+
@classmethod
async def connect(cls, endpoint, uuid, username, password, cacert=None):
url = "wss://{}/model/{}/api".format(endpoint, uuid)
@classmethod
async def connect(cls, endpoint, uuid, username, password, cacert=None):
url = "wss://{}/model/{}/api".format(endpoint, uuid)
- client = cls()
+ client = cls(
endpoint, uuid, username, password, cacert
)
await client.open(url, cacert)
server_info = await client.login(username, password)
client.build_facades(server_info['facades'])
log.info("Driver connected to juju %s", endpoint)
await client.open(url, cacert)
server_info = await client.login(username, password)
client.build_facades(server_info['facades'])
log.info("Driver connected to juju %s", endpoint)
+
return client
@classmethod
return client
@classmethod
@@
-125,22
+147,22
@@
class Connection:
def build_facades(self, info):
self.facades.clear()
for facade in info:
def build_facades(self, info):
self.facades.clear()
for facade in info:
- self.facades[facade['
Name']] = facade['V
ersions'][-1]
+ self.facades[facade['
name']] = facade['v
ersions'][-1]
async def login(self, username, password):
if not username.startswith('user-'):
username = 'user-{}'.format(username)
result = await self.rpc({
async def login(self, username, password):
if not username.startswith('user-'):
username = 'user-{}'.format(username)
result = await self.rpc({
- "
T
ype": "Admin",
- "
R
equest": "Login",
- "
V
ersion": 3,
- "
P
arams": {
+ "
t
ype": "Admin",
+ "
r
equest": "Login",
+ "
v
ersion": 3,
+ "
p
arams": {
"auth-tag": username,
"credentials": password,
"auth-tag": username,
"credentials": password,
- "
N
once": "".join(random.sample(string.printable, 12)),
+ "
n
once": "".join(random.sample(string.printable, 12)),
}})
}})
- return result['
R
esponse']
+ return result['
r
esponse']
class JujuData:
class JujuData:
@@
-149,13
+171,10
@@
class JujuData:
self.path = os.path.abspath(os.path.expanduser(self.path))
def current_controller(self):
self.path = os.path.abspath(os.path.expanduser(self.path))
def current_controller(self):
- try:
- filepath = os.path.join(self.path, 'current-controller')
- with io.open(filepath, 'rt') as f:
- return f.read().strip()
- except OSError as e:
- log.exception(e)
- return None
+ cmd = shlex.split('juju show-controller --format yaml')
+ output = subprocess.check_output(cmd)
+ output = yaml.safe_load(output)
+ return list(output.keys())[0]
def controllers(self):
return self._load_yaml('controllers.yaml', 'controllers')
def controllers(self):
return self._load_yaml('controllers.yaml', 'controllers')