Skip to content

Commit

Permalink
move departed check to charm
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Oct 10, 2023
1 parent 231bb0e commit b0a0fc4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 3 additions & 13 deletions lib/charms/mongodb/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def __init__(self, charm: CharmBase, substrate="k8s", relation_name: str = "data
"""
self.relation_name = relation_name
self.substrate = substrate
self.charm = charm

super().__init__(charm, self.relation_name)
self.framework.observe(
charm.on[self.relation_name].relation_departed, self._on_relation_departed
charm.on[self.relation_name].relation_departed,
self.charm.check_relation_broken_or_scale_down,
)
self.framework.observe(
charm.on[self.relation_name].relation_broken, self._on_relation_event
Expand All @@ -74,24 +76,12 @@ def __init__(self, charm: CharmBase, substrate="k8s", relation_name: str = "data
charm.on[self.relation_name].relation_changed, self._on_relation_event
)

self.charm = charm

# Charm events defined in the database provides charm library.
self.database_provides = DatabaseProvides(self.charm, relation_name=self.relation_name)
self.framework.observe(
self.database_provides.on.database_requested, self._on_relation_event
)

def _on_relation_departed(self, event):
"""Checks if users should be removed on the following event (relation-broken)."""
# 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
13 changes: 13 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,19 @@ def _juju_secret_remove(self, scope: Scopes, key: str) -> None:
secret.set_content(secret_cache)
logging.debug(f"Secret {scope}:{key}")

def check_relation_broken_or_scale_down(self, event: RelationDepartedEvent) -> None:
"""Checks relation departed event is the result of removed relation or scale down.
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.set_scaling_down(event)

if self.is_scaling_down(event.relation.id):
logger.info(
"Scaling down the application, no need to process removed relation in broken hook."
)

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)
Expand Down

0 comments on commit b0a0fc4

Please sign in to comment.