Skip to content

Commit

Permalink
Update AWS bucket for backups integration tests (#172)
Browse files Browse the repository at this point in the history
* Update AWS bucket for backups integration tests

* Update data_interfaces library to fix lint warnings
  • Loading branch information
shayancanonical authored Mar 16, 2023
1 parent 1d15721 commit 890246e
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 47 deletions.
70 changes: 55 additions & 15 deletions lib/charms/data_platform_libs/v0/data_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
This library contains the Requires and Provides classes for handling the relation
between an application and multiple managed application supported by the data-team:
MySQL, Postgresql, MongoDB, Redis, and Kakfa.
MySQL, Postgresql, MongoDB, Redis, and Kafka.
### Database (MySQL, Postgresql, MongoDB, and Redis)
Expand Down Expand Up @@ -303,7 +303,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 = 7
LIBPATCH = 9

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -660,6 +660,11 @@ class DatabaseProvidesEvents(CharmEvents):
class DatabaseRequiresEvent(RelationEvent):
"""Base class for database events."""

@property
def database(self) -> Optional[str]:
"""Returns the database name."""
return self.relation.data[self.relation.app].get("database")

@property
def endpoints(self) -> Optional[str]:
"""Returns a comma separated list of read/write endpoints."""
Expand Down Expand Up @@ -743,6 +748,18 @@ def _on_relation_changed(self, event: RelationChangedEvent) -> None:
if "database" in diff.added:
self.on.database_requested.emit(event.relation, app=event.app, unit=event.unit)

def set_database(self, relation_id: int, database_name: str) -> None:
"""Set database name.
This function writes in the application data bag, therefore,
only the leader unit can call it.
Args:
relation_id: the identifier for a particular relation.
database_name: database name.
"""
self._update_relation_data(relation_id, {"database": database_name})

def set_endpoints(self, relation_id: int, connection_strings: str) -> None:
"""Set database primary connections.
Expand Down Expand Up @@ -973,6 +990,11 @@ def topic(self) -> Optional[str]:
"""Returns the topic that was requested."""
return self.relation.data[self.relation.app].get("topic")

@property
def consumer_group_prefix(self) -> Optional[str]:
"""Returns the consumer-group-prefix that was requested."""
return self.relation.data[self.relation.app].get("consumer-group-prefix")


class TopicRequestedEvent(KafkaProvidesEvent, ExtraRoleEvent):
"""Event emitted when a new topic is requested for use on this relation."""
Expand All @@ -990,6 +1012,11 @@ class KafkaProvidesEvents(CharmEvents):
class KafkaRequiresEvent(RelationEvent):
"""Base class for Kafka events."""

@property
def topic(self) -> Optional[str]:
"""Returns the topic."""
return self.relation.data[self.relation.app].get("topic")

@property
def bootstrap_server(self) -> Optional[str]:
"""Returns a a comma-seperated list of broker uris."""
Expand Down Expand Up @@ -1049,6 +1076,15 @@ def _on_relation_changed(self, event: RelationChangedEvent) -> None:
if "topic" in diff.added:
self.on.topic_requested.emit(event.relation, app=event.app, unit=event.unit)

def set_topic(self, relation_id: int, topic: str) -> None:
"""Set topic name in the application relation databag.
Args:
relation_id: the identifier for a particular relation.
topic: the topic name.
"""
self._update_relation_data(relation_id, {"topic": topic})

def set_bootstrap_server(self, relation_id: int, bootstrap_server: str) -> None:
"""Set the bootstrap server in the application relation databag.
Expand Down Expand Up @@ -1082,26 +1118,30 @@ class KafkaRequires(DataRequires):

on = KafkaRequiresEvents()

def __init__(self, charm, relation_name: str, topic: str, extra_user_roles: str = None):
def __init__(
self,
charm,
relation_name: str,
topic: str,
extra_user_roles: Optional[str] = None,
consumer_group_prefix: Optional[str] = None,
):
"""Manager of Kafka client relations."""
# super().__init__(charm, relation_name)
super().__init__(charm, relation_name, extra_user_roles)
self.charm = charm
self.topic = topic
self.consumer_group_prefix = consumer_group_prefix or ""

def _on_relation_joined_event(self, event: RelationJoinedEvent) -> None:
"""Event emitted when the application joins the Kafka relation."""
# Sets both topic and extra user roles in the relation
# if the roles are provided. Otherwise, sets only the topic.
self._update_relation_data(
event.relation.id,
{
"topic": self.topic,
"extra-user-roles": self.extra_user_roles,
}
if self.extra_user_roles is not None
else {"topic": self.topic},
)
# Sets topic, extra user roles, and "consumer-group-prefix" in the relation
relation_data = {
f: getattr(self, f.replace("-", "_"), "")
for f in ["consumer-group-prefix", "extra-user-roles", "topic"]
}

self._update_relation_data(event.relation.id, relation_data)

def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
"""Event emitted when the Kafka relation has changed."""
Expand All @@ -1119,7 +1159,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
# “endpoints_changed“ event if “topic_created“ is triggered.
return

# Emit an endpoints (bootstap-server) changed event if the Kakfa endpoints
# Emit an endpoints (bootstap-server) changed event if the Kafka endpoints
# added or changed this info in the relation databag.
if "endpoints" in diff.added or "endpoints" in diff.changed:
# Emit the default event (the one without an alias).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
This library contains the Requires and Provides classes for handling the relation
between an application and multiple managed application supported by the data-team:
MySQL, Postgresql, MongoDB, Redis, and Kakfa.
MySQL, Postgresql, MongoDB, Redis, and Kafka.
### Database (MySQL, Postgresql, MongoDB, and Redis)
Expand Down Expand Up @@ -303,7 +303,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 = 7
LIBPATCH = 9

PYDEPS = ["ops>=2.0.0"]

Expand Down Expand Up @@ -660,6 +660,11 @@ class DatabaseProvidesEvents(CharmEvents):
class DatabaseRequiresEvent(RelationEvent):
"""Base class for database events."""

@property
def database(self) -> Optional[str]:
"""Returns the database name."""
return self.relation.data[self.relation.app].get("database")

@property
def endpoints(self) -> Optional[str]:
"""Returns a comma separated list of read/write endpoints."""
Expand Down Expand Up @@ -743,6 +748,18 @@ def _on_relation_changed(self, event: RelationChangedEvent) -> None:
if "database" in diff.added:
self.on.database_requested.emit(event.relation, app=event.app, unit=event.unit)

def set_database(self, relation_id: int, database_name: str) -> None:
"""Set database name.
This function writes in the application data bag, therefore,
only the leader unit can call it.
Args:
relation_id: the identifier for a particular relation.
database_name: database name.
"""
self._update_relation_data(relation_id, {"database": database_name})

def set_endpoints(self, relation_id: int, connection_strings: str) -> None:
"""Set database primary connections.
Expand Down Expand Up @@ -973,6 +990,11 @@ def topic(self) -> Optional[str]:
"""Returns the topic that was requested."""
return self.relation.data[self.relation.app].get("topic")

@property
def consumer_group_prefix(self) -> Optional[str]:
"""Returns the consumer-group-prefix that was requested."""
return self.relation.data[self.relation.app].get("consumer-group-prefix")


class TopicRequestedEvent(KafkaProvidesEvent, ExtraRoleEvent):
"""Event emitted when a new topic is requested for use on this relation."""
Expand All @@ -990,6 +1012,11 @@ class KafkaProvidesEvents(CharmEvents):
class KafkaRequiresEvent(RelationEvent):
"""Base class for Kafka events."""

@property
def topic(self) -> Optional[str]:
"""Returns the topic."""
return self.relation.data[self.relation.app].get("topic")

@property
def bootstrap_server(self) -> Optional[str]:
"""Returns a a comma-seperated list of broker uris."""
Expand Down Expand Up @@ -1049,6 +1076,15 @@ def _on_relation_changed(self, event: RelationChangedEvent) -> None:
if "topic" in diff.added:
self.on.topic_requested.emit(event.relation, app=event.app, unit=event.unit)

def set_topic(self, relation_id: int, topic: str) -> None:
"""Set topic name in the application relation databag.
Args:
relation_id: the identifier for a particular relation.
topic: the topic name.
"""
self._update_relation_data(relation_id, {"topic": topic})

def set_bootstrap_server(self, relation_id: int, bootstrap_server: str) -> None:
"""Set the bootstrap server in the application relation databag.
Expand Down Expand Up @@ -1082,26 +1118,30 @@ class KafkaRequires(DataRequires):

on = KafkaRequiresEvents()

def __init__(self, charm, relation_name: str, topic: str, extra_user_roles: str = None):
def __init__(
self,
charm,
relation_name: str,
topic: str,
extra_user_roles: Optional[str] = None,
consumer_group_prefix: Optional[str] = None,
):
"""Manager of Kafka client relations."""
# super().__init__(charm, relation_name)
super().__init__(charm, relation_name, extra_user_roles)
self.charm = charm
self.topic = topic
self.consumer_group_prefix = consumer_group_prefix or ""

def _on_relation_joined_event(self, event: RelationJoinedEvent) -> None:
"""Event emitted when the application joins the Kafka relation."""
# Sets both topic and extra user roles in the relation
# if the roles are provided. Otherwise, sets only the topic.
self._update_relation_data(
event.relation.id,
{
"topic": self.topic,
"extra-user-roles": self.extra_user_roles,
}
if self.extra_user_roles is not None
else {"topic": self.topic},
)
# Sets topic, extra user roles, and "consumer-group-prefix" in the relation
relation_data = {
f: getattr(self, f.replace("-", "_"), "")
for f in ["consumer-group-prefix", "extra-user-roles", "topic"]
}

self._update_relation_data(event.relation.id, relation_data)

def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
"""Event emitted when the Kafka relation has changed."""
Expand All @@ -1119,7 +1159,7 @@ def _on_relation_changed_event(self, event: RelationChangedEvent) -> None:
# “endpoints_changed“ event if “topic_created“ is triggered.
return

# Emit an endpoints (bootstap-server) changed event if the Kakfa endpoints
# Emit an endpoints (bootstap-server) changed event if the Kafka endpoints
# added or changed this info in the relation databag.
if "endpoints" in diff.added or "endpoints" in diff.changed:
# Emit the default event (the one without an alias).
Expand Down
Loading

0 comments on commit 890246e

Please sign in to comment.