@@ -85,6 +85,18 @@ async def connect(self, **kwargs):
8585 # connected to.
8686 if self ._connection :
8787 await self ._connection .close ()
88+
89+ account = kwargs .pop ('account' , {})
90+ # Prioritize the username and password that user provided
91+ # If not enough, try to patch it with info from accounts.yaml
92+ if 'username' not in kwargs and account .get ('user' ):
93+ kwargs .update (username = account .get ('user' ))
94+ if 'password' not in kwargs and account .get ('password' ):
95+ kwargs .update (password = account .get ('password' ))
96+
97+ if not ({'username' , 'password' }.issubset (kwargs )):
98+ required = {'username' , 'password' }.difference (kwargs )
99+ raise ValueError (f'Some authentication parameters are required : { "," .join (required )} ' )
88100 self ._connection = await Connection .connect (** kwargs )
89101
90102 # Check if we support the target controller
@@ -117,7 +129,7 @@ async def disconnect(self, entity):
117129 await self ._log_connection .close ()
118130 self ._log_connection = None
119131
120- async def connect_controller (self , controller_name = None , specified_facades = None ):
132+ async def connect_controller (self , controller_name = None , specified_facades = None , ** kwargs ):
121133 """Connect to a controller by name. If the name is empty, it
122134 connect to the current controller.
123135 """
@@ -130,16 +142,16 @@ async def connect_controller(self, controller_name=None, specified_facades=None)
130142
131143 proxy = proxy_from_config (controller .get ("proxy-config" , None ))
132144
133- await self . connect (
134- endpoint = endpoints ,
135- uuid = None ,
136- username = accounts .get ("user" ),
137- password = accounts . get ( "password" ),
138- cacert = controller . get ( "ca-cert" ) ,
139- bakery_client = self . bakery_client_for_controller ( controller_name ) ,
140- specified_facades = specified_facades ,
141- proxy = proxy ,
142- )
145+ kwargs . update ( endpoint = endpoints ,
146+ uuid = None ,
147+ account = accounts ,
148+ cacert = controller .get ('ca-cert' ),
149+ bakery_client = self . bakery_client_for_controller ( controller_name ),
150+ specified_facades = specified_facades ,
151+ proxy = proxy ,
152+ )
153+ await self . connect ( ** kwargs )
154+ self . controller_name = controller_name
143155 self .controller_uuid = controller ["uuid" ]
144156
145157 async def connect_model (self , _model_name = None , ** kwargs ):
@@ -184,15 +196,12 @@ async def connect_model(self, _model_name=None, **kwargs):
184196
185197 # TODO remove the need for base.CleanModel to subclass
186198 # JujuData.
187- kwargs .update (
188- endpoint = endpoints ,
189- uuid = model_uuid ,
190- username = account .get ("user" ),
191- password = account .get ("password" ),
192- cacert = controller .get ("ca-cert" ),
193- bakery_client = self .bakery_client_for_controller (controller_name ),
194- proxy = proxy ,
195- )
199+ kwargs .update (endpoint = endpoints ,
200+ uuid = model_uuid ,
201+ account = account ,
202+ cacert = controller .get ('ca-cert' ),
203+ bakery_client = self .bakery_client_for_controller (controller_name ),
204+ proxy = proxy )
196205 await self .connect (** kwargs )
197206 # TODO this might be a good spot to trigger refreshing the
198207 # local cache (the connection to the model might help)
0 commit comments