Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Dec 5, 2023
1 parent 875d368 commit 08e9494
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 14 additions & 7 deletions lib/charms/mongodb/v0/config_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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(
Expand Down Expand Up @@ -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."""
Expand All @@ -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(
Expand Down Expand Up @@ -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
10 changes: 5 additions & 5 deletions tests/unit/test_config_server_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"]
Expand All @@ -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()

0 comments on commit 08e9494

Please sign in to comment.