@@ -1998,11 +1998,13 @@ async def remove_offer(self, endpoint, force=False):
19981998 controller = await self .get_controller ()
19991999 return await controller .remove_offer (self .info .uuid , endpoint , force )
20002000
2001- async def consume (self , endpoint , application_alias = "" , controller_name = None ):
2001+ async def consume (self , endpoint , application_alias = "" , controller_name = None , controller = None ):
20022002 """
20032003 Adds a remote offer to the model. Relations can be created later using
20042004 "juju relate".
20052005 """
2006+ if controller and controller_name :
2007+ raise JujuError ("cannot set both controller_name and controller" )
20062008 try :
20072009 offer = parse_offer_url (endpoint )
20082010 except OfferParseError as e :
@@ -2014,8 +2016,24 @@ async def consume(self, endpoint, application_alias="", controller_name=None):
20142016 offer .user = self .info .username
20152017 endpoint = offer .string ()
20162018
2017- source = await self ._get_source_api (offer , controller_name = controller_name )
2019+ source = None
2020+ if controller_name :
2021+ source = await self ._get_source_api (offer , controller_name = controller_name )
2022+ else :
2023+ if controller :
2024+ source = controller
2025+ else :
2026+ source = Controller ()
2027+ kwargs = self .connection ().connect_params ()
2028+ kwargs ["uuid" ] = None
2029+ await source ._connect_direct (** kwargs )
2030+
20182031 consume_details = await source .get_consume_details (offer .as_local ().string ())
2032+
2033+ # Only disconnect when the controller object has been created within with function
2034+ # We don't want to disconnect the object passed by the user in the controller argument
2035+ if not controller :
2036+ await source .disconnect ()
20192037 if consume_details is None or consume_details .offer is None :
20202038 raise JujuAPIError ("missing consuming offer url for {}" .format (offer .string ()))
20212039
0 commit comments