Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DPE-6555] Add read only URIs #206

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent):

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

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -960,6 +960,7 @@ class Data(ABC):
"username": SECRET_GROUPS.USER,
"password": SECRET_GROUPS.USER,
"uris": SECRET_GROUPS.USER,
"read-only-uris": SECRET_GROUPS.USER,
"tls": SECRET_GROUPS.TLS,
"tls-ca": SECRET_GROUPS.TLS,
}
Expand Down Expand Up @@ -1700,7 +1701,7 @@ def set_tls_ca(self, relation_id: int, tls_ca: str) -> None:
class RequirerData(Data):
"""Requirer-side of the relation."""

SECRET_FIELDS = ["username", "password", "tls", "tls-ca", "uris"]
SECRET_FIELDS = ["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]

def __init__(
self,
Expand Down Expand Up @@ -2749,6 +2750,19 @@ def uris(self) -> Optional[str]:

return self.relation.data[self.relation.app].get("uris")

@property
def read_only_uris(self) -> Optional[str]:
"""Returns the readonly connection URIs."""
if not self.relation.app:
return None

if self.secrets_enabled:
secret = self._get_secret("user")
if secret:
return secret.get("read-only-uris")

return self.relation.data[self.relation.app].get("read-only-uris")

@property
def version(self) -> Optional[str]:
"""Returns the version of the database.
Expand Down Expand Up @@ -2855,6 +2869,15 @@ def set_uris(self, relation_id: int, uris: str) -> None:
"""
self.update_relation_data(relation_id, {"uris": uris})

def set_read_only_uris(self, relation_id: int, uris: str) -> None:
"""Set the database readonly connection URIs in the application relation databag.

Args:
relation_id: the identifier for a particular relation.
uris: connection URIs.
"""
self.update_relation_data(relation_id, {"read-only-uris": uris})

def set_version(self, relation_id: int, version: str) -> None:
"""Set the database version in the application relation databag.

Expand Down
18 changes: 10 additions & 8 deletions tests/unit/test_data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def _on_index_requested(self, _) -> None:


class DataProvidesBaseTests(ABC):
SECRET_FIELDS = ["username", "password", "tls", "tls-ca", "uris"]
SECRET_FIELDS = ["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]
DATABASE_FIELD = "database"

@pytest.fixture
Expand Down Expand Up @@ -804,7 +804,7 @@ def test_provider_interface_functions_secrets(self):
rel_data = interface.fetch_relation_data()
assert rel_data == {
0: {
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris"]',
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]',
self.DATABASE_FIELD: DATABASE,
}
}
Expand All @@ -813,7 +813,7 @@ def test_provider_interface_functions_secrets(self):
0: {
"data": json.dumps(
{
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris"]',
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]',
self.DATABASE_FIELD: DATABASE,
}
)
Expand Down Expand Up @@ -852,11 +852,11 @@ def test_provider_interface_dict_secrets(self):
assert datadict == {
"data": json.dumps(
{
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris"]',
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]',
self.DATABASE_FIELD: DATABASE,
}
),
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris"]',
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]',
self.DATABASE_FIELD: DATABASE,
}

Expand All @@ -865,7 +865,7 @@ def test_provider_interface_dict_secrets(self):
with self._caplog.at_level(logging.ERROR):
assert (
datadict["requested-secrets"]
== '["username", "password", "tls", "tls-ca", "uris"]'
== '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]'
)
assert (
"This operation (fetch_my_relation_field()) can only be performed by the leader unit"
Expand Down Expand Up @@ -946,6 +946,7 @@ def test_set_additional_fields(self):
self.harness.charm.provider.set_tls(self.rel_id, "True")
self.harness.charm.provider.set_tls_ca(self.rel_id, "Canonical")
self.harness.charm.provider.set_uris(self.rel_id, "host1:port,host2:port")
self.harness.charm.provider.set_read_only_uris(self.rel_id, "host2:port")
self.harness.charm.provider.set_version(self.rel_id, "1.0")

# Check that the additional fields are present in the relation.
Expand All @@ -957,6 +958,7 @@ def test_set_additional_fields(self):
"tls": "True",
"tls-ca": "Canonical",
"uris": "host1:port,host2:port",
"read-only-uris": "host2:port",
"version": "1.0",
}

Expand Down Expand Up @@ -1963,7 +1965,7 @@ def test_requires_interface_functions_secrets(self):
"alias": "cluster1",
"database": "data_platform",
"extra-user-roles": "CREATEDB,CREATEROLE",
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris"]',
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]',
}
}

Expand Down Expand Up @@ -1991,7 +1993,7 @@ def test_requires_interface_dict_secrets(self):
"alias": "cluster1",
"database": "data_platform",
"extra-user-roles": "CREATEDB,CREATEROLE",
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris"]',
"requested-secrets": '["username", "password", "tls", "tls-ca", "uris", "read-only-uris"]',
}

# Non-leader can try to fetch data (won't have anything thought, as only app data is there...
Expand Down