Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Oct 10, 2023
1 parent 5273f8e commit 08e6b60
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 59 deletions.
48 changes: 1 addition & 47 deletions lib/charms/mongodb/v0/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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.
Expand Down
22 changes: 12 additions & 10 deletions lib/charms/mongodb/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."
)
Expand Down
24 changes: 22 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand Down

0 comments on commit 08e6b60

Please sign in to comment.