Skip to content

Commit

Permalink
refactor: split relation in peer relations and other relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu1nness committed Dec 4, 2024
1 parent 54b6bd5 commit 602ef4c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
schedule:
- cron: "53 0 * * *" # Daily at 00:53 UTC
workflow_dispatch:
workflow_call:

jobs:
pre-commit:
Expand Down
3 changes: 2 additions & 1 deletion single_kernel_mongo/abstract_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ops.charm import CharmBase

from single_kernel_mongo.config.literals import Substrates
from single_kernel_mongo.config.relations import PeerRelationNames
from single_kernel_mongo.core.operator import OperatorProtocol
from single_kernel_mongo.core.structured_config import MongoConfigModel
from single_kernel_mongo.events.lifecycle import LifecycleEventsHandler
Expand Down Expand Up @@ -35,7 +36,7 @@ class AbstractMongoCharm(Generic[T, U], CharmBase):
config_type: type[T]
operator_type: type[U]
substrate: ClassVar[Substrates]
peer_rel_name: ClassVar[str]
peer_rel_name: ClassVar[PeerRelationNames]
name: ClassVar[str]

def __init__(self, *args):
Expand Down
9 changes: 7 additions & 2 deletions single_kernel_mongo/config/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
from enum import Enum


class PeerRelationNames(str, Enum):
"""The peer relation names."""

PEERS = "database-peers" # check.
ROUTER_PEERS = "router-peers"


class RelationNames(str, Enum):
"""The different relations."""

DATABASE = "database" # In progress
PEERS = "database-peers" # check.
ROUTER_PEERS = "router-peers"
SHARDING = "sharding"
CONFIG_SERVER = "config-server"
CLUSTER = "cluster"
Expand Down
15 changes: 11 additions & 4 deletions single_kernel_mongo/events/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from ops.framework import Object

