From 08e9494f402b4c501c8c67013d984b3ae66d49ce Mon Sep 17 00:00:00 2001 From: Mia Altieri Date: Tue, 5 Dec 2023 16:39:07 +0000 Subject: [PATCH] PR feedback --- .../mongodb/v0/config_server_interface.py | 21 ++++++++++++------- tests/unit/test_config_server_lib.py | 10 ++++----- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/charms/mongodb/v0/config_server_interface.py b/lib/charms/mongodb/v0/config_server_interface.py index 34af58335..ca4c210be 100644 --- a/lib/charms/mongodb/v0/config_server_interface.py +++ b/lib/charms/mongodb/v0/config_server_interface.py @@ -8,6 +8,10 @@ """ import logging +from charms.data_platform_libs.v0.data_interfaces import ( + DatabaseProvides, + DatabaseRequires, +) from charms.mongodb.v1.helpers import add_args_to_env, get_mongos_args from charms.mongodb.v1.mongos import MongosConnection from ops.charm import CharmBase, EventBase @@ -31,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 = 3 +LIBPATCH = 4 class ClusterProvider(Object): @@ -43,6 +47,7 @@ def __init__( """Constructor for ShardingProvider object.""" self.relation_name = relation_name self.charm = charm + self.database_provides = DatabaseProvides(self.charm, relation_name=self.relation_name) super().__init__(charm, self.relation_name) self.framework.observe( @@ -104,9 +109,7 @@ def update_relation_data(self, relation_id: int, data: dict) -> None: that should be updated in the relation. """ if self.charm.unit.is_leader(): - relation = self.charm.model.get_relation(self.relation_name, relation_id) - if relation: - relation.data[self.charm.model.app].update(data) + self.database_provides.update_relation_data(relation_id, data) def generate_config_server_db(self) -> str: """Generates the config server database for mongos to connect to.""" @@ -128,6 +131,12 @@ def __init__( """Constructor for ShardingProvider object.""" self.relation_name = relation_name self.charm = charm + self.database_requires = DatabaseRequires( + self.charm, + relation_name=self.relation_name, + database_name=self.charm.database, + extra_user_roles=self.charm.extra_user_roles, + ) super().__init__(charm, self.relation_name) self.framework.observe( @@ -237,8 +246,6 @@ def update_relation_data(self, relation_id: int, data: dict) -> None: that should be updated in the relation. """ if self.charm.unit.is_leader(): - relation = self.charm.model.get_relation(self.relation_name, relation_id) - if relation: - relation.data[self.charm.model.app].update(data) + self.database_requires.update_relation_data(relation_id, data) # END: helper functions diff --git a/tests/unit/test_config_server_lib.py b/tests/unit/test_config_server_lib.py index 23150aaba..2e2e83291 100644 --- a/tests/unit/test_config_server_lib.py +++ b/tests/unit/test_config_server_lib.py @@ -22,8 +22,8 @@ def setUp(self): self.charm = self.harness.charm self.addCleanup(self.harness.cleanup) - @patch("charm.ClusterProvider._update_relation_data") - def test_on_relation_joined_failed_hook_checks(self, _update_relation_data): + @patch("charm.ClusterProvider.update_relation_data") + def test_on_relation_joined_failed_hook_checks(self, update_relation_data): """Tests that no relation data is set when cluster joining conditions are not met.""" def is_not_config_mock_call(*args): @@ -36,7 +36,7 @@ def is_not_config_mock_call(*args): self.harness.charm.is_role = is_not_config_mock_call relation_id = self.harness.add_relation("cluster", "mongos") self.harness.add_relation_unit(relation_id, "mongos/0") - _update_relation_data.assert_not_called() + update_relation_data.assert_not_called() # fails because db has not been initialized del self.harness.charm.app_peer_data["db_initialised"] @@ -47,9 +47,9 @@ def is_config_mock_call(*args): self.harness.charm.is_role = is_config_mock_call self.harness.add_relation_unit(relation_id, "mongos/1") - _update_relation_data.assert_not_called() + update_relation_data.assert_not_called() # fails because not leader self.harness.set_leader(False) self.harness.add_relation_unit(relation_id, "mongos/2") - _update_relation_data.assert_not_called() + update_relation_data.assert_not_called()