Skip to content

Commit 83dc9ff

Browse files
docs(samples): Add Create Topic with Kinesis IngestionDataSourceSettings Sample (#1120)
Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent c1c6fa5 commit 83dc9ff

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

samples/snippets/publisher.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,51 @@ def create_topic(project_id: str, topic_id: str) -> None:
6060
# [END pubsub_create_topic]
6161

6262

63+
def create_topic_kinesis_ingestion(
64+
project_id: str,
65+
topic_id: str,
66+
stream_arn: str,
67+
consumer_arn: str,
68+
aws_role_arn: str,
69+
gcp_service_account: str,
70+
) -> None:
71+
"""Create a new Pub/Sub topic with AWS Kinesis Ingestion Settings."""
72+
# [START pubsub_quickstart_create_topic_kinesis_ingestion]
73+
# [START pubsub_create_topic_kinesis_ingestion]
74+
from google.cloud import pubsub_v1
75+
from google.pubsub_v1.types import Topic
76+
from google.pubsub_v1.types import IngestionDataSourceSettings
77+
78+
# TODO(developer)
79+
# project_id = "your-project-id"
80+
# topic_id = "your-topic-id"
81+
# stream_arn = "your-stream-arn"
82+
# consumer_arn = "your-consumer-arn"
83+
# aws_role_arn = "your-aws-role-arn"
84+
# gcp_service_account = "your-gcp-service-account"
85+
86+
publisher = pubsub_v1.PublisherClient()
87+
topic_path = publisher.topic_path(project_id, topic_id)
88+
89+
request = Topic(
90+
name=topic_path,
91+
ingestion_data_source_settings=IngestionDataSourceSettings(
92+
aws_kinesis=IngestionDataSourceSettings.AwsKinesis(
93+
stream_arn=stream_arn,
94+
consumer_arn=consumer_arn,
95+
aws_role_arn=aws_role_arn,
96+
gcp_service_account=gcp_service_account,
97+
)
98+
),
99+
)
100+
101+
topic = publisher.create_topic(request=request)
102+
103+
print(f"Created topic: {topic.name} with AWS Kinesis Ingestion Settings")
104+
# [END pubsub_quickstart_create_topic_kinesis_ingestion]
105+
# [END pubsub_create_topic_kinesis_ingestion]
106+
107+
63108
def delete_topic(project_id: str, topic_id: str) -> None:
64109
"""Deletes an existing Pub/Sub topic."""
65110
# [START pubsub_delete_topic]
@@ -430,6 +475,15 @@ def detach_subscription(project_id: str, subscription_id: str) -> None:
430475
create_parser = subparsers.add_parser("create", help=create_topic.__doc__)
431476
create_parser.add_argument("topic_id")
432477

478+
create_topic_kinesis_ingestion_parser = subparsers.add_parser(
479+
"create_kinesis_ingestion", help=create_topic_kinesis_ingestion.__doc__
480+
)
481+
create_topic_kinesis_ingestion_parser.add_argument("topic_id")
482+
create_topic_kinesis_ingestion_parser.add_argument("stream_arn")
483+
create_topic_kinesis_ingestion_parser.add_argument("consumer_arn")
484+
create_topic_kinesis_ingestion_parser.add_argument("aws_role_arn")
485+
create_topic_kinesis_ingestion_parser.add_argument("gcp_service_account")
486+
433487
delete_parser = subparsers.add_parser("delete", help=delete_topic.__doc__)
434488
delete_parser.add_argument("topic_id")
435489

@@ -490,6 +544,15 @@ def detach_subscription(project_id: str, subscription_id: str) -> None:
490544
list_topics(args.project_id)
491545
elif args.command == "create":
492546
create_topic(args.project_id, args.topic_id)
547+
elif args.command == "create_kinesis_ingestion":
548+
create_topic_kinesis_ingestion(
549+
args.project_id,
550+
args.topic_id,
551+
args.stream_arn,
552+
args.consumer_arn,
553+
args.aws_role_arn,
554+
args.gcp_service_account,
555+
)
493556
elif args.command == "delete":
494557
delete_topic(args.project_id, args.topic_id)
495558
elif args.command == "publish":

samples/snippets/publisher_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,38 @@ def test_create(
124124
assert f"Created topic: {topic_path}" in out
125125

126126

127+
def test_create_kinesis_ingestion(
128+
publisher_client: pubsub_v1.PublisherClient, capsys: CaptureFixture[str]
129+
) -> None:
130+
# The scope of `topic_path` is limited to this function.
131+
topic_path = publisher_client.topic_path(PROJECT_ID, TOPIC_ID)
132+
133+
# Outside of automated CI tests, these values must be of actual AWS resources for the test to pass.
134+
stream_arn = "arn:aws:kinesis:us-west-2:111111111111:stream/fake-stream-name"
135+
consumer_arn = "arn:aws:kinesis:us-west-2:111111111111:stream/fake-stream-name/consumer/consumer-1:1111111111"
136+
aws_role_arn = "arn:aws:iam::111111111111:role/fake-role-name"
137+
gcp_service_account = (
138+
139+
)
140+
141+
try:
142+
publisher_client.delete_topic(request={"topic": topic_path})
143+
except NotFound:
144+
pass
145+
146+
publisher.create_topic_kinesis_ingestion(
147+
PROJECT_ID,
148+
TOPIC_ID,
149+
stream_arn,
150+
consumer_arn,
151+
aws_role_arn,
152+
gcp_service_account,
153+
)
154+
155+
out, _ = capsys.readouterr()
156+
assert f"Created topic: {topic_path} with AWS Kinesis Ingestion Settings" in out
157+
158+
127159
def test_list(topic_path: str, capsys: CaptureFixture[str]) -> None:
128160
publisher.list_topics(PROJECT_ID)
129161
out, _ = capsys.readouterr()

samples/snippets/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
google-cloud-pubsub==2.19.0
1+
google-cloud-pubsub==2.20.1
22
avro==1.11.3
33
protobuf===4.24.4; python_version == '3.7'
44
protobuf==4.25.1; python_version >= '3.8'

0 commit comments

Comments
 (0)