Change add_relation function in libjuju.py to accept saas 39/9639/4
authorDavid Garcia <david.garcia@canonical.com>
Tue, 25 Aug 2020 14:10:07 +0000 (16:10 +0200)
committergarciadav <david.garcia@canonical.com>
Fri, 11 Sep 2020 12:34:31 +0000 (14:34 +0200)
Before, app_1, endpoint1, app_2, endpoint2 parameters were needed.
Now we directly pass the endpoint names of the relation with the right format.
If "app:endpoint", it is understood that the endpoint it of a charm in the current model.
If "name", it is understood that the endpoint is a consumed endpoint from another model.

This function allows to cross-model-relate charms to external models

Change-Id: I84ab45ba735a1960ab742d9ec731c357ec1042c6
Signed-off-by: David Garcia <david.garcia@canonical.com>
n2vc/libjuju.py
n2vc/n2vc_juju_conn.py
n2vc/tests/unit/test_libjuju.py

index d8b0858..ceb5e02 100644 (file)
@@ -705,21 +705,18 @@ class Libjuju:
     async def add_relation(
         self,
         model_name: str,
-        application_name_1: str,
-        application_name_2: str,
-        relation_1: str,
-        relation_2: 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 +724,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))
index 690d3be..31bdd6e 100644 (file)
@@ -709,10 +709,8 @@ class N2VCJujuConnector(N2VCConnector):
         try:
             await self.libjuju.add_relation(
                 model_name=model_1,
-                application_name_1=app_1,
-                application_name_2=app_2,
-                relation_1=endpoint_1,
-                relation_2=endpoint_2,
+                endpoint_1="{}:{}".format(app_1, endpoint_1),
+                endpoint_2="{}:{}".format(app_2, endpoint_2),
             )
         except Exception as e:
             message = "Error adding relation between {} and {}: {}".format(
index 27f0454..a789bc8 100644 (file)
@@ -670,9 +670,7 @@ class AddRelationTest(LibjujuTestCase):
         mock_add_relation.side_effect = JujuAPIError(result)
 
         self.loop.run_until_complete(
-            self.libjuju.add_relation(
-                "model", "app1", "app2", "relation1", "relation2",
-            )
+            self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
         )
 
         mock_warning.assert_called_with("Relation not found: not found")
@@ -696,9 +694,7 @@ class AddRelationTest(LibjujuTestCase):
         mock_add_relation.side_effect = JujuAPIError(result)
 
         self.loop.run_until_complete(
-            self.libjuju.add_relation(
-                "model", "app1", "app2", "relation1", "relation2",
-            )
+            self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
         )
 
         mock_warning.assert_called_with("Relation already exists: already exists")
@@ -719,9 +715,7 @@ class AddRelationTest(LibjujuTestCase):
 
         with self.assertRaises(JujuAPIError):
             self.loop.run_until_complete(
-                self.libjuju.add_relation(
-                    "model", "app1", "app2", "relation1", "relation2",
-                )
+                self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
             )
 
             mock_disconnect_controller.assert_called_once()
@@ -738,13 +732,31 @@ class AddRelationTest(LibjujuTestCase):
         mock_get_model.return_value = juju.model.Model()
 
         self.loop.run_until_complete(
-            self.libjuju.add_relation(
-                "model", "app1", "app2", "relation1", "relation2",
-            )
+            self.libjuju.add_relation("model", "app1:relation1", "app2:relation2",)
+        )
+
+        mock_add_relation.assert_called_with(
+            "app1:relation1", "app2:relation2"
+        )
+        mock_disconnect_controller.assert_called_once()
+        mock_disconnect_model.assert_called_once()
+
+    def test_saas(
+        self,
+        mock_add_relation,
+        mock_disconnect_controller,
+        mock_disconnect_model,
+        mock_get_model,
+        mock_get_controller,
+    ):
+        mock_get_model.return_value = juju.model.Model()
+
+        self.loop.run_until_complete(
+            self.libjuju.add_relation("model", "app1:relation1", "saas_name",)
         )
 
         mock_add_relation.assert_called_with(
-            relation1="app1:relation1", relation2="app2:relation2"
+            "app1:relation1", "saas_name"
         )
         mock_disconnect_controller.assert_called_once()
         mock_disconnect_model.assert_called_once()