def update_subscription_with_dead_letter_policy(project_id, topic_id, subscription_id, dead_letter_topic_id): """Update a subscription's dead letter policy.""" # [START pubsub_dead_letter_update_subscription] from google.cloud import pubsub_v1 from google.cloud.pubsub_v1.types import DeadLetterPolicy, FieldMask # TODO(developer) # project_id = "your-project-id" # TODO(developer): This is an existing topic that the subscription # with dead letter policy is attached to. # topic_id = "your-topic-id" # TODO(developer): This is an existing subscription with a dead letter policy. # subscription_id = "your-subscription-id" # TODO(developer): This is an existing dead letter topic that the subscription # with dead letter policy will forward dead letter messages to. # dead_letter_topic_id = "your-dead-letter-topic-id" publisher = pubsub_v1.PublisherClient() subscriber = pubsub_v1.SubscriberClient() topic_path = publisher.topic_path(project_id, topic_id) subscription_path = subscriber.subscription_path(project_id, subscription_id) dead_letter_topic_path = publisher.topic_path(project_id, dead_letter_topic_id) subscription_before_update = subscriber.get_subscription( request={"subscription": subscription_path}) print("Before the update: {}".format(subscription_before_update)) # Indicates which fields in the provided subscription to update. update_mask = FieldMask(paths=["dead_letter_policy.max_delivery_attempts"]) # Construct a dead letter policy you expect to have after the update. dead_letter_policy = DeadLetterPolicy( dead_letter_topic=dead_letter_topic_path, max_delivery_attempts=20) # Construct the subscription with the dead letter policy you expect to have # after the update. Here, values in the required fields (name, topic) help # identify the subscription. subscription = pubsub_v1.types.Subscription( name=subscription_path, topic=topic_path, dead_letter_policy=dead_letter_policy, ) with subscriber: subscription_after_update = subscriber.update_subscription( request={ "subscription": subscription, "update_mask": update_mask }) print("After the update: {}".format(subscription_after_update)) # [END pubsub_dead_letter_update_subscription] return subscription_after_update
def remove_dead_letter_policy(project_id, topic_id, subscription_id): """Remove dead letter policy from a subscription.""" # [START pubsub_dead_letter_remove] from google.cloud import pubsub_v1 from google.cloud.pubsub_v1.types import FieldMask # TODO(developer) # project_id = "your-project-id" # TODO(developer): This is an existing topic that the subscription # with dead letter policy is attached to. # topic_id = "your-topic-id" # TODO(developer): This is an existing subscription with a dead letter policy. # subscription_id = "your-subscription-id" publisher = pubsub_v1.PublisherClient() subscriber = pubsub_v1.SubscriberClient() topic_path = publisher.topic_path(project_id, topic_id) subscription_path = subscriber.subscription_path(project_id, subscription_id) subscription_before_update = subscriber.get_subscription( request={"subscription": subscription_path}) print("Before removing the policy: {}".format(subscription_before_update)) # Indicates which fields in the provided subscription to update. update_mask = FieldMask(paths=[ "dead_letter_policy.dead_letter_topic", "dead_letter_policy.max_delivery_attempts", ]) # Construct the subscription (without any dead letter policy) that you # expect to have after the update. subscription = pubsub_v1.types.Subscription(name=subscription_path, topic=topic_path) with subscriber: subscription_after_update = subscriber.update_subscription( request={ "subscription": subscription, "update_mask": update_mask }) print("After removing the policy: {}".format(subscription_after_update)) # [END pubsub_dead_letter_remove] return subscription_after_update