Add NBI Charm Library
[osm/devops.git] / installers / charm / osm-nbi / src / charm.py
index 964050a..162e2a5 100755 (executable)
@@ -40,8 +40,9 @@ from charms.osm_libs.v0.utils import (
     check_container_ready,
     check_service_active,
 )
     check_container_ready,
     check_service_active,
 )
+from charms.osm_nbi.v0.nbi import NbiProvides
 from lightkube.models.core_v1 import ServicePort
 from lightkube.models.core_v1 import ServicePort
-from ops.charm import ActionEvent, CharmBase
+from ops.charm import ActionEvent, CharmBase, RelationJoinedEvent
 from ops.framework import StoredState
 from ops.main import main
 from ops.model import ActiveStatus, Container
 from ops.framework import StoredState
 from ops.main import main
 from ops.model import ActiveStatus, Container
@@ -80,6 +81,7 @@ class OsmNbiCharm(CharmBase):
             },
         )
         self.kafka = KafkaRequires(self)
             },
         )
         self.kafka = KafkaRequires(self)
+        self.nbi = NbiProvides(self)
         self.mongodb_client = MongoClient(self, "mongodb")
         self.prometheus_client = PrometheusClient(self, "prometheus")
         self.keystone_client = KeystoneClient(self, "keystone")
         self.mongodb_client = MongoClient(self, "mongodb")
         self.prometheus_client = PrometheusClient(self, "prometheus")
         self.keystone_client = KeystoneClient(self, "keystone")
@@ -113,7 +115,7 @@ class OsmNbiCharm(CharmBase):
 
             self._configure_service(self.container)
             self._update_ingress_config()
 
             self._configure_service(self.container)
             self._update_ingress_config()
-
+            self._update_nbi_relation()
             # Update charm status
             self._on_update_status()
         except CharmError as e:
             # Update charm status
             self._on_update_status()
         except CharmError as e:
@@ -145,6 +147,11 @@ class OsmNbiCharm(CharmBase):
         finally:
             self._on_update_status()
 
         finally:
             self._on_update_status()
 
+    def _update_nbi_relation(self, event: RelationJoinedEvent = None) -> None:
+        """Handler for the nbi-relation-joined event."""
+        if self.unit.is_leader():
+            self.nbi.set_host_info(self.app.name, SERVICE_PORT, event.relation if event else None)
+
     def _on_get_debug_mode_information_action(self, event: ActionEvent) -> None:
         """Handler for the get-debug-mode-information action event."""
         if not self.debug_mode.started:
     def _on_get_debug_mode_information_action(self, event: ActionEvent) -> None:
         """Handler for the get-debug-mode-information action event."""
         if not self.debug_mode.started:
@@ -173,6 +180,7 @@ class OsmNbiCharm(CharmBase):
             self.on["kafka"].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["kafka"].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"]]:
             event_handler_mapping[relation.relation_changed] = self._on_config_changed
         }
         for relation in [self.on[rel_name] for rel_name in ["mongodb", "prometheus", "keystone"]]:
             event_handler_mapping[relation.relation_changed] = self._on_config_changed