Skip to content

Commit 9db70df

Browse files
samples: regional topics samples (#266)
* samples: regional topics samples * also update list and update topic samples * update deprecated partition config scale to capacity * update list topic example to use bool * update imports for reservation * choose regional or zonal topic in get and update topic samples
1 parent f0d65ca commit 9db70df

8 files changed

Lines changed: 398 additions & 151 deletions

samples/snippets/create_lite_topic_example.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,56 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
"""This application demonstrates how to create a topic with the Pub/Sub
18-
Lite API. For more information, see the root level README.md and the
19-
documentation at https://cloud.google.com/pubsub/lite/docs/topics.
17+
"""This application demonstrates how to create both a regional and zonal topic
18+
with the Pub/Sub Lite API. For more information, see the root level README.md
19+
and the documentation at https://cloud.google.com/pubsub/lite/docs/topics.
2020
"""
2121

2222
import argparse
2323

2424

25-
def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_partitions):
25+
def create_lite_topic(
26+
project_number,
27+
cloud_region,
28+
zone_id,
29+
topic_id,
30+
reservation_id,
31+
num_partitions,
32+
regional,
33+
):
2634
# [START pubsublite_create_topic]
2735
from google.api_core.exceptions import AlreadyExists
2836
from google.cloud.pubsublite import AdminClient, Topic
29-
from google.cloud.pubsublite.types import CloudRegion, CloudZone, TopicPath
37+
from google.cloud.pubsublite.types import (
38+
CloudRegion,
39+
CloudZone,
40+
ReservationPath,
41+
TopicPath,
42+
)
3043
from google.protobuf.duration_pb2 import Duration
3144

3245
# TODO(developer):
3346
# project_number = 1122334455
3447
# cloud_region = "us-central1"
3548
# zone_id = "a"
3649
# topic_id = "your-topic-id"
50+
# reservation_id = "your-reservation-id"
3751
# num_partitions = 1
52+
# regional = True
3853

3954
cloud_region = CloudRegion(cloud_region)
40-
location = CloudZone(cloud_region, zone_id)
41-
topic_path = TopicPath(project_number, location, topic_id)
55+
reservation_path = ReservationPath(project_number, cloud_region, reservation_id)
56+
57+
topic_path = None
58+
if regional:
59+
# A regional topic.
60+
topic_path = TopicPath(project_number, cloud_region, topic_id)
61+
else:
62+
# A zonal topic
63+
topic_path = TopicPath(
64+
project_number, CloudZone(cloud_region, zone_id), topic_id
65+
)
66+
4267
topic = Topic(
4368
name=str(topic_path),
4469
partition_config=Topic.PartitionConfig(
@@ -61,12 +86,18 @@ def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_parti
6186
# Allow messages to be retained for 7 days.
6287
period=Duration(seconds=60 * 60 * 24 * 7),
6388
),
89+
reservation_config=Topic.ReservationConfig(
90+
throughput_reservation=str(reservation_path),
91+
),
6492
)
6593

6694
client = AdminClient(cloud_region)
6795
try:
6896
response = client.create_topic(topic)
69-
print(f"{response.name} created successfully.")
97+
if regional:
98+
print(f"{response.name} (regional topic) created successfully.")
99+
else:
100+
print(f"{response.name} (zonal topic) created successfully.")
70101
except AlreadyExists:
71102
print(f"{topic_path} already exists.")
72103
# [END pubsublite_create_topic]
@@ -80,9 +111,11 @@ def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_parti
80111
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
81112
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
82113
parser.add_argument("topic_id", help="Your topic ID")
114+
parser.add_argument("reservation_id", help="Your reservation ID")
83115
parser.add_argument(
84116
"num_partitions", type=int, help="Number of partitions in the topic"
85117
)
118+
parser.add_argument("regional", type=bool, help="Regional topic or not")
86119

87120
args = parser.parse_args()
88121

@@ -91,5 +124,7 @@ def create_lite_topic(project_number, cloud_region, zone_id, topic_id, num_parti
91124
args.cloud_region,
92125
args.zone_id,
93126
args.topic_id,
127+
args.reservation_id,
94128
args.num_partitions,
129+
args.regional,
95130
)

samples/snippets/delete_lite_topic_example.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
"""This application demonstrates how to delete a topic with the Pub/Sub
18-
Lite API. For more information, see the root level README.md and the
19-
documentation at https://cloud.google.com/pubsub/lite/docs/topics.
17+
"""This application demonstrates how to delete both a regional and zonal topic
18+
with the Pub/Sub Lite API. For more information, see the root level README.md
19+
and the documentation at https://cloud.google.com/pubsub/lite/docs/topics.
2020
"""
2121

2222
import argparse
2323

2424

25-
def delete_lite_topic(project_number, cloud_region, zone_id, topic_id):
25+
def delete_lite_topic(project_number, cloud_region, zone_id, topic_id, regional):
2626
# [START pubsublite_delete_topic]
2727
from google.api_core.exceptions import NotFound
2828
from google.cloud.pubsublite import AdminClient
@@ -33,15 +33,26 @@ def delete_lite_topic(project_number, cloud_region, zone_id, topic_id):
3333
# cloud_region = "us-central1"
3434
# zone_id = "a"
3535
# topic_id = "your-topic-id"
36+
# regional = True
3637

3738
cloud_region = CloudRegion(cloud_region)
38-
location = CloudZone(cloud_region, zone_id)
39-
topic_path = TopicPath(project_number, location, topic_id)
39+
topic_path = None
40+
if regional:
41+
# A regional topic.
42+
topic_path = TopicPath(project_number, cloud_region, topic_id)
43+
else:
44+
# A zonal topic
45+
topic_path = TopicPath(
46+
project_number, CloudZone(cloud_region, zone_id), topic_id
47+
)
4048

4149
client = AdminClient(cloud_region)
4250
try:
4351
client.delete_topic(topic_path)
44-
print(f"{topic_path} deleted successfully.")
52+
if regional:
53+
print(f"{topic_path} (regional topic) deleted successfully.")
54+
else:
55+
print(f"{topic_path} (zonal topic) deleted successfully.")
4556
except NotFound:
4657
print(f"{topic_path} not found.")
4758
# [END pubsublite_delete_topic]
@@ -55,9 +66,14 @@ def delete_lite_topic(project_number, cloud_region, zone_id, topic_id):
5566
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
5667
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
5768
parser.add_argument("topic_id", help="Your topic ID")
69+
parser.add_argument("regional", help="Regional topic or not")
5870

5971
args = parser.parse_args()
6072

6173
delete_lite_topic(
62-
args.project_number, args.cloud_region, args.zone_id, args.topic_id,
74+
args.project_number,
75+
args.cloud_region,
76+
args.zone_id,
77+
args.topic_id,
78+
args.regional,
6379
)

samples/snippets/get_lite_topic_example.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import argparse
2323

2424

25-
def get_lite_topic(project_number, cloud_region, zone_id, topic_id):
25+
def get_lite_topic(project_number, cloud_region, zone_id, topic_id, regional):
2626
# [START pubsublite_get_topic]
2727
from google.api_core.exceptions import NotFound
2828
from google.cloud.pubsublite import AdminClient
@@ -33,9 +33,16 @@ def get_lite_topic(project_number, cloud_region, zone_id, topic_id):
3333
# cloud_region = "us-central1"
3434
# zone_id = "a"
3535
# topic_id = "your-topic-id"
36+
# regional = True
37+
38+
location = None
39+
if regional:
40+
# A region.
41+
location = CloudRegion(cloud_region)
42+
else:
43+
# A zone.
44+
location = CloudZone(CloudRegion(cloud_region), zone_id)
3645

37-
cloud_region = CloudRegion(cloud_region)
38-
location = CloudZone(cloud_region, zone_id)
3946
topic_path = TopicPath(project_number, location, topic_id)
4047

4148
client = AdminClient(cloud_region)
@@ -56,9 +63,14 @@ def get_lite_topic(project_number, cloud_region, zone_id, topic_id):
5663
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
5764
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
5865
parser.add_argument("topic_id", help="Your topic ID")
66+
parser.add_argument("regional", type=bool, help="Regional topic or not")
5967

6068
args = parser.parse_args()
6169

6270
get_lite_topic(
63-
args.project_number, args.cloud_region, args.zone_id, args.topic_id,
71+
args.project_number,
72+
args.cloud_region,
73+
args.zone_id,
74+
args.topic_id,
75+
args.regional,
6476
)

samples/snippets/list_lite_topics_example.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import argparse
2323

2424

25-
def list_lite_topics(project_number, cloud_region, zone_id):
25+
def list_lite_topics(project_number, cloud_region, zone_id, regional):
2626
# [START pubsublite_list_topics]
2727
from google.cloud.pubsublite import AdminClient
2828
from google.cloud.pubsublite.types import CloudRegion, CloudZone, LocationPath
@@ -31,9 +31,16 @@ def list_lite_topics(project_number, cloud_region, zone_id):
3131
# project_number = 1122334455
3232
# cloud_region = "us-central1"
3333
# zone_id = "a"
34+
# regional = True
35+
36+
location = None
37+
if regional:
38+
# A region.
39+
location = CloudRegion(cloud_region)
40+
else:
41+
# A zone.
42+
location = CloudZone(CloudRegion(cloud_region), zone_id)
3443

35-
cloud_region = CloudRegion(cloud_region)
36-
location = CloudZone(cloud_region, zone_id)
3744
location_path = LocationPath(project_number, location)
3845

3946
client = AdminClient(cloud_region)
@@ -53,9 +60,10 @@ def list_lite_topics(project_number, cloud_region, zone_id):
5360
parser.add_argument("project_number", help="Your Google Cloud Project Number")
5461
parser.add_argument("cloud_region", help="Your Cloud Region, e.g. 'us-central1'")
5562
parser.add_argument("zone_id", help="Your Zone ID, e.g. 'a'")
63+
parser.add_argument("regional", type=bool, help="Regional topic or not")
5664

5765
args = parser.parse_args()
5866

5967
list_lite_topics(
60-
args.project_number, args.cloud_region, args.zone_id,
68+
args.project_number, args.cloud_region, args.zone_id, args.regional,
6169
)

0 commit comments

Comments
 (0)