From 718b96ba429616534897b071a8a29b84fb6a0196 Mon Sep 17 00:00:00 2001 From: Paulo Machado Date: Sat, 27 Apr 2024 21:54:48 -0300 Subject: [PATCH] first pass --- .../data_platform_libs/v0/data_interfaces.py | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/charms/data_platform_libs/v0/data_interfaces.py b/lib/charms/data_platform_libs/v0/data_interfaces.py index d46770f0..18d32af1 100644 --- a/lib/charms/data_platform_libs/v0/data_interfaces.py +++ b/lib/charms/data_platform_libs/v0/data_interfaces.py @@ -307,6 +307,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent): Optional, Set, Tuple, + TypedDict, Union, ValuesView, ) @@ -331,7 +332,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 = 34 +LIBPATCH = 35 PYDEPS = ["ops>=2.0.0"] @@ -1306,6 +1307,8 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None: raise NotImplementedError +HostnameMapping = TypedDict("HostnameMapping", {"address": str, "names": List[str]}) + # Base ProviderData and RequiresData @@ -2277,7 +2280,7 @@ def __init__( ################################################################################ -# Cross-charm Relatoins Data Handling and Evenets +# Cross-charm Relations Data Handling and Events ################################################################################ # Generic events @@ -2300,7 +2303,7 @@ class RelationEventWithSecret(RelationEvent): @property def _secrets(self) -> dict: - """Caching secrets to avoid fetching them each time a field is referrd. + """Caching secrets to avoid fetching them each time a field is referred. DON'T USE the encapsulated helper variable outside of this function """ @@ -2309,7 +2312,7 @@ def _secrets(self) -> dict: return self._cached_secrets def _get_secret(self, group) -> Optional[Dict[str, str]]: - """Retrieveing secrets.""" + """Retrieving secrets.""" if not self.app: return if not self._secrets.get(group): @@ -2498,6 +2501,18 @@ def version(self) -> Optional[str]: return self.relation.data[self.relation.app].get("version") + @property + def hostname_mapping(self) -> Optional[List[HostnameMapping]]: + """Returns the hostname mapping. + + A list that maps the hostnames to IP address. + """ + if not self.relation.app: + return None + + if mapping := self.relation.data[self.relation.app].get("hostname-mapping"): + return json.loads(mapping) + class DatabaseCreatedEvent(AuthenticationEvent, DatabaseRequiresEvent): """Event emitted when a new database is created for use on this relation.""" @@ -2602,6 +2617,17 @@ def set_version(self, relation_id: int, version: str) -> None: """ self.update_relation_data(relation_id, {"version": version}) + def set_hostname_mapping( + self, relation_id: int, hostname_mapping: List[HostnameMapping] + ) -> None: + """Set hostname mapping. + + Args: + relation_id: the identifier for a particular relation. + hostname_mapping: list of hostname mapping. + """ + self.update_relation_data(relation_id, {"hostname-mapping": json.dumps(hostname_mapping)}) + class DatabaseProviderEventHandlers(EventHandlers): """Provider-side of the database relation handlers."""