Skip to content

Commit 7829067

Browse files
feat(PubSub): Add CPS to GCS sample (GoogleCloudPlatform#1874)
1 parent d98b3ef commit 7829067

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

.kokoro/secrets.sh.enc

69 Bytes
Binary file not shown.

pubsub/api/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-pubsub": "^1.39",
3+
"google/cloud-pubsub": "^1.46",
44
"rg/avro-php": "^2.0.1||^3.0.0"
55
}
66
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
/**
3+
* Copyright 2023 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_create_cloud_storage_subscription]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/**
30+
* Creates a Pub/Sub GCS subscription.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $topicName The Pub/Sub topic name.
34+
* @param string $subscriptionName The Pub/Sub subscription name.
35+
* @param string $bucket The Cloud Storage bucket name without any prefix like "gs://".
36+
*/
37+
function create_cloud_storage_subscription($projectId, $topicName, $subscriptionName, $bucket)
38+
{
39+
$pubsub = new PubSubClient([
40+
'projectId' => $projectId,
41+
]);
42+
$topic = $pubsub->topic($topicName);
43+
$subscription = $topic->subscription($subscriptionName);
44+
$config = ['bucket' => $bucket];
45+
$subscription->create([
46+
'cloudStorageConfig' => $config
47+
]);
48+
49+
printf('Subscription created: %s' . PHP_EOL, $subscription->name());
50+
}
51+
# [END pubsub_create_cloud_storage_subscription]
52+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
53+
\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
@@ -309,6 +309,31 @@ public function testCreateAndDeleteBigQuerySubscription()
309309
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
310310
}
311311

312+
public function testCreateAndDeleteStorageSubscription()
313+
{
314+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
315+
$subscription = 'test-subscription-' . rand();
316+
$bucket = $this->requireEnv('GOOGLE_PUBSUB_STORAGE_BUCKET');
317+
318+
$output = $this->runFunctionSnippet('create_cloud_storage_subscription', [
319+
self::$projectId,
320+
$topic,
321+
$subscription,
322+
$bucket,
323+
]);
324+
325+
$this->assertMatchesRegularExpression('/Subscription created:/', $output);
326+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
327+
328+
$output = $this->runFunctionSnippet('delete_subscription', [
329+
self::$projectId,
330+
$subscription,
331+
]);
332+
333+
$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
334+
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
335+
}
336+
312337
public function testCreateAndDetachSubscription()
313338
{
314339
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');

0 commit comments

Comments
 (0)