diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index eddd1329..7fd70f1f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,6 +9,7 @@ on: schedule: - cron: "53 0 * * *" # Daily at 00:53 UTC workflow_dispatch: + workflow_call: jobs: pre-commit: diff --git a/single_kernel_mongo/abstract_charm.py b/single_kernel_mongo/abstract_charm.py index 789cee7a..e278ea8d 100644 --- a/single_kernel_mongo/abstract_charm.py +++ b/single_kernel_mongo/abstract_charm.py @@ -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 @@ -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): diff --git a/single_kernel_mongo/config/relations.py b/single_kernel_mongo/config/relations.py index c502f7bd..5d98ba3c 100644 --- a/single_kernel_mongo/config/relations.py +++ b/single_kernel_mongo/config/relations.py @@ -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" diff --git a/single_kernel_mongo/events/lifecycle.py b/single_kernel_mongo/events/lifecycle.py index df27329a..9920efb9 100644 --- a/single_kernel_mongo/events/lifecycle.py +++ b/single_kernel_mongo/events/lifecycle.py @@ -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, @@ -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 @@ -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 diff --git a/single_kernel_mongo/state/charm_state.py b/single_kernel_mongo/state/charm_state.py index e44514d8..00b330c7 100644 --- a/single_kernel_mongo/state/charm_state.py +++ b/single_kernel_mongo/state/charm_state.py @@ -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 @@ -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, ) @@ -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]: @@ -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 } diff --git a/tests/unit/mongodb_test_charm/src/charm.py b/tests/unit/mongodb_test_charm/src/charm.py index 01829538..4214fb41 100755 --- a/tests/unit/mongodb_test_charm/src/charm.py +++ b/tests/unit/mongodb_test_charm/src/charm.py @@ -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 @@ -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" diff --git a/tests/unit/test_charm_state.py b/tests/unit/test_charm_state.py index 62be7c93..7756abf5 100644 --- a/tests/unit/test_charm_state.py +++ b/tests/unit/test_charm_state.py @@ -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 @@ -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 @@ -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) @@ -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 @@ -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