From 51b5b644a2125109bc5287f8f20a17731720a5d6 Mon Sep 17 00:00:00 2001 From: Dragomir Penev <6687393+dragomirp@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:21:28 +0200 Subject: [PATCH] [DPE-2844] Use named redis interfaces for indico (#330) * Use named redis interfaces for indico * Bump lib * Redis cache relation Signed-off-by: Marcelo Henrique Neppel --- .../data_platform_libs/v0/data_interfaces.py | 27 ++++++++++++++++--- .../new_relations/test_new_relations.py | 4 +-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/charms/data_platform_libs/v0/data_interfaces.py b/lib/charms/data_platform_libs/v0/data_interfaces.py index 5d1691d9f1..714eace460 100644 --- a/lib/charms/data_platform_libs/v0/data_interfaces.py +++ b/lib/charms/data_platform_libs/v0/data_interfaces.py @@ -320,7 +320,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 = 23 +LIBPATCH = 24 PYDEPS = ["ops>=2.0.0"] @@ -526,7 +526,16 @@ def get_content(self) -> Dict[str, str]: """Getting cached secret content.""" if not self._secret_content: if self.meta: - self._secret_content = self.meta.get_content() + try: + self._secret_content = self.meta.get_content(refresh=True) + except (ValueError, ModelError) as err: + # https://bugs.launchpad.net/juju/+bug/2042596 + # Only triggered when 'refresh' is set + msg = "ERROR either URI or label should be used for getting an owned secret but not both" + if isinstance(err, ModelError) and msg not in str(err): + raise + # Due to: ValueError: Secret owner cannot use refresh=True + self._secret_content = self.meta.get_content() return self._secret_content def set_content(self, content: Dict[str, str]) -> None: @@ -1085,7 +1094,7 @@ def _delete_relation_secret( secret = self._get_relation_secret(relation.id, group) if not secret: - logging.error("Can't update secret for relation %s", str(relation.id)) + logging.error("Can't delete secret for relation %s", str(relation.id)) return False old_content = secret.get_content() @@ -1827,7 +1836,8 @@ def _assign_relation_alias(self, relation_id: int) -> None: # We need to set relation alias also on the application level so, # it will be accessible in show-unit juju command, executed for a consumer application unit - self.update_relation_data(relation_id, {"alias": available_aliases[0]}) + if self.local_unit.is_leader(): + self.update_relation_data(relation_id, {"alias": available_aliases[0]}) def _emit_aliased_event(self, event: RelationChangedEvent, event_name: str) -> None: """Emit an aliased event to a particular relation if it has an alias. @@ -1914,6 +1924,9 @@ def _on_relation_created_event(self, event: RelationCreatedEvent) -> None: # Sets both database and extra user roles in the relation # if the roles are provided. Otherwise, sets only the database. + if not self.local_unit.is_leader(): + return + if self.extra_user_roles: self.update_relation_data( event.relation.id, @@ -2173,6 +2186,9 @@ def _on_relation_created_event(self, event: RelationCreatedEvent) -> None: """Event emitted when the Kafka relation is created.""" super()._on_relation_created_event(event) + if not self.local_unit.is_leader(): + return + # Sets topic, extra user roles, and "consumer-group-prefix" in the relation relation_data = { f: getattr(self, f.replace("-", "_"), "") @@ -2345,6 +2361,9 @@ def _on_relation_created_event(self, event: RelationCreatedEvent) -> None: """Event emitted when the OpenSearch relation is created.""" super()._on_relation_created_event(event) + if not self.local_unit.is_leader(): + return + # Sets both index and extra user roles in the relation if the roles are provided. # Otherwise, sets only the index. data = {"index": self.index} diff --git a/tests/integration/new_relations/test_new_relations.py b/tests/integration/new_relations/test_new_relations.py index db3f072efa..e268bd9dc5 100644 --- a/tests/integration/new_relations/test_new_relations.py +++ b/tests/integration/new_relations/test_new_relations.py @@ -583,8 +583,8 @@ async def test_indico_datatabase(ops_test: OpsTest) -> None: await ops_test.model.deploy("redis-k8s", channel="stable", application_name="redis-broker") await ops_test.model.deploy("redis-k8s", channel="stable", application_name="redis-cache") await asyncio.gather( - ops_test.model.relate("redis-broker", "indico"), - ops_test.model.relate("redis-cache", "indico"), + ops_test.model.relate("redis-broker", "indico:redis-broker"), + ops_test.model.relate("redis-cache", "indico:redis-cache"), ) # Wait for model to stabilise