Skip to content

Commit b93c87d

Browse files
authored
feat(PubSub): Add create_topic_with_confluent_cloud_ingestion sample (#2094)
1 parent 145ac20 commit b93c87d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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_confluent_cloud_ingestion]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Creates a Pub/Sub topic with Confluent Cloud ingestion.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $bootstrapServer The address of the bootstrap server. The format is url:port.
36+
* @param string $clusterId The id of the cluster.
37+
* @param string $confluentTopic The name of the topic in the Confluent Cloud cluster that Pub/Sub will import from.
38+
* @param string $identityPoolId The id of the identity pool to be used for Federated Identity authentication with Confluent Cloud.
39+
* @param string $gcpServiceAccount The GCP service account to be used for Federated Identity authentication with identity_pool_id.
40+
*/
41+
function create_topic_with_confluent_cloud_ingestion(
42+
string $projectId,
43+
string $topicName,
44+
string $bootstrapServer,
45+
string $clusterId,
46+
string $confluentTopic,
47+
string $identityPoolId,
48+
string $gcpServiceAccount
49+
): void {
50+
$pubsub = new PubSubClient([
51+
'projectId' => $projectId,
52+
]);
53+
54+
$topic = $pubsub->createTopic($topicName, [
55+
'ingestionDataSourceSettings' => [
56+
'confluent_cloud' => [
57+
'bootstrap_server' => $bootstrapServer,
58+
'cluster_id' => $clusterId,
59+
'topic' => $confluentTopic,
60+
'identity_pool_id' => $identityPoolId,
61+
'gcp_service_account' => $gcpServiceAccount
62+
]
63+
]
64+
]);
65+
66+
printf('Topic created: %s' . PHP_EOL, $topic->name());
67+
}
68+
# [END pubsub_create_topic_with_confluent_cloud_ingestion]
69+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
70+
\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
@@ -673,4 +673,29 @@ public function testCreateTopicWithAwsMskIngestion()
673673
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
674674
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
675675
}
676+
677+
public function testCreateTopicWithConfluentCloudIngestion()
678+
{
679+
$this->requireEnv('PUBSUB_EMULATOR_HOST');
680+
681+
$topic = 'test-topic-' . rand();
682+
$output = $this->runFunctionSnippet('create_topic_with_confluent_cloud_ingestion', [
683+
self::$projectId,
684+
$topic,
685+
'fake-bootstrap-server-id.us-south1.gcp.confluent.cloud:9092',
686+
'fake-cluster-id',
687+
'fake-confluent-topic-name',
688+
'fake-identity-pool-id',
689+
self::$gcpServiceAccount
690+
]);
691+
$this->assertMatchesRegularExpression('/Topic created:/', $output);
692+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
693+
694+
$output = $this->runFunctionSnippet('delete_topic', [
695+
self::$projectId,
696+
$topic,
697+
]);
698+
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
699+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
700+
}
676701
}

0 commit comments

Comments
 (0)