From f266001f70fec581d2af5beae279200c19eb5a4c Mon Sep 17 00:00:00 2001 From: Javier de la Puente Date: Wed, 11 Sep 2024 14:06:24 +0200 Subject: [PATCH] Code suggestions --- paas_app_charmer/rabbitmq.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/paas_app_charmer/rabbitmq.py b/paas_app_charmer/rabbitmq.py index d22c5b4..41ff4af 100644 --- a/paas_app_charmer/rabbitmq.py +++ b/paas_app_charmer/rabbitmq.py @@ -100,7 +100,7 @@ def __init__(self, charm: CharmBase, relation_name: str, username: str, vhost: s ) self.framework.observe( self.charm.on[relation_name].relation_departed, - self._on_rabbitmq_relation_changed, + self._on_rabbitmq_relation_departed, ) self.framework.observe( self.charm.on[relation_name].relation_broken, @@ -117,6 +117,11 @@ def _on_rabbitmq_relation_changed(self, _: HookEvent) -> None: if self.rabbitmq_uri(): self.on.ready.emit() + def _on_rabbitmq_relation_departed(self, _: HookEvent) -> None: + """Handle RabbitMQ departed.""" + if self.rabbitmq_uri(): + self.on.ready.emit() + def _on_rabbitmq_relation_broken(self, _: HookEvent) -> None: """Handle RabbitMQ broken.""" self.on.departed.emit() @@ -150,11 +155,11 @@ def request_access(self, username: str, vhost: str) -> None: vhost: virtual host requested for RabbitMQ """ if self.model.unit.is_leader(): - if self._rabbitmq_rel: - self._rabbitmq_rel.data[self.charm.app]["username"] = username - self._rabbitmq_rel.data[self.charm.app]["vhost"] = vhost - else: + if not self._rabbitmq_rel: logger.warning("request_access but no rabbitmq relation") + return + self._rabbitmq_rel.data[self.charm.app]["username"] = username + self._rabbitmq_rel.data[self.charm.app]["vhost"] = vhost def _rabbitmq_server_uri(self) -> str | None: """Return uri for rabbitmq-server.