Bug 1981 fixed
[osm/N2VC.git] / n2vc / k8s_juju_conn.py
index 149947d..fb15be7 100644 (file)
@@ -13,6 +13,7 @@
 #     limitations under the License.
 
 import asyncio
+from typing import Union
 import os
 import uuid
 import yaml
@@ -20,6 +21,7 @@ import tempfile
 import binascii
 
 from n2vc.config import EnvironConfig
+from n2vc.definitions import RelationEndpoint
 from n2vc.exceptions import K8sException
 from n2vc.k8s_conn import K8sConnector
 from n2vc.kubectl import Kubectl
@@ -128,7 +130,7 @@ class K8sJujuConnector(K8sConnector):
             cleanup_data.append(
                 {
                     "delete": kubectl.delete_cluster_role,
-                    "args": (metadata_name),
+                    "args": (metadata_name,),
                 }
             )
 
@@ -139,7 +141,7 @@ class K8sJujuConnector(K8sConnector):
             cleanup_data.append(
                 {
                     "delete": kubectl.delete_service_account,
-                    "args": (metadata_name),
+                    "args": (metadata_name,),
                 }
             )
 
@@ -150,7 +152,7 @@ class K8sJujuConnector(K8sConnector):
             cleanup_data.append(
                 {
                     "delete": kubectl.delete_service_account,
-                    "args": (metadata_name),
+                    "args": (metadata_name,),
                 }
             )
             token, client_cert_data = await kubectl.get_secret_data(
@@ -302,6 +304,10 @@ class K8sJujuConnector(K8sConnector):
             raise K8sException("bundle must be set")
 
         if bundle.startswith("cs:"):
+            # For Juju Bundles provided by the Charm Store
+            pass
+        elif bundle.startswith("ch:"):
+            # For Juju Bundles provided by the Charm Hub (this only works for juju version >= 2.9)
             pass
         elif bundle.startswith("http"):
             # Download the file
@@ -641,7 +647,7 @@ class K8sJujuConnector(K8sConnector):
         complete_status: bool = False,
         yaml_format: bool = False,
         **kwargs,
-    ) -> dict:
+    ) -> Union[str, dict]:
         """Get the status of the KDU
 
         Get the current status of the KDU instance.
@@ -673,6 +679,53 @@ class K8sJujuConnector(K8sConnector):
 
         return status
 
+    async def add_relation(
+        self,
+        provider: RelationEndpoint,
+        requirer: RelationEndpoint,
+    ):
+        """
+        Add relation between two charmed endpoints
+
+        :param: provider: Provider relation endpoint
+        :param: requirer: Requirer relation endpoint
+        """
+        self.log.debug(f"adding new relation between {provider} and {requirer}")
+        cross_model_relation = (
+            provider.model_name != requirer.model_name
+            or requirer.vca_id != requirer.vca_id
+        )
+        try:
+            if cross_model_relation:
+                # Cross-model relation
+                provider_libjuju = await self._get_libjuju(provider.vca_id)
+                requirer_libjuju = await self._get_libjuju(requirer.vca_id)
+                offer = await provider_libjuju.offer(provider)
+                if offer:
+                    saas_name = await requirer_libjuju.consume(
+                        requirer.model_name, offer, provider_libjuju
+                    )
+                    await requirer_libjuju.add_relation(
+                        requirer.model_name,
+                        requirer.endpoint,
+                        saas_name,
+                    )
+            else:
+                # Standard relation
+                vca_id = provider.vca_id
+                model = provider.model_name
+                libjuju = await self._get_libjuju(vca_id)
+                # add juju relations between two applications
+                await libjuju.add_relation(
+                    model_name=model,
+                    endpoint_1=provider.endpoint,
+                    endpoint_2=requirer.endpoint,
+                )
+        except Exception as e:
+            message = f"Error adding relation between {provider} and {requirer}: {e}"
+            self.log.error(message)
+            raise Exception(message=message)
+
     async def update_vca_status(self, vcastatus: dict, kdu_instance: str, **kwargs):
         """
         Add all configs, actions, executed actions of all applications in a model to vcastatus dict