diff --git a/lib/charms/mongodb/v0/config_server_interface.py b/lib/charms/mongodb/v0/config_server_interface.py index eeb1ee299..65242d9b3 100644 --- a/lib/charms/mongodb/v0/config_server_interface.py +++ b/lib/charms/mongodb/v0/config_server_interface.py @@ -35,7 +35,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 1 +LIBPATCH = 2 class ClusterProvider(Object): @@ -139,6 +139,7 @@ def __init__( self.database_requires = DatabaseRequires( self.charm, relation_name=self.relation_name, + relations_aliases=[self.relation_name], database_name=self.charm.database, extra_user_roles=self.charm.extra_user_roles, additional_secret_fields=[KEYFILE_KEY], @@ -149,11 +150,24 @@ def __init__( charm.on[self.relation_name].relation_created, self.database_requires._on_relation_created_event, ) + + self.framework.observe( + self.database_requires.on.database_created, self._on_database_created + ) self.framework.observe( charm.on[self.relation_name].relation_changed, self._on_relation_changed ) # TODO Future PRs handle scale down + def _on_database_created(self, event) -> None: + if not self.charm.unit.is_leader(): + return + + logger.info("Database and user created for mongos application") + self.charm.set_secret(Config.Relations.APP_SCOPE, Config.Secrets.USERNAME, event.username) + self.charm.set_secret(Config.Relations.APP_SCOPE, Config.Secrets.PASSWORD, event.password) + self.charm.share_connection_info() + def _on_relation_changed(self, event) -> None: """Starts/restarts monogs with config server information.""" key_file_contents = self.database_requires.fetch_relation_field( @@ -186,7 +200,6 @@ def _on_relation_changed(self, event) -> None: event.defer() return - self.charm.share_uri() self.charm.unit.status = ActiveStatus() # BEGIN: helper functions diff --git a/lib/charms/mongodb/v1/mongos.py b/lib/charms/mongodb/v1/mongos.py index ee2c11678..90711652b 100644 --- a/lib/charms/mongodb/v1/mongos.py +++ b/lib/charms/mongodb/v1/mongos.py @@ -52,8 +52,11 @@ class MongosConfiguration: @property def uri(self): """Return URI concatenated from fields.""" - hosts = [f"{host}:{self.port}" for host in self.hosts] - hosts = ",".join(hosts) + # mongos using Unix Domain Socket to communicate do not use port + if self.port: + self.hosts = [f"{host}:{self.port}" for host in self.hosts] + + hosts = ",".join(self.hosts) # Auth DB should be specified while user connects to application DB. auth_source = "" if self.database != "admin":