Skip to content

Commit

Permalink
make linters happy (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
delgod authored Sep 19, 2022
1 parent 16b547b commit a828e63
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/charms/mongodb_libs/v0/mongodb_provider.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright 2022 Canonical Ltd.
# See LICENSE file for licensing details.

"""In this class we manage client database relations.
"""In this class, we manage client database relations.
This class creates user and database for each application relation
This class creates a user and database for each application relation
and expose needed information for client connection via fields in
external relation.
"""
Expand Down Expand Up @@ -50,13 +50,13 @@
Diff = namedtuple("Diff", "added changed deleted")
Diff.__doc__ = """
A tuple for storing the diff between two data mappings.
added - keys that were added
changed - keys that still exist but have new values
deleted - key that were deleted"""
added keys that were added
changed keys that still exist but have new values
deleted key that were deleted."""


class MongoDBProvider(Object):
"""In this class we manage client database relations."""
"""In this class, we manage client database relations."""

def __init__(self, charm, substrate="k8s"):
"""Manager of MongoDB client relations."""
Expand All @@ -72,7 +72,7 @@ def _on_relation_event(self, event):
When relations join, change, or depart, the :class:`MongoDBClientRelation`
creates or drops MongoDB users and sets credentials into relation
data. As result, related charm gets credentials for accessing the
data. As a result, related charm gets credentials for accessing the
MongoDB database.
"""
if not self.charm.unit.is_leader():
Expand Down Expand Up @@ -132,7 +132,7 @@ def oversee_users(self, departed_relation_id: Optional[int], event):
event: relation event.
When the function is executed in relation departed event, the departed
relation is still in the list of all relations. Therefore, for proper
relation is still on the list of all relations. Therefore, for proper
work of the function, we need to exclude departed relation from the list.
"""
with MongoDBConnection(self.charm.mongodb_config) as mongo:
Expand All @@ -146,7 +146,7 @@ def oversee_users(self, departed_relation_id: Optional[int], event):
for username in relation_users - database_users:
config = self._get_config(username, None)
if config.database is None:
# We need to wait for moment when provider library
# We need to wait for the moment when the provider library
# set the database name into the relation.
continue
logger.info("Create relation user: %s on %s", config.username, config.database)
Expand Down Expand Up @@ -179,7 +179,7 @@ def _diff(self, event: RelationChangedEvent) -> Diff:
a Diff instance containing the added, deleted and changed
keys from the event relation databag.
"""
# TODO import marcelos unit tests in a future PR
# TODO import marvelous unit tests in a future PR
# Retrieve the old data from the data key in the application relation databag.
old_data = json.loads(event.relation.data[self.charm.model.app].get("data", "{}"))
# Retrieve the new data from the event relation databag.
Expand All @@ -191,7 +191,7 @@ def _diff(self, event: RelationChangedEvent) -> Diff:
added = new_data.keys() - old_data.keys()
# These are the keys that were removed from the databag and triggered this event.
deleted = old_data.keys() - new_data.keys()
# These are the keys that already existed in the databag,
# These are the keys that already existed in the databag
# but had their values changed.
changed = {
key for key in old_data.keys() & new_data.keys() if old_data[key] != new_data[key]
Expand All @@ -205,8 +205,8 @@ def _diff(self, event: RelationChangedEvent) -> Diff:
# Return the diff with all possible changes.
return Diff(added, changed, deleted)

def _get_config(self, username: str, password: str) -> MongoDBConfiguration:
"""Construct config object for future user creation."""
def _get_config(self, username: str, password: Optional[str]) -> MongoDBConfiguration:
"""Construct the config object for future user creation."""
relation = self._get_relation_from_username(username)
if not password:
password = generate_password()
Expand Down Expand Up @@ -236,7 +236,7 @@ def _set_relation(self, config: MongoDBConfiguration):
relation.data[self.charm.app].update(data)

@staticmethod
def _get_username_from_relation_id(relation_id: str) -> str:
def _get_username_from_relation_id(relation_id: int) -> str:
"""Construct username."""
return f"relation-{relation_id}"

Expand Down Expand Up @@ -274,20 +274,22 @@ def _get_relation_from_username(self, username: str) -> Relation:
match = re.match(r"^relation-(\d+)$", username)
# We generated username in `_get_users_from_relations`
# func and passed it into this function later.
# It means the username here MUST match to regex.
# It means the username here MUST match regex.
assert match is not None, "No relation match"
relation_id = int(match.group(1))
logger.debug("Relation ID: %s", relation_id)
return self.model.get_relation(REL_NAME, relation_id)

def _get_database_from_relation(self, relation: Relation) -> Optional[str]:
@staticmethod
def _get_database_from_relation(relation: Relation) -> Optional[str]:
"""Return database name from relation."""
database = relation.data[relation.app].get("database", None)
if database is not None:
return database
return None

def _get_roles_from_relation(self, relation: Relation) -> Set[str]:
@staticmethod
def _get_roles_from_relation(relation: Relation) -> Set[str]:
"""Return additional user roles from relation if specified or return None."""
roles = relation.data[relation.app].get("extra-user-roles", None)
if roles is not None:
Expand Down

0 comments on commit a828e63

Please sign in to comment.