From 08e6b602ac5c8b8093e98aa85d2c5414a6559cfe Mon Sep 17 00:00:00 2001 From: Mia Altieri Date: Mon, 9 Oct 2023 15:18:26 +0000 Subject: [PATCH] reformat --- lib/charms/mongodb/v0/helpers.py | 48 +---------------------- lib/charms/mongodb/v0/mongodb_provider.py | 22 ++++++----- src/charm.py | 24 +++++++++++- 3 files changed, 35 insertions(+), 59 deletions(-) diff --git a/lib/charms/mongodb/v0/helpers.py b/lib/charms/mongodb/v0/helpers.py index 3fb9bc40a..b7aa6f1b5 100644 --- a/lib/charms/mongodb/v0/helpers.py +++ b/lib/charms/mongodb/v0/helpers.py @@ -10,8 +10,6 @@ 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, @@ -31,7 +29,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 9 +LIBPATCH = 8 # path to store mongodb ketFile KEY_FILE = "keyFile" @@ -51,50 +49,6 @@ 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.info( - "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 63c7673ac..b84419be2 100644 --- a/lib/charms/mongodb/v0/mongodb_provider.py +++ b/lib/charms/mongodb/v0/mongodb_provider.py @@ -84,10 +84,13 @@ 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).""" - 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." - ) + # relation departed and relation broken events occur during scaling down or during relation + # removal, only relation departed events have access to metadata to determine which case. + self.charm.set_scaling_down(event) + + # check if were scaling down and add a log message + if self.charm.is_scaling_down(event.relation.id): + logger.info("Scaling down the application, no need to remove external users.") def _on_relation_event(self, event): """Handle relation joined events. @@ -121,18 +124,17 @@ def _on_relation_event(self, event): departed_relation_id = None if type(event) is RelationBrokenEvent: departed_relation_id = event.relation.id - # we receives relation broken 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. - relation_departed_key = f"relation_{event.relation.id}_departed" - if relation_departed_key not in self.charm.app_peer_data: + + # Only relation_deparated events can check if scaling down + if not self.charm.has_departed_run(departed_relation_id): logger.info( "Deferring, must wait for relation departed hook to decide if relation should be removed." ) event.defer() return - if not json.loads(self.charm.app_peer_data[relation_departed_key]): + # check if were scaling down and add a log message + if self.charm.is_scaling_down(event.relation.id): logger.info( "Relation broken event occurring due to scale down, do not proceed to remove users." ) diff --git a/src/charm.py b/src/charm.py index 59ef94959..5e209d42e 100755 --- a/src/charm.py +++ b/src/charm.py @@ -19,7 +19,6 @@ TLS_EXT_PEM_FILE, TLS_INT_CA_FILE, TLS_INT_PEM_FILE, - MongoDBHelper, build_unit_status, copy_licenses_to_unit, generate_keyfile, @@ -124,7 +123,6 @@ 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) @@ -1390,6 +1388,28 @@ def _juju_secret_remove(self, scope: Scopes, key: str) -> None: secret.set_content(secret_cache) logging.debug(f"Secret {scope}:{key}") + def is_scaling_down(self, rel_id: int) -> bool: + """Returns True if the application is scaling down.""" + rel_departed_key = self._generate_relation_departed_key(rel_id) + return json.loads(self.unit_peer_data[rel_departed_key]) + + def has_departed_run(self, rel_id: int) -> bool: + """Returns True if the relation departed event has run.""" + rel_departed_key = self._generate_relation_departed_key(rel_id) + return rel_departed_key in self.unit_peer_data + + def set_scaling_down(self, event: RelationDepartedEvent) -> None: + """Sets whether or not the current unit is scaling down.""" + # check if relation departed is due to current unit being removed. (i.e. scaling down the + # application.) + rel_departed_key = self._generate_relation_departed_key(event.relation.id) + self.unit_peer_data[rel_departed_key] = json.dumps(event.departing_unit == self.unit) + + @staticmethod + def _generate_relation_departed_key(rel_id: int) -> str: + """Generates the relation departed key for a specified relation id.""" + return f"relation_{rel_id}_departed" + # END: helper functions