Skip to content

Commit

Permalink
Use relation_created instead of relation_joined
Browse files Browse the repository at this point in the history
The handler is supposed to run when the relation is created, not when a unit joins the relation

See canonical/mysql-router-k8s-operator#101
  • Loading branch information
carlcsaposs-canonical committed Mar 15, 2024
1 parent e7acbdd commit ba000de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/charms/opensearch/v0/opensearch_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
generate_csr,
generate_private_key,
)
from ops.charm import ActionEvent, RelationBrokenEvent, RelationJoinedEvent
from ops.charm import ActionEvent, RelationBrokenEvent, RelationCreatedEvent
from ops.framework import Object

# The unique Charmhub library identifier, never change it
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(self, charm, peer_relation: str):
)

self.framework.observe(
self.charm.on[TLS_RELATION].relation_joined, self._on_tls_relation_joined
self.charm.on[TLS_RELATION].relation_created, self._on_tls_relation_created
)
self.framework.observe(
self.charm.on[TLS_RELATION].relation_broken, self._on_tls_relation_broken
Expand Down Expand Up @@ -98,8 +98,8 @@ def request_new_unit_certificates(self) -> None:
secrets = self.charm.secrets.get_object(Scope.UNIT, cert_type.val)
self._request_certificate_renewal(Scope.UNIT, cert_type, secrets)

def _on_tls_relation_joined(self, _: RelationJoinedEvent) -> None:
"""Request certificate when TLS relation joined."""
def _on_tls_relation_created(self, _: RelationCreatedEvent) -> None:
"""Request certificate when TLS relation created."""
admin_cert = self.charm.secrets.get_object(Scope.APP, CertType.APP_ADMIN.val)
if self.charm.unit.is_leader() and admin_cert is None:
self._request_certificate(Scope.APP, CertType.APP_ADMIN)
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/lib/test_opensearch_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ def test_find_secret(self):
@patch("charms.opensearch.v0.opensearch_tls.OpenSearchTLS._request_certificate")
@patch("charm.OpenSearchOperatorCharm._put_admin_user")
@patch("charm.OpenSearchOperatorCharm._purge_users")
def test_on_relation_joined_admin(self, _, _put_admin_user, _request_certificate):
"""Test on certificate relation joined event."""
def test_on_relation_created_admin(self, _, _put_admin_user, _request_certificate):
"""Test on certificate relation created event."""
event_mock = MagicMock()

self.harness.set_leader(is_leader=True)
self.charm.tls._on_tls_relation_joined(event_mock)
self.charm.tls._on_tls_relation_created(event_mock)
self.assertEqual(
_request_certificate.mock_calls,
[
Expand All @@ -109,12 +109,12 @@ def test_on_relation_joined_admin(self, _, _put_admin_user, _request_certificate
@patch("charms.opensearch.v0.opensearch_tls.OpenSearchTLS._request_certificate")
@patch("charm.OpenSearchOperatorCharm._put_admin_user")
@patch("charm.OpenSearchOperatorCharm._purge_users")
def test_on_relation_joined_non_admin(self, _, _put_admin_user, _request_certificate):
"""Test on certificate relation joined event."""
def test_on_relation_created_non_admin(self, _, _put_admin_user, _request_certificate):
"""Test on certificate relation created event."""
event_mock = MagicMock()

self.harness.set_leader(is_leader=False)
self.charm.tls._on_tls_relation_joined(event_mock)
self.charm.tls._on_tls_relation_created(event_mock)
self.assertEqual(
_request_certificate.mock_calls,
[
Expand Down

0 comments on commit ba000de

Please sign in to comment.