Skip to content

Commit 07df6a1

Browse files
authored
feat(PubSub): Add create_topic_with_azure_event_hubs_ingestion sample (#2093)
1 parent b93c87d commit 07df6a1

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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_azure_event_hubs_ingestion]
28+
use Google\Cloud\PubSub\PubSubClient;
29+
30+
/**
31+
* Creates a Pub/Sub topic with Azure Event Hubs ingestion.
32+
*
33+
* @param string $projectId The Google project ID.
34+
* @param string $topicName The Pub/Sub topic name.
35+
* @param string $resourceGroup The name of the resource group within the azure subscription.
36+
* @param string $namespace The name of the Event Hubs namespace.
37+
* @param string $eventHub The name of the Event Hub.
38+
* @param string $clientId The client id of the Azure application that is being used to authenticate Pub/Sub.
39+
* @param string $tenantId The tenant id of the Azure application that is being used to authenticate Pub/Sub.
40+
* @param string $subscriptionId The Azure subscription id.
41+
* @param string $gcpServiceAccount The GCP service account to be used for Federated Identity authentication.
42+
*/
43+
function create_topic_with_azure_event_hubs_ingestion(
44+
string $projectId,
45+
string $topicName,
46+
string $resourceGroup,
47+
string $namespace,
48+
string $eventHub,
49+
string $clientId,
50+
string $tenantId,
51+
string $subscriptionId,
52+
string $gcpServiceAccount
53+
): void {
54+
$pubsub = new PubSubClient([
55+
'projectId' => $projectId,
56+
]);
57+
58+
$topic = $pubsub->createTopic($topicName, [
59+
'ingestionDataSourceSettings' => [
60+
'azure_event_hubs' => [
61+
'resource_group' => $resourceGroup,
62+
'namespace' => $namespace,
63+
'event_hub' => $eventHub,
64+
'client_id' => $clientId,
65+
'tenant_id' => $tenantId,
66+
'subscription_id' => $subscriptionId,
67+
'gcp_service_account' => $gcpServiceAccount
68+
]
69+
]
70+
]);
71+
72+
printf('Topic created: %s' . PHP_EOL, $topic->name());
73+
}
74+
# [END pubsub_create_topic_with_azure_event_hubs_ingestion]
75+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
76+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

pubsub/api/test/pubsubTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,31 @@ public function testCreateTopicWithConfluentCloudIngestion()
698698
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
699699
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
700700
}
701+
702+
public function testCreateTopicWithAzureEventHubsIngestion()
703+
{
704+
$this->requireEnv('PUBSUB_EMULATOR_HOST');
705+
706+
$topic = 'test-topic-' . rand();
707+
$output = $this->runFunctionSnippet('create_topic_with_azure_event_hubs_ingestion', [
708+
self::$projectId,
709+
$topic,
710+
'fake-resource-group',
711+
'fake-namespace',
712+
'fake-event-hub',
713+
'11111111-1111-1111-1111-11111111111',
714+
'22222222-2222-2222-2222-222222222222',
715+
'33333333-3333-3333-3333-333333333333',
716+
self::$gcpServiceAccount
717+
]);
718+
$this->assertMatchesRegularExpression('/Topic created:/', $output);
719+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
720+
721+
$output = $this->runFunctionSnippet('delete_topic', [
722+
self::$projectId,
723+
$topic,
724+
]);
725+
$this->assertMatchesRegularExpression('/Topic deleted:/', $output);
726+
$this->assertMatchesRegularExpression(sprintf('/%s/', $topic), $output);
727+
}
701728
}

0 commit comments

Comments
 (0)