Remove EntityType from juju watcher and workaround juju bug for retrieving the status
[osm/N2VC.git] / n2vc / libjuju.py
index d8b0858..e73e552 100644 (file)
@@ -242,7 +242,9 @@ class Libjuju:
         """
         if not model_names:
             raise Exception(
-                "model_names must be a non-empty array. Given value: {}".format(model_names)
+                "model_names must be a non-empty array. Given value: {}".format(
+                    model_names
+                )
             )
         non_existing_models = []
         models = await self.list_models()
@@ -624,7 +626,9 @@ class Libjuju:
                 if await u.is_leader_from_status():
                     unit = u
             if unit is None:
-                raise JujuLeaderUnitNotFound("Cannot execute action: leader unit not found")
+                raise JujuLeaderUnitNotFound(
+                    "Cannot execute action: leader unit not found"
+                )
 
             actions = await application.get_actions()
 
@@ -703,23 +707,17 @@ class Libjuju:
             await self.disconnect_controller(controller)
 
     async def add_relation(
-        self,
-        model_name: str,
-        application_name_1: str,
-        application_name_2: str,
-        relation_1: str,
-        relation_2: str,
+        self, model_name: str, endpoint_1: str, endpoint_2: str,
     ):
         """Add relation
 
-        :param: model_name:             Model name
-        :param: application_name_1      First application name
-        :param: application_name_2:     Second application name
-        :param: relation_1:             First relation name
-        :param: relation_2:             Second relation name
+        :param: model_name:     Model name
+        :param: endpoint_1      First endpoint name
+                                ("app:endpoint" format or directly the saas name)
+        :param: endpoint_2:     Second endpoint name (^ same format)
         """
 
-        self.log.debug("Adding relation: {} -> {}".format(relation_1, relation_2))
+        self.log.debug("Adding relation: {} -> {}".format(endpoint_1, endpoint_2))
 
         # Get controller
         controller = await self.get_controller()
@@ -727,13 +725,9 @@ class Libjuju:
         # Get model
         model = await self.get_model(controller, model_name)
 
-        # Build relation strings
-        r1 = "{}:{}".format(application_name_1, relation_1)
-        r2 = "{}:{}".format(application_name_2, relation_2)
-
         # Add relation
         try:
-            await model.add_relation(relation1=r1, relation2=r2)
+            await model.add_relation(endpoint_1, endpoint_2)
         except JujuAPIError as e:
             if "not found" in e.message:
                 self.log.warning("Relation not found: {}".format(e.message))
@@ -747,6 +741,28 @@ class Libjuju:
             await self.disconnect_model(model)
             await self.disconnect_controller(controller)
 
+    async def consume(
+        self, offer_url: str, model_name: str,
+    ):
+        """
+        Adds a remote offer to the model. Relations can be created later using "juju relate".
+
+        :param: offer_url:      Offer Url
+        :param: model_name:     Model name
+
+        :raises ParseError if there's a problem parsing the offer_url
+        :raises JujuError if remote offer includes and endpoint
+        :raises JujuAPIError if the operation is not successful
+        """
+        controller = await self.get_controller()
+        model = await controller.get_model(model_name)
+
+        try:
+            await model.consume(offer_url)
+        finally:
+            await self.disconnect_model(model)
+            await self.disconnect_controller(controller)
+
     async def destroy_model(self, model_name: str, total_timeout: float):
         """
         Destroy model
@@ -848,9 +864,7 @@ class Libjuju:
             # wait for machine removal
             machines = await model.get_machines()
             while machine_id in machines and time.time() < end:
-                self.log.debug(
-                    "Waiting for machine {} is destroyed".format(machine_id)
-                )
+                self.log.debug("Waiting for machine {} is destroyed".format(machine_id))
                 await asyncio.sleep(0.5)
                 machines = await model.get_machines()
             self.log.debug("Machine destroyed: {}".format(machine_id))