Fix 1539: Helm v2.17.0 and set default value for stablerepourl
[osm/devops.git] / installers / charm / lcm / src / charm.py
index e9552fd..9b025c9 100755 (executable)
@@ -42,15 +42,16 @@ PORT = 9999
 
 
 class ConfigModel(ModelValidator):
-    vca_host: str
-    vca_port: int
-    vca_user: str
-    vca_secret: str
-    vca_pubkey: str
-    vca_cacert: str
-    vca_cloud: str
-    vca_k8s_cloud: str
+    vca_host: Optional[str]
+    vca_port: Optional[int]
+    vca_user: Optional[str]
+    vca_secret: Optional[str]
+    vca_pubkey: Optional[str]
+    vca_cacert: Optional[str]
+    vca_cloud: Optional[str]
+    vca_k8s_cloud: Optional[str]
     database_commonkey: str
+    mongodb_uri: Optional[str]
     log_level: str
     vca_apiproxy: Optional[str]
     # Model-config options
@@ -107,6 +108,7 @@ class ConfigModel(ModelValidator):
     vca_model_config_test_mode: Optional[bool]
     vca_model_config_transmit_vendor_metrics: Optional[bool]
     vca_model_config_update_status_hook_interval: Optional[str]
+    vca_stablerepourl: Optional[str]
 
     @validator("log_level")
     def validate_log_level(cls, v):
@@ -114,6 +116,12 @@ class ConfigModel(ModelValidator):
             raise ValueError("value must be INFO or DEBUG")
         return v
 
+    @validator("mongodb_uri")
+    def validate_mongodb_uri(cls, v):
+        if v and not v.startswith("mongodb://"):
+            raise ValueError("mongodb_uri is not properly formed")
+        return v
+
 
 class LcmCharm(CharmedOsmBase):
     def __init__(self, *args) -> NoReturn:
@@ -136,7 +144,7 @@ class LcmCharm(CharmedOsmBase):
 
         if self.kafka_client.is_missing_data_in_unit():
             missing_relations.append("kafka")
-        if self.mongodb_client.is_missing_data_in_unit():
+        if not config.mongodb_uri and self.mongodb_client.is_missing_data_in_unit():
             missing_relations.append("mongodb")
         if self.ro_client.is_missing_data_in_app():
             missing_relations.append("ro")
@@ -147,10 +155,16 @@ class LcmCharm(CharmedOsmBase):
     def build_pod_spec(self, image_info):
         # Validate config
         config = ConfigModel(**dict(self.config))
+
+        if config.mongodb_uri and not self.mongodb_client.is_missing_data_in_unit():
+            raise Exception("Mongodb data cannot be provided via config and relation")
+
         # Check relations
         self._check_missing_dependencies(config)
+
         # Create Builder for the PodSpec
         pod_spec_builder = PodSpecV3Builder()
+
         # Build Container
         container_builder = ContainerV3Builder(self.app.name, image_info)
         container_builder.add_port(name=self.app.name, port=PORT)
@@ -169,37 +183,47 @@ class LcmCharm(CharmedOsmBase):
                 "OSMLCM_MESSAGE_PORT": self.kafka_client.port,
                 # Database configuration
                 "OSMLCM_DATABASE_DRIVER": "mongo",
-                "OSMLCM_DATABASE_URI": self.mongodb_client.connection_string,
+                "OSMLCM_DATABASE_URI": config.mongodb_uri
+                or self.mongodb_client.connection_string,
                 "OSMLCM_DATABASE_COMMONKEY": config.database_commonkey,
                 # Storage configuration
                 "OSMLCM_STORAGE_DRIVER": "mongo",
                 "OSMLCM_STORAGE_PATH": "/app/storage",
                 "OSMLCM_STORAGE_COLLECTION": "files",
-                "OSMLCM_STORAGE_URI": self.mongodb_client.connection_string,
-                # VCA configuration
-                "OSMLCM_VCA_HOST": config.vca_host,
-                "OSMLCM_VCA_PORT": config.vca_port,
-                "OSMLCM_VCA_USER": config.vca_user,
-                "OSMLCM_VCA_PUBKEY": config.vca_pubkey,
-                "OSMLCM_VCA_SECRET": config.vca_secret,
-                "OSMLCM_VCA_CACERT": config.vca_cacert,
-                "OSMLCM_VCA_CLOUD": config.vca_cloud,
-                "OSMLCM_VCA_K8S_CLOUD": config.vca_k8s_cloud,
+                "OSMLCM_STORAGE_URI": config.mongodb_uri
+                or self.mongodb_client.connection_string,
+                "OSMLCM_VCA_STABLEREPOURL": config.vca_stablerepourl,
             }
         )
-        if config.vca_apiproxy:
-            container_builder.add_env("OSMLCM_VCA_APIPROXY", config.vca_apiproxy)
-
-        model_config_envs = {
-            f"OSMLCM_{k.upper()}": v
-            for k, v in self.config.items()
-            if k.startswith("vca_model_config")
-        }
-        if model_config_envs:
-            container_builder.add_envs(model_config_envs)
+        if config.vca_host:
+            container_builder.add_envs(
+                {
+                    # VCA configuration
+                    "OSMLCM_VCA_HOST": config.vca_host,
+                    "OSMLCM_VCA_PORT": config.vca_port,
+                    "OSMLCM_VCA_USER": config.vca_user,
+                    "OSMLCM_VCA_PUBKEY": config.vca_pubkey,
+                    "OSMLCM_VCA_SECRET": config.vca_secret,
+                    "OSMLCM_VCA_CACERT": config.vca_cacert,
+                    "OSMLCM_VCA_CLOUD": config.vca_cloud,
+                    "OSMLCM_VCA_K8S_CLOUD": config.vca_k8s_cloud,
+                }
+            )
+            if config.vca_apiproxy:
+                container_builder.add_env("OSMLCM_VCA_APIPROXY", config.vca_apiproxy)
+
+            model_config_envs = {
+                f"OSMLCM_{k.upper()}": v
+                for k, v in self.config.items()
+                if k.startswith("vca_model_config")
+            }
+            if model_config_envs:
+                container_builder.add_envs(model_config_envs)
         container = container_builder.build()
+
         # Add container to pod spec
         pod_spec_builder.add_container(container)
+
         return pod_spec_builder.build()