def topic(client: AdminClient) -> Generator[Topic, None, None]: location = CloudZone(CloudRegion(CLOUD_REGION), ZONE_ID) topic_path = TopicPath(PROJECT_NUMBER, location, TOPIC_ID) # A topic of 2 partitions, each of size 30 GiB, publish throughput # capacity per partition to 4 MiB/s, and subscribe throughput # capacity per partition to 8 MiB/s. topic = Topic( name=str(topic_path), partition_config=Topic.PartitionConfig( count=2, capacity=Topic.PartitionConfig.Capacity( publish_mib_per_sec=4, subscribe_mib_per_sec=8, ), ), retention_config=Topic.RetentionConfig(per_partition_bytes=30 * 1024 * 1024 * 1024, ), ) try: response = client.get_topic(topic.name) except NotFound: response = client.create_topic(topic) yield response try: client.delete_topic(response.name) except NotFound as e: print(e.message)
def update_lite_topic(project_number, cloud_region, zone_id, topic_id): # [START pubsublite_update_topic] from google.api_core.exceptions import NotFound from google.cloud.pubsublite import AdminClient, Topic from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath from google.protobuf.duration_pb2 import Duration from google.protobuf.field_mask_pb2 import FieldMask # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # topic_id = "your-topic-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) # Defines which topic fields to update. field_mask = FieldMask(paths=[ "partition_config.scale", "retention_config.per_partition_bytes", "retention_config.period", ]) # Defines how to update the topic fields. topic = Topic( name=str(topic_path), partition_config=Topic.PartitionConfig( # Set publishing throughput to 2x standard partition throughput of 4 MiB # per second. This must in the range [1,4]. A topic with `scale` of 2 and # `count` of 10 is charged for 20 partitions. scale=2, ), retention_config=Topic.RetentionConfig( # Set storage per partition to 100 GiB. This must be in the range 30 GiB-10TiB. # If the number of byptes stored in any of the topic's partitions grows beyond # this value, older messages will be dropped to make room for newer ones, # regardless of the value of `period`. # Be careful when decreasing storage per partition as it may cuase lost messages. per_partition_bytes=100 * 1024 * 1024 * 1024, # Allow messages to be stored for 14 days. period=Duration(seconds=60 * 60 * 24 * 14), ), ) client = AdminClient(cloud_region) try: response = client.update_topic(topic, field_mask) print(f"{response.name} updated successfully.") except NotFound: print(f"{topic_path} not found.")
def seek_lite_subscription(project_number, cloud_region, zone_id, subscription_id, seek_target, wait_for_operation): # [START pubsublite_seek_subscription] from google.api_core.exceptions import NotFound from google.cloud.pubsublite import AdminClient from google.cloud.pubsublite.types import CloudRegion, CloudZone, SubscriptionPath # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # subscription_id = "your-subscription-id" # seek_target = BacklogLocation.BEGINNING # wait_for_operation = False # Possible values for seek_target: # - BacklogLocation.BEGINNING: replays from the beginning of all retained # messages. # - BacklogLocation.END: skips past all current published messages. # - PublishTime(<datetime>): delivers messages with publish time greater # than or equal to the specified timestamp. # - EventTime(<datetime>): seeks to the first message with event time # greater than or equal to the specified timestamp. # Waiting for the seek operation to complete is optional. It indicates when # subscribers for all partitions are receiving messages from the seek # target. If subscribers are offline, the operation will complete once they # are online. cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) client = AdminClient(cloud_region) try: # Initiate an out-of-band seek for a subscription to the specified # target. If an operation is returned, the seek has been successfully # registered and will eventually propagate to subscribers. seek_operation = client.seek_subscription(subscription_path, seek_target) print(f"Seek operation: {seek_operation.operation.name}") except NotFound: print(f"{subscription_path} not found.") return if wait_for_operation: print("Waiting for operation to complete...") seek_operation.result() print(f"Operation completed. Metadata:\n{seek_operation.metadata}")
def create_lite_subscription(project_number, cloud_region, zone_id, topic_id, subscription_id): # [START pubsublite_create_subscription] from google.api_core.exceptions import AlreadyExists from google.cloud.pubsublite import AdminClient, Subscription from google.cloud.pubsublite.types import ( CloudRegion, CloudZone, SubscriptionPath, TopicPath, ) # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # topic_id = "your-topic-id" # subscription_id = "your-subscription-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) subscription_path = SubscriptionPath(project_number, location, subscription_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, ), ) client = AdminClient(cloud_region) try: response = client.create_subscription(subscription) print(f"{response.name} created successfully.") except AlreadyExists: print(f"{subscription_path} already exists.")
def list_lite_subscriptions_in_project(project_number, cloud_region, zone_id): # [START pubsublite_list_subscriptions_in_project] from google.cloud.pubsublite import AdminClient from google.cloud.pubsublite.types import CloudRegion, CloudZone, LocationPath # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) location_path = LocationPath(project_number, location) client = AdminClient(cloud_region) response = client.list_subscriptions(location_path) for subscription in response: print(subscription.name) print(f"{len(response)} subscription(s) listed in your project and location.")
def subscription(client: AdminClient) -> Generator[Subscription, None, None]: location = CloudZone(CloudRegion(CLOUD_REGION), ZONE_ID) subscription_path = SubscriptionPath(PROJECT_NUMBER, location, SUBSCRIPTION_ID) subscription = Subscription( name=str(subscription_path), topic=f"projects/{PROJECT_NUMBER}/locations/{location}/topics/{PERMANENT_TOPIC_ID}", delivery_config=Subscription.DeliveryConfig( delivery_requirement=Subscription.DeliveryConfig.DeliveryRequirement.DELIVER_IMMEDIATELY, ), ) try: response = client.get_subscription(subscription.name) except NotFound: # This subscription will start receiving the first message in the topic. response = client.create_subscription(subscription, BacklogLocation.BEGINNING) yield response try: client.delete_subscription(response.name) except NotFound as e: print(e.message)
def delete_lite_topic(project_number, cloud_region, zone_id, topic_id): # [START pubsublite_delete_topic] from google.api_core.exceptions import NotFound from google.cloud.pubsublite import AdminClient from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # topic_id = "your-topic-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) client = AdminClient(cloud_region) try: client.delete_topic(topic_path) print(f"{topic_path} deleted successfully.") except NotFound: print(f"{topic_path} not found.")
def update_lite_subscription(project_number, cloud_region, zone_id, subscription_id): # [START pubsublite_update_subscription] from google.api_core.exceptions import NotFound from google.cloud.pubsublite import AdminClient, Subscription from google.cloud.pubsublite.types import CloudRegion, CloudZone, SubscriptionPath from google.protobuf.field_mask_pb2 import FieldMask # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # topic_id = "your-topic-id" # subscription_id = "your-subscription-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) field_mask = FieldMask(paths=["delivery_config.delivery_requirement"]) subscription = Subscription( name=str(subscription_path), delivery_config=Subscription.DeliveryConfig( # Possible values for delivery_requirement: # - `DELIVER_IMMEDIATELY` # - `DELIVER_AFTER_STORED` # `DELIVER_AFTER_STORED` requires a published message to be successfully written # to storage before the server delivers it to subscribers. delivery_requirement=Subscription.DeliveryConfig. DeliveryRequirement.DELIVER_AFTER_STORED, ), ) client = AdminClient(cloud_region) try: response = client.update_subscription(subscription, field_mask) print(f"{response.name} updated successfully.") except NotFound: print(f"{subscription_path} not found.")
def list_lite_subscriptions_in_topic(project_number, cloud_region, zone_id, topic_id): # [START pubsublite_list_subscriptions_in_topic] from google.cloud.pubsublite import AdminClient from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # topic_id = "your-topic-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) client = AdminClient(cloud_region) response = client.list_topic_subscriptions(topic_path) for subscription_path in response: print(subscription_path) print(f"{len(response)} subscription(s) listed in your topic.")
def get_lite_topic(project_number, cloud_region, zone_id, topic_id): # [START pubsublite_get_topic] from google.api_core.exceptions import NotFound from google.cloud.pubsublite import AdminClient from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # topic_id = "your-topic-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) topic_path = TopicPath(project_number, location, topic_id) client = AdminClient(cloud_region) try: response = client.get_topic(topic_path) num_partitions = client.get_topic_partition_count(topic_path) print(f"{response.name} has {num_partitions} partition(s).") except NotFound: print(f"{topic_path} not found.")
def get_lite_subscription(project_number, cloud_region, zone_id, subscription_id): # [START pubsublite_get_subscription] from google.api_core.exceptions import NotFound from google.cloud.pubsublite import AdminClient from google.cloud.pubsublite.types import CloudRegion, CloudZone, SubscriptionPath # TODO(developer): # project_number = 1122334455 # cloud_region = "us-central1" # zone_id = "a" # subscription_id = "your-subscription-id" cloud_region = CloudRegion(cloud_region) location = CloudZone(cloud_region, zone_id) subscription_path = SubscriptionPath(project_number, location, subscription_id) client = AdminClient(cloud_region) try: response = client.get_subscription(subscription_path) print(f"{response.name} exists.") except NotFound: print(f"{subscription_path} not found.")
def client() -> Generator[AdminClient, None, None]: yield AdminClient(CLOUD_REGION)
def client(): yield AdminClient(CLOUD_REGION)