Revert "Integrate NBI and Prometheus"
[osm/devops.git] / installers / charm / osm-nbi / src / charm.py
index 454b155..b19beae 100755 (executable)
@@ -30,6 +30,7 @@ See more: https://charmhub.io/osm
 import logging
 from typing import Any, Dict
 
+from charms.data_platform_libs.v0.data_interfaces import DatabaseRequires
 from charms.kafka_k8s.v0.kafka import KafkaEvents, KafkaRequires
 from charms.nginx_ingress_integrator.v0.ingress import IngressRequires
 from charms.observability_libs.v1.kubernetes_service_patch import KubernetesServicePatch
@@ -47,7 +48,7 @@ from ops.framework import StoredState
 from ops.main import main
 from ops.model import ActiveStatus, Container
 
-from legacy_interfaces import KeystoneClient, MongoClient, PrometheusClient
+from legacy_interfaces import KeystoneClient, PrometheusClient
 
 HOSTPATHS = [
     HostPath(
@@ -82,7 +83,9 @@ class OsmNbiCharm(CharmBase):
         )
         self.kafka = KafkaRequires(self)
         self.nbi = NbiProvides(self)
-        self.mongodb_client = MongoClient(self, "mongodb")
+        self.mongodb_client = DatabaseRequires(
+            self, "mongodb", database_name="osm", extra_user_roles="admin"
+        )
         self.prometheus_client = PrometheusClient(self, "prometheus")
         self.keystone_client = KeystoneClient(self, "keystone")
         self._observe_charm_events()
@@ -113,7 +116,8 @@ class OsmNbiCharm(CharmBase):
             # Eventually it will become ready after the first pebble-ready event.
             check_container_ready(self.container)
 
-            self._configure_service(self.container)
+            if not self.debug_mode.started:
+                self._configure_service(self.container)
             self._update_ingress_config()
             self._update_nbi_relation()
             # Update charm status
@@ -178,17 +182,25 @@ class OsmNbiCharm(CharmBase):
             # Relation events
             self.on.kafka_available: self._on_config_changed,
             self.on["kafka"].relation_broken: self._on_required_relation_broken,
+            self.mongodb_client.on.database_created: self._on_config_changed,
+            self.on["mongodb"].relation_broken: self._on_required_relation_broken,
             # Action events
             self.on.get_debug_mode_information_action: self._on_get_debug_mode_information_action,
             self.on.nbi_relation_joined: self._update_nbi_relation,
         }
-        for relation in [self.on[rel_name] for rel_name in ["mongodb", "prometheus", "keystone"]]:
+        for relation in [self.on[rel_name] for rel_name in ["prometheus", "keystone"]]:
             event_handler_mapping[relation.relation_changed] = self._on_config_changed
             event_handler_mapping[relation.relation_broken] = self._on_required_relation_broken
 
         for event, handler in event_handler_mapping.items():
             self.framework.observe(event, handler)
 
+    def _is_database_available(self) -> bool:
+        try:
+            return self.mongodb_client.is_resource_created()
+        except KeyError:
+            return False
+
     def _validate_config(self) -> None:
         """Validate charm configuration.
 
@@ -208,7 +220,7 @@ class OsmNbiCharm(CharmBase):
 
         if not self.kafka.host or not self.kafka.port:
             missing_relations.append("kafka")
-        if self.mongodb_client.is_missing_data_in_unit():
+        if not self._is_database_available():
             missing_relations.append("mongodb")
         if self.prometheus_client.is_missing_data_in_app():
             missing_relations.append("prometheus")
@@ -248,10 +260,11 @@ class OsmNbiCharm(CharmBase):
                 "nbi": {
                     "override": "replace",
                     "summary": "nbi service",
-                    "command": "python3 -m osm_nbi.nbi",
+                    "command": "/bin/sh -c 'cd /app/osm_nbi && python3 -m osm_nbi.nbi'",  # cd /app/osm_nbi is needed until we upgrade Juju to 3.x
                     "startup": "enabled",
                     "user": "appuser",
                     "group": "appuser",
+                    "working-dir": "/app/osm_nbi",  # This parameter has no effect in juju 2.9.x
                     "environment": {
                         # General configuration
                         "OSMNBI_SERVER_ENABLE_TEST": False,
@@ -262,13 +275,13 @@ class OsmNbiCharm(CharmBase):
                         "OSMNBI_MESSAGE_DRIVER": "kafka",
                         # Database configuration
                         "OSMNBI_DATABASE_DRIVER": "mongo",
-                        "OSMNBI_DATABASE_URI": self.mongodb_client.connection_string,
+                        "OSMNBI_DATABASE_URI": self._get_mongodb_uri(),
                         "OSMNBI_DATABASE_COMMONKEY": self.config["database-commonkey"],
                         # Storage configuration
                         "OSMNBI_STORAGE_DRIVER": "mongo",
                         "OSMNBI_STORAGE_PATH": "/app/storage",
                         "OSMNBI_STORAGE_COLLECTION": "files",
-                        "OSMNBI_STORAGE_URI": self.mongodb_client.connection_string,
+                        "OSMNBI_STORAGE_URI": self._get_mongodb_uri(),
                         # Prometheus configuration
                         "OSMNBI_PROMETHEUS_HOST": self.prometheus_client.hostname,
                         "OSMNBI_PROMETHEUS_PORT": self.prometheus_client.port,
@@ -293,6 +306,9 @@ class OsmNbiCharm(CharmBase):
             },
         }
 
+    def _get_mongodb_uri(self):
+        return list(self.mongodb_client.fetch_relation_data().values())[0]["uris"]
+
 
 if __name__ == "__main__":  # pragma: no cover
     main(OsmNbiCharm)