From e4d047c7914e7e3e0f1adb3011bfb83e501a13c0 Mon Sep 17 00:00:00 2001 From: Dolf Andringa Date: Wed, 4 Oct 2023 22:42:11 +0800 Subject: [PATCH] Catch PermissionDenied from create_subscription and continue to try to update the existing subscription if necessary --- rele/client.py | 2 +- tests/test_subscriber.py | 51 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/rele/client.py b/rele/client.py index da66622..3fb165d 100644 --- a/rele/client.py +++ b/rele/client.py @@ -84,7 +84,7 @@ def update_or_create_subscription(self, subscription): topic = self._create_topic(topic_path) logger.info(f"Topic {topic.name} created.") self._create_subscription(subscription_path, topic_path, subscription) - except exceptions.AlreadyExists: + except (exceptions.AlreadyExists, exceptions.PermissionDenied): self._update_subscription(subscription_path, topic_path, subscription) def _create_topic(self, topic_path): diff --git a/tests/test_subscriber.py b/tests/test_subscriber.py index 91fd72b..0aff6f1 100644 --- a/tests/test_subscriber.py +++ b/tests/test_subscriber.py @@ -266,6 +266,57 @@ def test_subscription_is_updated_with_retry_policy_when_already_exists( request={"subscription": subscription, "update_mask": update_mask} ) + @patch.object(SubscriberClient, "create_subscription") + @patch.object(SubscriberClient, "update_subscription") + def test_updates_subscription_when_create_raises_permission_denied( + self, + client_update_subscription, + client_create_subscription, + project_id, + subscriber, + ): + subscription_path = ( + f"projects/{project_id}/subscriptions/{project_id}-test-topic" + ) + topic_path = f"projects/{project_id}/topics/{project_id}-test-topic" + retry_policy = pubsub_v1.types.RetryPolicy( + minimum_backoff=duration_pb2.Duration(seconds=10), + maximum_backoff=duration_pb2.Duration(seconds=50), + ) + update_mask = FieldMask(paths=["retry_policy"]) + client_create_subscription.side_effect = exceptions.PermissionDenied( + message="Permission denied to create subscription" + ) + + expected_subscription = pubsub_v1.types.Subscription( + name=subscription_path, + topic=topic_path, + retry_policy=retry_policy, + ) + + subscriber.update_or_create_subscription( + Subscription( + None, + topic=f"{project_id}-test-topic", + retry_policy=RetryPolicy(10, 50), + ) + ) + + client_create_subscription.assert_called_once_with( + request={ + "name": subscription_path, + "topic": topic_path, + "ack_deadline_seconds": 60, + "retry_policy": retry_policy, + } + ) + client_update_subscription.assert_called_once_with( + request={ + "subscription": expected_subscription, + "update_mask": update_mask, + } + ) + @patch.object( SubscriberClient, "create_subscription",