Skip to content

Commit 857755b

Browse files
committed
feat(PubSub): Add create_topic_with_kinesis_ingestion sample
1 parent ff3d936 commit 857755b

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2025 Google LLC.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* For instructions on how to run the full sample:
21+
*
22+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
23+
*/
24+
25+
namespace Google\Cloud\Samples\PubSub;
26+
27+
# [START pubsub_create_topic_with_kinesis_ingestion]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Creates a Pub/Sub topic with AWS Kinesis ingestion.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $streamArn The Kinesis stream ARN to ingest data from.
36+
* @param string $consumerArn The Kinesis consumer ARN to used for ingestion in Enhanced Fan-Out mode. The consumer must be already created and ready to be used.
37+
* @param string $awsRoleArn AWS role ARN to be used for Federated Identity authentication with Kinesis. Check the Pub/Sub docs for how to set up this role and the required permissions that need to be attached to it.
38+
* @param string $gcpServiceAccount The GCP service account to be used for Federated Identity authentication with Kinesis (via a AssumeRoleWithWebIdentity call for the provided role). The $awsRoleArn must be set up with accounts.google.com:sub equals to this service account number.
39+
*/
40+
function create_topic_with_kinesis_ingestion(
41+
string $projectId,
42+
string $topicName,
43+
string $streamArn,
44+
string $consumerArn,
45+
string $awsRoleArn,
46+
string $gcpServiceAccount
47+
): void {
48+
$pubsub = new PubSubClient([
49+
'projectId' => $projectId,
50+
]);
51+
52+
$topic = $pubsub->createTopic($topicName, [
53+
'ingestionDataSourceSettings' => [
54+
'aws_kinesis' => [
55+
'stream_arn' => $streamArn,
56+
'consumer_arn' => $consumerArn,
57+
'aws_role_arn' => $awsRoleArn,
58+
'gcp_service_account' => $gcpServiceAccount
59+
]
60+
]
61+
]);
62+
63+
printf('Topic created: %s' . PHP_EOL, $topic->name());
64+
}
65+
# [END pubsub_create_topic_with_kinesis_ingestion]
66+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
67+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,4 +487,28 @@ public function testPublishAndSubscribeWithOrderingKeys()
487487
$this->assertMatchesRegularExpression('/Created subscription with ordering/', $output);
488488
$this->assertMatchesRegularExpression('/\"enableMessageOrdering\":true/', $output);
489489
}
490+
491+
public function testCreateTopicWithKinesisIngestion()
492+
{
493+
$this->requireEnv('PUBSUB_EMULATOR_HOST');
494+
495+
$topic = 'test-topic-' . rand();
496+
$output = $this->runFunctionSnippet('create_topic_with_kinesis_ingestion', [
497+
self::$projectId,
498+
$topic,
499+
'arn:aws:kinesis:us-west-2:111111111111:stream/fake-stream-name',
500+
'arn:aws:kinesis:us-west-2:111111111111:stream/fake-stream-name/consumer/consumer-1:1111111111',
501+
self::$awsRoleArn,
502+
self::$gcpServiceAccount
503+
]);
504+
$this->assertMatchesRegularExpression('/Topic created:/', $output);
505+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
506+
507+
$output = $this->runFunctionSnippet('delete_topic', [
508+
self::$projectId,
509+
$topic,
510+
]);
511+
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
512+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
513+
}
490514
}

0 commit comments

Comments
 (0)