Skip to content

Commit

Permalink
fmt + lint
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Oct 9, 2023
1 parent 61724e3 commit 31264d4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
48 changes: 47 additions & 1 deletion lib/charms/mongodb/v0/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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.
Expand Down
27 changes: 5 additions & 22 deletions lib/charms/mongodb/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 31264d4

Please sign in to comment.