Skip to content

Commit

Permalink
[DPE-2145] Refactoring: refactor charm events handlers (#166)
Browse files Browse the repository at this point in the history
* Reafactoring: charm events

* Refactoring charm event handlers

---------

Co-authored-by: Ubuntu <[email protected]>
  • Loading branch information
dmitry-ratushnyy and Ubuntu authored Jun 23, 2023
1 parent 8e28834 commit 41a167f
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 167 deletions.
4 changes: 2 additions & 2 deletions lib/charms/mongodb/v0/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import re
from dataclasses import dataclass
from typing import Dict, List, Optional, Set
from typing import Dict, List, Set
from urllib.parse import quote_plus

from bson.json_util import dumps
Expand Down Expand Up @@ -49,7 +49,7 @@ class MongoDBConfiguration:
"""

replset: str
database: Optional[str]
database: str
username: str
password: str
hosts: Set[str]
Expand Down
34 changes: 12 additions & 22 deletions lib/charms/mongodb/v0/mongodb_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
class MongoDBTLS(Object):
"""In this class we manage client database relations."""

def __init__(self, charm, peer_relation, substrate="k8s"):
def __init__(self, charm, peer_relation, substrate):
"""Manager of MongoDB client relations."""
super().__init__(charm, "client-relations")
self.charm = charm
Expand Down Expand Up @@ -139,13 +139,11 @@ def _on_tls_relation_broken(self, event: RelationBrokenEvent) -> None:
event.defer()
return

logger.debug("Restarting mongod with TLS disabled.")
if self.substrate == "vm":
self.charm.unit.status = MaintenanceStatus("disabling TLS")
self.charm.restart_mongod_service()
self.charm.unit.status = ActiveStatus()
else:
self.charm.on_mongod_pebble_ready(event)
logger.info("Restarting mongod with TLS disabled.")
self.charm.unit.status = MaintenanceStatus("disabling TLS")
self.charm.delete_tls_certificate_from_workload()
self.charm.restart_mongod_service()
self.charm.unit.status = ActiveStatus()

def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:
"""Enable TLS when TLS certificate available."""
Expand All @@ -165,9 +163,6 @@ def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:
logger.error("An unknown certificate available.")
return

old_cert = self.charm.get_secret(scope, "cert")
renewal = old_cert and old_cert != event.certificate

if scope == "unit" or (scope == "app" and self.charm.unit.is_leader()):
self.charm.set_secret(
scope, "chain", "\n".join(event.chain) if event.chain is not None else None
Expand All @@ -182,17 +177,12 @@ def _on_certificate_available(self, event: CertificateAvailableEvent) -> None:
event.defer()
return

if renewal and self.substrate == "k8s":
self.charm.unit.get_container("mongod").stop("mongod")

logger.debug("Restarting mongod with TLS enabled.")
if self.substrate == "vm":
self.charm._push_tls_certificate_to_workload()
self.charm.unit.status = MaintenanceStatus("enabling TLS")
self.charm.restart_mongod_service()
self.charm.unit.status = ActiveStatus()
else:
self.charm.on_mongod_pebble_ready(event)

self.charm.push_tls_certificate_to_workload()
self.charm.unit.status = MaintenanceStatus("enabling TLS")
self.charm.restart_mongod_service()
self.charm.unit.status = ActiveStatus()

def _waiting_for_certs(self):
"""Returns a boolean indicating whether additional certs are needed."""
Expand Down Expand Up @@ -276,4 +266,4 @@ def get_host(self, unit: Unit):
if self.substrate == "vm":
return self.charm._unit_ip(unit)
else:
return self.charm.get_hostname_by_unit(unit.name)
return self.charm.get_hostname_for_unit(unit)
4 changes: 2 additions & 2 deletions metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ containers:
mongod:
resource: mongodb-image
mounts:
- storage: db
- storage: mongodb
location: /var/lib/mongodb
resources:
mongodb-image:
type: oci-image
description: OCI image for mongodb
upstream-source: "ghcr.io/canonical/charmed-mongodb:5.0.14-22.04_edge"
storage:
db:
mongodb:
type: filesystem
location: /var/lib/mongodb
Loading

0 comments on commit 41a167f

Please sign in to comment.