Skip to content

Commit 145ac20

Browse files
authored
feat(PubSub): Add CreateTopicWithAwsMskIngestion sample (#2091)
1 parent 681562f commit 145ac20

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_aws_msk_ingestion]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Creates a Pub/Sub topic with AWS MSK ingestion.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $clusterArn The Amazon Resource Name (ARN) that uniquely identifies the cluster.
36+
* @param string $mskTopic The name of the topic in the Amazon MSK cluster that Pub/Sub will import from.
37+
* @param string $awsRoleArn AWS role ARN to be used for Federated Identity authentication with Amazon MSK.
38+
* Check the Pub/Sub docs for how to set up this role and the required permissions that need to be
39+
* attached to it.
40+
* @param string $gcpServiceAccount The GCP service account to be used for Federated Identity authentication
41+
* with Amazon MSK (via a AssumeRoleWithWebIdentity call for the provided role). The aws_role_arn
42+
* must be set up with accounts.google.com:sub equals to this service account number.
43+
*/
44+
function create_topic_with_aws_msk_ingestion(
45+
string $projectId,
46+
string $topicName,
47+
string $clusterArn,
48+
string $mskTopic,
49+
string $awsRoleArn,
50+
string $gcpServiceAccount
51+
): void {
52+
$pubsub = new PubSubClient([
53+
'projectId' => $projectId,
54+
]);
55+
56+
$topic = $pubsub->createTopic($topicName, [
57+
'ingestionDataSourceSettings' => [
58+
'aws_msk' => [
59+
'cluster_arn' => $clusterArn,
60+
'topic' => $mskTopic,
61+
'aws_role_arn' => $awsRoleArn,
62+
'gcp_service_account' => $gcpServiceAccount
63+
]
64+
]
65+
]);
66+
67+
printf('Topic created: %s' . PHP_EOL, $topic->name());
68+
}
69+
# [END pubsub_create_topic_with_aws_msk_ingestion]
70+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
71+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ public function testUpdateTopicType()
624624
$this->assertMatchesRegularExpression('/Topic updated:/', $output);
625625
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
626626
}
627+
627628
public function testCreateTopicWithCloudStorageIngestion()
628629
{
629630
$this->requireEnv('PUBSUB_EMULATOR_HOST');
@@ -648,4 +649,28 @@ public function testCreateTopicWithCloudStorageIngestion()
648649
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
649650
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
650651
}
652+
653+
public function testCreateTopicWithAwsMskIngestion()
654+
{
655+
$this->requireEnv('PUBSUB_EMULATOR_HOST');
656+
657+
$topic = 'test-topic-' . rand();
658+
$output = $this->runFunctionSnippet('create_topic_with_aws_msk_ingestion', [
659+
self::$projectId,
660+
$topic,
661+
'arn:aws:kafka:us-east-1:111111111111:cluster/fake-cluster-name/11111111-1111-1',
662+
'fake-msk-topic-name',
663+
self::$awsRoleArn,
664+
self::$gcpServiceAccount
665+
]);
666+
$this->assertMatchesRegularExpression('/Topic created:/', $output);
667+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
668+
669+
$output = $this->runFunctionSnippet('delete_topic', [
670+
self::$projectId,
671+
$topic,
672+
]);
673+
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
674+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
675+
}
651676
}

0 commit comments

Comments
 (0)