1212from juju .client .jujudata import API_ENDPOINTS_KEY , FileJujuData
1313from juju .client .proxy .factory import proxy_from_config
1414from juju .errors import JujuConnectionError , JujuError
15- from juju .client import client
1615from juju .version import SUPPORTED_MAJOR_VERSION , TARGET_JUJU_VERSION
1716
18- log = logging .getLogger (' connector' )
17+ log = logging .getLogger (" connector" )
1918
2019
2120class NoConnectionException (Exception ):
22- '''Raised by Connector when the connection method is called
23- and there is no current connection.'''
21+ """Raised by Connector when the connection method is called
22+ and there is no current connection."""
23+
2424 pass
2525
2626
2727class Connector :
28- ''' This class abstracts out a reconnectable client that can connect
28+ """ This class abstracts out a reconnectable client that can connect
2929 to controllers and models found in the Juju data files.
30- '''
30+ """
31+
3132 def __init__ (
3233 self ,
3334 max_frame_size = None ,
3435 bakery_client = None ,
3536 jujudata = None ,
3637 ):
37- ''' Initialize a connector that will use the given parameters
38- by default when making a new connection'''
38+ """ Initialize a connector that will use the given parameters
39+ by default when making a new connection"""
3940 self .max_frame_size = max_frame_size
4041 self .bakery_client = bakery_client
4142 self ._connection = None
@@ -45,32 +46,32 @@ def __init__(
4546 self .jujudata = jujudata or FileJujuData ()
4647
4748 def is_connected (self ):
48- ''' Report whether there is a currently connected controller or not'''
49+ """ Report whether there is a currently connected controller or not"""
4950 return self ._connection is not None
5051
5152 def connection (self ):
52- ''' Return the current connection; raises an exception if there
53- is no current connection.'''
53+ """ Return the current connection; raises an exception if there
54+ is no current connection."""
5455 if not self .is_connected ():
55- raise NoConnectionException (' not connected' )
56+ raise NoConnectionException (" not connected" )
5657 return self ._connection
5758
5859 async def connect (self , ** kwargs ):
5960 """Connect to an arbitrary Juju model.
6061
6162 kwargs are passed through to Connection.connect()
6263 """
63- kwargs .setdefault (' max_frame_size' , self .max_frame_size )
64- kwargs .setdefault (' bakery_client' , self .bakery_client )
65- if ' macaroons' in kwargs :
66- if not kwargs [' bakery_client' ]:
67- kwargs [' bakery_client' ] = httpbakery .Client ()
68- if not kwargs [' bakery_client' ].cookies :
69- kwargs [' bakery_client' ].cookies = GoCookieJar ()
70- jar = kwargs [' bakery_client' ].cookies
71- for macaroon in kwargs .pop (' macaroons' ):
64+ kwargs .setdefault (" max_frame_size" , self .max_frame_size )
65+ kwargs .setdefault (" bakery_client" , self .bakery_client )
66+ if " macaroons" in kwargs :
67+ if not kwargs [" bakery_client" ]:
68+ kwargs [" bakery_client" ] = httpbakery .Client ()
69+ if not kwargs [" bakery_client" ].cookies :
70+ kwargs [" bakery_client" ].cookies = GoCookieJar ()
71+ jar = kwargs [" bakery_client" ].cookies
72+ for macaroon in kwargs .pop (" macaroons" ):
7273 jar .set_cookie (go_to_py_cookie (macaroon ))
73- if ' debug_log_conn' in kwargs :
74+ if " debug_log_conn" in kwargs :
7475 assert self ._connection
7576 self ._log_connection = await Connection .connect (** kwargs )
7677 else :
@@ -85,21 +86,28 @@ async def connect(self, **kwargs):
8586 self ._connection = await Connection .connect (** kwargs )
8687
8788 # Check if we support the target controller
88- juju_server_version = self ._connection .info [' server-version' ]
89+ juju_server_version = self ._connection .info [" server-version" ]
8990 if not juju_server_version .startswith (TARGET_JUJU_VERSION ):
90- log .debug ("This version was tested using {} juju version {} may have compatibility issues" .format (TARGET_JUJU_VERSION , juju_server_version ))
91- if not self ._connection .info ['server-version' ].startswith (SUPPORTED_MAJOR_VERSION ):
92- raise JujuConnectionError ("juju server-version %s not supported" % juju_server_version )
91+ log .debug (
92+ "This version was tested using {} juju version {} may have compatibility issues" .format (
93+ TARGET_JUJU_VERSION , juju_server_version
94+ )
95+ )
96+ if not self ._connection .info ["server-version" ].startswith (
97+ SUPPORTED_MAJOR_VERSION
98+ ):
99+ raise JujuConnectionError (
100+ "juju server-version %s not supported" % juju_server_version
101+ )
93102
94103 async def disconnect (self , entity ):
95- """Shut down the watcher task and close websockets.
96- """
104+ """Shut down the watcher task and close websockets."""
97105 if self ._connection :
98- log .debug (f' Connector: closing { entity } connection' )
106+ log .debug (f" Connector: closing { entity } connection" )
99107 await self ._connection .close ()
100108 self ._connection = None
101109 if self ._log_connection :
102- log .debug (' Also closing debug-log connection' )
110+ log .debug (" Also closing debug-log connection" )
103111 await self ._log_connection .close ()
104112 self ._log_connection = None
105113
@@ -114,14 +122,14 @@ async def connect_controller(self, controller_name=None, specified_facades=None)
114122 endpoints = controller [API_ENDPOINTS_KEY ]
115123 accounts = self .jujudata .accounts ().get (controller_name , {})
116124
117- proxy = proxy_from_config (controller .get (' proxy-config' , None ))
125+ proxy = proxy_from_config (controller .get (" proxy-config" , None ))
118126
119127 await self .connect (
120128 endpoint = endpoints ,
121129 uuid = None ,
122- username = accounts .get (' user' ),
123- password = accounts .get (' password' ),
124- cacert = controller .get (' ca-cert' ),
130+ username = accounts .get (" user" ),
131+ password = accounts .get (" password" ),
132+ cacert = controller .get (" ca-cert" ),
125133 bakery_client = self .bakery_client_for_controller (controller_name ),
126134 specified_facades = specified_facades ,
127135 proxy = proxy ,
@@ -142,57 +150,57 @@ async def connect_model(self, _model_name=None, **kwargs):
142150 except JujuError as e :
143151 raise JujuConnectionError (e .message ) from e
144152 if controller is None :
145- raise JujuConnectionError ('Controller {} not found' .format (
146- controller_name ))
153+ raise JujuConnectionError ("Controller {} not found" .format (controller_name ))
147154 endpoints = controller [API_ENDPOINTS_KEY ]
148155 account = self .jujudata .accounts ().get (controller_name , {})
149- models = self .jujudata .models ().get (controller_name , {}).get ('models' ,
150- {})
156+ models = self .jujudata .models ().get (controller_name , {}).get ("models" , {})
151157 model_uuid = None
152158 if _model_name in models :
153- model_uuid = models [_model_name ][' uuid' ]
159+ model_uuid = models [_model_name ][" uuid" ]
154160 else :
155161 # let's try to find it through the actual controller
156162 await self .connect_controller (controller_name = controller_name )
157163 # get the facade
158164 controller_facade = client .ControllerFacade .from_connection (
159- self .connection ())
165+ self .connection ()
166+ )
160167 # get all the user models from the api
161168 response = await controller_facade .AllModels ()
162169 # search the one that contains admin/model_name
163170 for user_model in response .user_models :
164- if ' admin/' + user_model .model .name == _model_name :
171+ if " admin/" + user_model .model .name == _model_name :
165172 model_uuid = user_model .model .uuid
166173
167174 if model_uuid is None :
168- raise JujuConnectionError (' Model not found: {}' .format (_model_name ))
175+ raise JujuConnectionError (" Model not found: {}" .format (_model_name ))
169176
170- proxy = proxy_from_config (controller .get (' proxy-config' , None ))
177+ proxy = proxy_from_config (controller .get (" proxy-config" , None ))
171178
172179 # TODO remove the need for base.CleanModel to subclass
173180 # JujuData.
174- kwargs .update (endpoint = endpoints ,
175- uuid = model_uuid ,
176- username = account .get ('user' ),
177- password = account .get ('password' ),
178- cacert = controller .get ('ca-cert' ),
179- bakery_client = self .bakery_client_for_controller (controller_name ),
180- proxy = proxy )
181+ kwargs .update (
182+ endpoint = endpoints ,
183+ uuid = model_uuid ,
184+ username = account .get ("user" ),
185+ password = account .get ("password" ),
186+ cacert = controller .get ("ca-cert" ),
187+ bakery_client = self .bakery_client_for_controller (controller_name ),
188+ proxy = proxy ,
189+ )
181190 await self .connect (** kwargs )
182191 # TODO this might be a good spot to trigger refreshing the
183192 # local cache (the connection to the model might help)
184- self .model_name = controller_name + ':' + _model_name
193+ self .model_name = controller_name + ":" + _model_name
185194 return model_uuid
186195
187196 def bakery_client_for_controller (self , controller_name ):
188- ''' Make a copy of the bakery client with a the appropriate controller's
197+ """ Make a copy of the bakery client with a the appropriate controller's
189198 cookiejar in it.
190- '''
199+ """
191200 bakery_client = self .bakery_client
192201 if bakery_client :
193202 bakery_client = copy .copy (bakery_client )
194203 else :
195204 bakery_client = httpbakery .Client ()
196- bakery_client .cookies = self .jujudata .cookies_for_controller (
197- controller_name )
205+ bakery_client .cookies = self .jujudata .cookies_for_controller (controller_name )
198206 return bakery_client
0 commit comments