From 31264d4f6e228acb345d7f7bf1c8bf25c7f03ccf Mon Sep 17 00:00:00 2001 From: Mia Altieri Date: Mon, 9 Oct 2023 06:54:27 +0000 Subject: [PATCH] fmt + lint --- lib/charms/mongodb/v0/helpers.py | 48 ++++++++++++++++++++++- lib/charms/mongodb/v0/mongodb_provider.py | 27 +++---------- src/charm.py | 2 + 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/lib/charms/mongodb/v0/helpers.py b/lib/charms/mongodb/v0/helpers.py index b7aa6f1b5..421b1fef1 100644 --- a/lib/charms/mongodb/v0/helpers.py +++ b/lib/charms/mongodb/v0/helpers.py @@ -10,6 +10,8 @@ from typing import List from charms.mongodb.v0.mongodb import MongoDBConfiguration, MongoDBConnection +from ops.charm import CharmBase, RelationDepartedEvent +from ops.framework import Object from ops.model import ( ActiveStatus, BlockedStatus, @@ -29,7 +31,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 8 +LIBPATCH = 9 # path to store mongodb ketFile KEY_FILE = "keyFile" @@ -49,6 +51,50 @@ logger = logging.getLogger(__name__) +class MongoDBHelper(Object): + """In this class, we manage client database relations.""" + + def __init__(self, charm: CharmBase) -> None: + """Constructor for MongoDBProvider object. + + Args: + charm: the charm for which this relation is provided + """ + super().__init__(charm, None) + self.charm = charm + + def _is_departed_removed_relation(self, event: RelationDepartedEvent): + """Sets app metadata indicating if a relation should be removed. + + Relation-broken executes after relation-departed, and it must know if it should remove the + related user. Relation-broken doesn't have access to `event.departing_unit`, so we make + use of it here. + + We receive relation departed events when: the relation has been removed, units are scaling + down, or the application has been removed. Only proceed to process user removal if the + relation has been removed. + """ + if not self.charm.unit.is_leader(): + return + + # assume relation departed is due to manual removal of relation. + relation_departed = True + + # check if relation departed is due to current unit being removed. (i.e. scaling down the + # application.) + if event.departing_unit == self.charm.unit: + logger.debug( + "Relation departed is due to scale down, no need to process removed relation in RelationBrokenEvent." + ) + relation_departed = False + + self.charm.app_peer_data[f"relation_{event.relation.id}_departed"] = json.dumps( + relation_departed + ) + + return relation_departed + + # noinspection GrazieInspection def get_create_user_cmd(config: MongoDBConfiguration, mongo_path=MONGO_SHELL) -> List[str]: """Creates initial admin user for MongoDB. diff --git a/lib/charms/mongodb/v0/mongodb_provider.py b/lib/charms/mongodb/v0/mongodb_provider.py index b63ad76f5..63c7673ac 100644 --- a/lib/charms/mongodb/v0/mongodb_provider.py +++ b/lib/charms/mongodb/v0/mongodb_provider.py @@ -83,28 +83,11 @@ def __init__(self, charm: CharmBase, substrate="k8s", relation_name: str = "data ) def _on_relation_departed(self, event): - """Checks if users should be removed on the following event (relation-broken). - - Relation-broken executes after relation-departed, and it must know if it should remove the - related user. Relation-broken doesn't have access to `event.departing_unit`, so we make - use of it here. - - We receive relation departed events when: the relation has been removed, units are scaling - down, or the application has been removed. Only proceed to process user removal if the - relation has been removed. - """ - if not self.charm.unit.is_leader(): - return - - # assume relation departed is due to manual removal of relation. - relation_departed = True - - # check if relation departed is due to current unit being removed. (i.e. scaling down the - # application.) - if event.departing_unit == self.charm.unit: - relation_departed = False - - self.charm.app_peer_data[f"relation_{event.relation.id}_departed"] = json.dumps(relation_departed) + """Checks if users should be removed on the following event (relation-broken).""" + if self.charm.mongodb_helpers._is_departed_removed_relation(event): + logger.info( + "Relation departed event occurred due to relation removal, proceed to clean up users in RelationBroken." + ) def _on_relation_event(self, event): """Handle relation joined events. diff --git a/src/charm.py b/src/charm.py index b8e12d1de..59ef94959 100755 --- a/src/charm.py +++ b/src/charm.py @@ -19,6 +19,7 @@ TLS_EXT_PEM_FILE, TLS_INT_CA_FILE, TLS_INT_PEM_FILE, + MongoDBHelper, build_unit_status, copy_licenses_to_unit, generate_keyfile, @@ -123,6 +124,7 @@ def __init__(self, *args): self.framework.observe(self.on.secret_changed, self._on_secret_changed) # handle provider side of relations + self.mongodb_helpers = MongoDBHelper(self) self.client_relations = MongoDBProvider(self, substrate=Config.SUBSTRATE) self.legacy_client_relations = MongoDBLegacyProvider(self) self.tls = MongoDBTLS(self, Config.Relations.PEERS, substrate=Config.SUBSTRATE)