Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: add samples #37

Merged
merged 23 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
82 changes: 82 additions & 0 deletions samples/snippets/create_lite_subscription_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application demonstrates how to create a subscription with the Pub/Sub
Lite API. For more information, see the root level README.md and the
documentation at https://cloud.google.com/pubsub/lite/docs/subscriptions.
"""

import argparse


def create_lite_subscription(
project_number, cloud_region, zone_id, topic_id, subscription_id
):
# [START pubsublite_create_subscription]
from google.cloud.pubsublite.location import CloudRegion, CloudZone
from google.cloud.pubsublite.make_admin_client import make_admin_client
from google.cloud.pubsublite.paths import SubscriptionPath, TopicPath
from google.cloud.pubsublite_v1 import Subscription

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"

client = make_admin_client(cloud_region)

location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = str(TopicPath(project_number, location, topic_id))
subscription_path = str(SubscriptionPath(project_number, location, subscription_id))

subscription = Subscription(
name=subscription_path,
topic=topic_path,
delivery_config=Subscription.DeliveryConfig(
# The server does not wait for a published message to be successfully
# written to storage before delivering it to subscribers. As such, a subscriber
# may receive a message for which the write to storage failed. If the subscriber
# re-reads the offset of that mesage later on, there may be a gap at that offset.
anguillanneuf marked this conversation as resolved.
Show resolved Hide resolved
delivery_requirement=Subscription.DeliveryConfig.DeliveryRequirement.DELIVER_IMMEDIATELY,
),
)

response = client.create_subscription(subscription)
print(f"{response}\ncreated successfully.")
anguillanneuf marked this conversation as resolved.
Show resolved Hide resolved
# [END pubsublite_create_subscription]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_number", help="Your Google Cloud Project Number")
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
parser.add_argument("topic_id", help="Your topic ID")
parser.add_argument("subscription_id", help="Your subscription ID")

args = parser.parse_args()

create_lite_subscription(
args.project_number,
args.cloud_region,
args.zone_id,
args.topic_id,
args.subscription_id,
)
90 changes: 90 additions & 0 deletions samples/snippets/create_lite_topic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application demonstrates how to create a topic with the Pub/Sub
Lite API. For more information, see the root level README.md and the
documentation at https://cloud.google.com/pubsub/lite/docs/topics.
"""

import argparse


def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_partitions):
# [START pubsublite_create_topic]
from google.cloud.pubsublite.location import CloudRegion, CloudZone
from google.cloud.pubsublite.make_admin_client import make_admin_client
from google.cloud.pubsublite.paths import TopicPath
from google.cloud.pubsublite_v1 import Topic
from google.protobuf.duration_pb2 import Duration

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# topic_id = "your-topic-id"
# num_partitions = 1

client = make_admin_client(cloud_region)

location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = str(TopicPath(project_number, location, topic_id))
topic = Topic(
name=topic_path,
partition_config=Topic.PartitionConfig(
# This must be greater than 1.
count=num_partitions,
# Set publishing throughput to 1x standard partition throughput of 4 MiB
dpcollins-google marked this conversation as resolved.
Show resolved Hide resolved
# 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=1,
),
retention_config=Topic.RetentionConfig(
# Set storage per partition to 30 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`.
per_partition_bytes=30 * 1024 * 1024 * 1024,
# How long messages are retained.
period=Duration(seconds=60 * 60 * 24 * 7),
),
)

response = client.create_topic(topic)
print(f"{response}\ncreated successfully.")
anguillanneuf marked this conversation as resolved.
Show resolved Hide resolved
# [END pubsublite_create_topic]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_number", help="Your Google Cloud Project Number")
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
parser.add_argument("topic_id", help="Your topic ID")
parser.add_argument(
"num_partitions", type=int, help="Number of partitions in the topic"
)

args = parser.parse_args()

create_lite_topic(
args.project_number,
args.cloud_region,
args.zone_id,
args.topic_id,
args.num_partitions,
)
60 changes: 60 additions & 0 deletions samples/snippets/delete_lite_subscription_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application demonstrates how to delete a subscription with the Pub/Sub
Lite API. For more information, see the root level README.md and the
documentation at https://cloud.google.com/pubsub/lite/docs/subscriptions.
"""

import argparse


def delete_lite_subscription(project_number, cloud_region, zone_id, subscription_id):
# [START pubsublite_delete_subscription]
from google.cloud.pubsublite.location import CloudRegion, CloudZone
from google.cloud.pubsublite.make_admin_client import make_admin_client
from google.cloud.pubsublite.paths import SubscriptionPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# subscription_id = "your-subscription-id"

client = make_admin_client(cloud_region)

location = CloudZone(CloudRegion(cloud_region), zone_id)
subscription_path = str(SubscriptionPath(project_number, location, subscription_id))

client.delete_subscription(subscription_path)
print(f"{subscription_path} deleted successfully.")
# [END pubsublite_delete_subscription]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_number", help="Your Google Cloud Project Number")
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
parser.add_argument("subscription_id", help="Your subscription ID")

args = parser.parse_args()

delete_lite_subscription(
args.project_number, args.cloud_region, args.zone_id, args.subscription_id,
)
60 changes: 60 additions & 0 deletions samples/snippets/delete_lite_topic_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application demonstrates how to delete a topic with the Pub/Sub
Lite API. For more information, see the root level README.md and the
documentation at https://cloud.google.com/pubsub/lite/docs/topics.
"""

import argparse


def delete_lite_topic(project_number, cloud_region, zone_id, topic_id):
# [START pubsublite_delete_topic]
from google.cloud.pubsublite.location import CloudRegion, CloudZone
from google.cloud.pubsublite.make_admin_client import make_admin_client
from google.cloud.pubsublite.paths import TopicPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# topic_id = "your-topic-id"

client = make_admin_client(cloud_region)

location = CloudZone(CloudRegion(cloud_region), zone_id)
topic_path = str(TopicPath(project_number, location, topic_id))

client.delete_topic(topic_path)
print(f"{topic_path} deleted successfully.")
# [END pubsublite_delete_topic]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_number", help="Your Google Cloud Project Number")
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
parser.add_argument("topic_id", help="Your topic ID")

args = parser.parse_args()

delete_lite_topic(
args.project_number, args.cloud_region, args.zone_id, args.topic_id,
)
60 changes: 60 additions & 0 deletions samples/snippets/get_lite_subscription_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This application demonstrates how to get a subscription with the Pub/Sub
Lite API. For more information, see the root level README.md and the
documentation at https://cloud.google.com/pubsub/lite/docs/subscriptions.
"""

import argparse


def get_lite_subscription(project_number, cloud_region, zone_id, subscription_id):
# [START pubsublite_get_subscription]
from google.cloud.pubsublite.location import CloudRegion, CloudZone
from google.cloud.pubsublite.make_admin_client import make_admin_client
from google.cloud.pubsublite.paths import SubscriptionPath

# TODO(developer):
# project_number = 1122334455
# cloud_region = "us-central1"
# zone_id = "a"
# subscription_id = "your-subscription-id"

client = make_admin_client(cloud_region)

location = CloudZone(CloudRegion(cloud_region), zone_id)
subscription_path = str(SubscriptionPath(project_number, location, subscription_id))

response = client.get_subscription(subscription_path)
print(f"{response}\nexists.")
# [END pubsublite_get_subscription]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_number", help="Your Google Cloud Project Number")
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
parser.add_argument("subscription_id", help="Your subscription ID")

args = parser.parse_args()

get_lite_subscription(
args.project_number, args.cloud_region, args.zone_id, args.subscription_id,
)