from google.api_core.exceptions import AlreadyExists
from google.cloud.pubsub_v1 import PublisherClient
from google.cloud.pubsublite import AdminClient, Subscription, ExportConfig
from google.cloud.pubsublite.types import (
BacklogLocation,
CloudRegion,
CloudZone,
SubscriptionPath,
TopicPath,
)
def create_lite_pubsub_export_subscription(
project_number,
cloud_region="us-central1",
zone_id="a",
topic_id="my-topic-id",
subscription_id="my-subscription-id",
pubsub_topic_id="destination-topic-id",
regional=True,
target_location=BacklogLocation.BEGINNING,
):
if regional:
location = CloudRegion(cloud_region)
else:
location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = TopicPath(project_number, location, topic_id)
subscription_path = SubscriptionPath(project_number, location, subscription_id)
destination_topic_path = PublisherClient.topic_path(project_number, pubsub_topic_id)
subscription = Subscription(
name=str(subscription_path),
topic=str(topic_path),
delivery_config=Subscription.DeliveryConfig(
# Possible values for delivery_requirement:
# - `DELIVER_IMMEDIATELY`
# - `DELIVER_AFTER_STORED`
# You may choose whether to wait for a published message to be successfully written
# to storage before the server delivers it to subscribers. `DELIVER_IMMEDIATELY` is
# suitable for applications that need higher throughput.
delivery_requirement=Subscription.DeliveryConfig.DeliveryRequirement.DELIVER_IMMEDIATELY,
),
# Configures an export subscription that writes messages to a Pub/Sub topic.
export_config=ExportConfig(
# Possible values for desired_state:
# - `ACTIVE`: enable message processing.
# - `PAUSED`: suspend message processing.
desired_state=ExportConfig.State.ACTIVE,
pubsub_config=ExportConfig.PubSubConfig(
topic=destination_topic_path,
),
),
)
# Initialize client that will be used to send requests across threads. This
# client only needs to be created once, and can be reused for multiple requests.
client = AdminClient(cloud_region)
try:
response = client.create_subscription(subscription, target_location)
print(f"{response.name} created successfully.")
except AlreadyExists:
print(f"{subscription_path} already exists.")