Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Dec 12, 2023
1 parent 0f9cb55 commit 3eb5d7f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 62 deletions.
82 changes: 22 additions & 60 deletions lib/charms/mongodb/v0/config_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 5


class ClusterProvider(Object):
Expand Down Expand Up @@ -87,29 +87,16 @@ def _on_relation_changed(self, event) -> None:
self.charm.client_relations.oversee_users(None, None)

# TODO Future PR, use secrets
self.update_relation_data(
event.relation.id,
{
KEYFILE_KEY: self.charm.get_secret(
Config.Relations.APP_SCOPE, Config.Secrets.SECRET_KEYFILE_NAME
),
CONFIG_SERVER_DB_KEY: config_server_db,
},
)

def update_relation_data(self, relation_id: int, data: dict) -> None:
"""Updates a set of key-value pairs in the relation.
This function writes in the application data bag, therefore, only the leader unit can call
it.
Args:
relation_id: the identifier for a particular relation.
data: dict containing the key-value pairs
that should be updated in the relation.
"""
if self.charm.unit.is_leader():
self.database_provides.update_relation_data(relation_id, data)
self.database_provides.update_relation_data(
event.relation.id,
{
KEYFILE_KEY: self.charm.get_secret(
Config.Relations.APP_SCOPE, Config.Secrets.SECRET_KEYFILE_NAME
),
CONFIG_SERVER_DB_KEY: config_server_db,
},
)

def generate_config_server_db(self) -> str:
"""Generates the config server database for mongos to connect to."""
Expand All @@ -136,45 +123,34 @@ def __init__(
relation_name=self.relation_name,
database_name=self.charm.database,
extra_user_roles=self.charm.extra_user_roles,
additional_secret_fields=[KEYFILE_KEY],
)

super().__init__(charm, self.relation_name)
self.framework.observe(
charm.on[self.relation_name].relation_created, self._on_relation_created_event
charm.on[self.relation_name].relation_created,
self.database_requires._on_relation_created_event,
)
self.framework.observe(
charm.on[self.relation_name].relation_changed, self._on_relation_changed
)
# TODO Future PRs handle scale down

def _on_relation_created_event(self, event):
"""Sets database and extra user roles in the relation."""
if not self.charm.unit.is_leader():
return

if not self.charm.database:
logger.info("Waiting for database from application")
event.defer()
return

rel_data = {"database": self.charm.database}
if self.charm.extra_user_roles:
rel_data["extra-user-roles"] = str(self.charm.extra_user_roles)

self.update_relation_data(event.relation.id, rel_data)

def _on_relation_changed(self, event) -> None:
"""Starts/restarts monogs with config server information."""
relation_data = event.relation.data[event.app]
if not relation_data.get(KEYFILE_KEY) or not relation_data.get(CONFIG_SERVER_DB_KEY):
key_file_contents = self.database_requires.fetch_relation_field(
event.relation.id, KEYFILE_KEY
)
config_server_db = self.database_requires.fetch_relation_field(
event.relation.id, CONFIG_SERVER_DB_KEY
)
if not key_file_contents or not config_server_db:
event.defer()
self.charm.unit.status = WaitingStatus("Waiting for secrets from config-server")
return

updated_keyfile = self.update_keyfile(key_file_contents=relation_data.get(KEYFILE_KEY))
updated_config = self.update_config_server_db(
config_server_db=relation_data.get(CONFIG_SERVER_DB_KEY)
)
updated_keyfile = self.update_keyfile(key_file_contents=key_file_contents)
updated_config = self.update_config_server_db(config_server_db=config_server_db)

# avoid restarting mongos when possible
if not updated_keyfile and not updated_config and self.is_mongos_running():
Expand Down Expand Up @@ -234,18 +210,4 @@ def update_keyfile(self, key_file_contents: str) -> bool:

return True

def update_relation_data(self, relation_id: int, data: dict) -> None:
"""Updates a set of key-value pairs in the relation.
This function writes in the application data bag, therefore, only the leader unit can call
it.
Args:
relation_id: the identifier for a particular relation.
data: dict containing the key-value pairs
that should be updated in the relation.
"""
if self.charm.unit.is_leader():
self.database_requires.update_relation_data(relation_id, data)

# END: helper functions
4 changes: 2 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def set_user_role(self, roles: List[str]):
config_server_rel = self.model.relations[
Config.Relations.CLUSTER_RELATIONS_NAME
][0]
self.cluster.update_relation_data(
self.cluster.database_requires.update_relation_data(
config_server_rel.id, {USER_ROLES_TAG: roles_str}
)

Expand All @@ -264,7 +264,7 @@ def set_database(self, database: str):
config_server_rel = self.model.relations[
Config.Relations.CLUSTER_RELATIONS_NAME
][0]
self.cluster.update_relation_data(
self.cluster.database_requires.update_relation_data(
config_server_rel.id, {DATABASE_TAG: database}
)

Expand Down

0 comments on commit 3eb5d7f

Please sign in to comment.