from single_kernel_mongo.config.literals import Substrates
from single_kernel_mongo.config.relations import PeerRelationNames
from single_kernel_mongo.core.operator import OperatorProtocol
from single_kernel_mongo.exceptions import (
ContainerNotReadyError,
Expand All @@ -42,7 +43,7 @@ class LifecycleEventsHandler(Object):
In charge of handling the lifecycle events such as install, start, pebble ready, etc.
"""

def __init__(self, dependent: OperatorProtocol, rel_name: str):
def __init__(self, dependent: OperatorProtocol, rel_name: PeerRelationNames):
super().__init__(parent=dependent, key=dependent.name)
self.dependent = dependent
self.charm = dependent.charm
Expand All @@ -60,9 +61,15 @@ def __init__(self, dependent: OperatorProtocol, rel_name: str):
self.framework.observe(getattr(self.charm.on, "update_status"), self.on_update_status)
self.framework.observe(getattr(self.charm.on, "secret_changed"), self.on_secret_changed)

self.framework.observe(self.charm.on[rel_name].relation_joined, self.on_relation_joined)
self.framework.observe(self.charm.on[rel_name].relation_changed, self.on_relation_changed)
self.framework.observe(self.charm.on[rel_name].relation_departed, self.on_relation_departed)
self.framework.observe(
self.charm.on[rel_name.value].relation_joined, self.on_relation_joined
)
self.framework.observe(
self.charm.on[rel_name.value].relation_changed, self.on_relation_changed
)
self.framework.observe(
self.charm.on[rel_name.value].relation_departed, self.on_relation_departed
)

self.framework.observe(
getattr(self.charm.on, "mongodb_storage_attached"), self.on_storage_attached
Expand Down
9 changes: 5 additions & 4 deletions single_kernel_mongo/state/charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from single_kernel_mongo.config.models import Role
from single_kernel_mongo.config.relations import (
ExternalRequirerRelations,
PeerRelationNames,
RelationNames,
)
from single_kernel_mongo.core.secrets import SecretCache
Expand Down Expand Up @@ -86,11 +87,11 @@ def __init__(self, charm: AbstractMongoCharm[T, U], substrate: Substrates, charm

self.peer_app_interface = DataPeerData(
self.model,
relation_name=RelationNames.PEERS.value,
relation_name=PeerRelationNames.PEERS.value,
)
self.peer_unit_interface = DataPeerUnitData(
self.model,
relation_name=RelationNames.PEERS.value,
relation_name=PeerRelationNames.PEERS.value,
additional_secret_fields=SECRETS_UNIT,
)

Expand All @@ -99,7 +100,7 @@ def __init__(self, charm: AbstractMongoCharm[T, U], substrate: Substrates, charm
@property
def peer_relation(self) -> Relation | None:
"""The replica set peer relation."""
return self.model.get_relation(RelationNames.PEERS.value)
return self.model.get_relation(PeerRelationNames.PEERS.value)

@property
def peers_units(self) -> set[Unit]:
Expand Down Expand Up @@ -260,7 +261,7 @@ def peer_units_data_interfaces(self) -> dict[Unit, DataPeerOtherUnitData]:
"""The cluster peer relation."""
return {
unit: DataPeerOtherUnitData(
model=self.model, unit=unit, relation_name=RelationNames.PEERS.value
model=self.model, unit=unit, relation_name=PeerRelationNames.PEERS.value
)
for unit in self.peers_units
}
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/mongodb_test_charm/src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from single_kernel_mongo.abstract_charm import AbstractMongoCharm
from single_kernel_mongo.config.literals import Substrates
from single_kernel_mongo.config.relations import RelationNames
from single_kernel_mongo.config.relations import PeerRelationNames
from single_kernel_mongo.core.structured_config import MongoDBCharmConfig
from single_kernel_mongo.managers.mongodb_operator import MongoDBOperator

Expand All @@ -16,7 +16,7 @@ class MongoTestCharm(AbstractMongoCharm[MongoDBCharmConfig, MongoDBOperator]):
config_type = MongoDBCharmConfig
operator_type = MongoDBOperator
substrate = Substrates.VM
peer_rel_name = RelationNames.PEERS.value
peer_rel_name = PeerRelationNames.PEERS
name = "mongodb-test"


Expand Down
12 changes: 6 additions & 6 deletions tests/unit/test_charm_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ops.testing import Harness

from single_kernel_mongo.config.literals import Scope
from single_kernel_mongo.config.relations import RelationNames
from single_kernel_mongo.config.relations import PeerRelationNames
from single_kernel_mongo.core.structured_config import MongoDBRoles
from single_kernel_mongo.utils.mongodb_users import BackupUser, MonitorUser, OperatorUser

Expand All @@ -14,7 +14,7 @@

@patch_network_get(private_address="1.1.1.1")
def test_app_hosts(harness: Harness[MongoTestCharm]):
rel_id = harness.charm.model.get_relation(RelationNames.PEERS.value).id # type: ignore
rel_id = harness.charm.model.get_relation(PeerRelationNames.PEERS.value).id # type: ignore
harness.add_relation_unit(rel_id, "test-mongodb/1")
harness.update_relation_data(rel_id, "test-mongodb/1", PEER_ADDR)
resulting_ips = harness.charm.operator.state.app_hosts
Expand All @@ -29,14 +29,14 @@ def test_config(harness: Harness[MongoTestCharm]):


def test_peer_units(harness: Harness[MongoTestCharm]):
rel = harness.charm.model.get_relation(RelationNames.PEERS.value)
rel = harness.charm.model.get_relation(PeerRelationNames.PEERS.value)
harness.add_relation_unit(rel.id, "test-mongodb/1") # type: ignore
assert harness.charm.operator.state.peer_relation.id == rel.id # type: ignore
assert {unit.name for unit in harness.charm.operator.state.peers_units} == {"test-mongodb/1"}


def test_users_secrets(harness: Harness[MongoTestCharm]):
rel = harness.charm.model.get_relation(RelationNames.PEERS.value)
rel = harness.charm.model.get_relation(PeerRelationNames.PEERS.value)
harness.add_relation_unit(rel.id, "test-mongodb/1") # type: ignore

harness.set_leader(True)
Expand All @@ -51,7 +51,7 @@ def test_users_secrets(harness: Harness[MongoTestCharm]):


def test_app_peer_data(harness: Harness[MongoTestCharm]):
rel = harness.charm.model.get_relation(RelationNames.PEERS.value)
rel = harness.charm.model.get_relation(PeerRelationNames.PEERS.value)
harness.add_relation_unit(rel.id, "test-mongodb/1") # type: ignore
harness.set_leader(True)
state = harness.charm.operator.state
Expand Down Expand Up @@ -83,7 +83,7 @@ def test_app_peer_data(harness: Harness[MongoTestCharm]):

@patch_network_get(private_address="1.1.1.1")
def test_unit_peer_data(harness: Harness[MongoTestCharm]):
rel = harness.charm.model.get_relation(RelationNames.PEERS.value)
rel = harness.charm.model.get_relation(PeerRelationNames.PEERS.value)
harness.add_relation_unit(rel.id, "test-mongodb/1") # type: ignore
harness.set_leader(True)
state = harness.charm.operator.state
Expand Down

0 comments on commit 602ef4c

Please sign in to comment.