Skip to content
Draft
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
34 changes: 30 additions & 4 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent):
Optional,
Set,
Tuple,
TypedDict,
Union,
ValuesView,
)
Expand All @@ -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"]

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -2277,7 +2280,7 @@ def __init__(


################################################################################
# Cross-charm Relatoins Data Handling and Evenets
# Cross-charm Relations Data Handling and Events
################################################################################

# Generic events
Expand All @@ -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
"""
Expand All @@ -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):
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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."""
Expand Down