Skip to content

Commit

Permalink
Add method in the smtp library to retrieve the relation data directly (
Browse files Browse the repository at this point in the history
  • Loading branch information
arturo-seijas authored Nov 2, 2023
1 parent a474e19 commit 16b057c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
47 changes: 34 additions & 13 deletions lib/charms/smtp_integrator/v0/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, *args):

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 2
LIBPATCH = 3

# pylint: disable=wrong-import-position
import logging
Expand Down Expand Up @@ -244,6 +244,38 @@ def __init__(self, charm: ops.CharmBase, relation_name: str = DEFAULT_RELATION_N
self.relation_name = relation_name
self.framework.observe(charm.on[relation_name].relation_changed, self._on_relation_changed)

def get_relation_data(self) -> Optional[SmtpRelationData]:
"""Retrieve the relation data.
Returns:
SmtpRelationData: the relation data.
"""
relation = self.model.get_relation(self.relation_name)
assert relation
return self._get_relation_data_from_relation(relation) if relation else None

def _get_relation_data_from_relation(self, relation: ops.Relation) -> SmtpRelationData:
"""Retrieve the relation data.
Args:
relation: the relation to retrieve the data from.
Returns:
SmtpRelationData: the relation data.
"""
assert relation.app
relation_data = relation.data[relation.app]
return SmtpRelationData(
host=relation_data.get("host"),
port=relation_data.get("port"),
user=relation_data.get("user"),
password=relation_data.get("password"),
password_id=relation_data.get("password_id"),
auth_type=relation_data.get("auth_type"),
transport_security=relation_data.get("transport_security"),
domain=relation_data.get("domain"),
)

def _is_relation_data_valid(self, relation: ops.Relation) -> bool:
"""Validate the relation data.
Expand All @@ -254,18 +286,7 @@ def _is_relation_data_valid(self, relation: ops.Relation) -> bool:
true: if the relation data is valid.
"""
try:
assert relation.app
relation_data = relation.data[relation.app]
_ = SmtpRelationData(
host=relation_data.get("host"),
port=relation_data.get("port"),
user=relation_data.get("user"),
password=relation_data.get("password"),
password_id=relation_data.get("password_id"),
auth_type=relation_data.get("auth_type"),
transport_security=relation_data.get("transport_security"),
domain=relation_data.get("domain"),
)
_ = self._get_relation_data_from_relation(relation)
return True
except ValidationError:
return False
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/test_library_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ def test_legacy_requirer_charm_with_valid_relation_data_emits_event(is_leader):
assert harness.charm.events[0].transport_security == relation_data["transport_security"]
assert harness.charm.events[0].domain == relation_data["domain"]

retrieved_relation_data = harness.charm.smtp_legacy.get_relation_data()
assert retrieved_relation_data.host == relation_data["host"]
assert retrieved_relation_data.port == int(relation_data["port"])
assert retrieved_relation_data.user == relation_data["user"]
assert retrieved_relation_data.password == relation_data["password"]
assert retrieved_relation_data.auth_type == relation_data["auth_type"]
assert retrieved_relation_data.transport_security == relation_data["transport_security"]
assert retrieved_relation_data.domain == relation_data["domain"]


@pytest.mark.parametrize("is_leader", [True, False])
def test_requirer_charm_with_valid_relation_data_emits_event(is_leader):
Expand Down

0 comments on commit 16b057c

Please sign in to comment.