Skip to content

Commit

Permalink
feat(PubSub): Add CPS to GCS sample (#1874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajupazhamayil authored Oct 15, 2023
1 parent d98b3ef commit 7829067
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
Binary file modified .kokoro/secrets.sh.enc
Binary file not shown.
2 changes: 1 addition & 1 deletion pubsub/api/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"google/cloud-pubsub": "^1.39",
"google/cloud-pubsub": "^1.46",
"rg/avro-php": "^2.0.1||^3.0.0"
}
}
53 changes: 53 additions & 0 deletions pubsub/api/src/create_cloud_storage_subscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
/**
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/blob/main/pubsub/api/README.md
*/

namespace Google\Cloud\Samples\PubSub;

# [START pubsub_create_cloud_storage_subscription]
use Google\Cloud\PubSub\PubSubClient;

/**
* Creates a Pub/Sub GCS subscription.
*
* @param string $projectId The Google project ID.
* @param string $topicName The Pub/Sub topic name.
* @param string $subscriptionName The Pub/Sub subscription name.
* @param string $bucket The Cloud Storage bucket name without any prefix like "gs://".
*/
function create_cloud_storage_subscription($projectId, $topicName, $subscriptionName, $bucket)
{
$pubsub = new PubSubClient([
'projectId' => $projectId,
]);
$topic = $pubsub->topic($topicName);
$subscription = $topic->subscription($subscriptionName);
$config = ['bucket' => $bucket];
$subscription->create([
'cloudStorageConfig' => $config
]);

printf('Subscription created: %s' . PHP_EOL, $subscription->name());
}
# [END pubsub_create_cloud_storage_subscription]
require_once __DIR__ . '/../../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
25 changes: 25 additions & 0 deletions pubsub/api/test/pubsubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,31 @@ public function testCreateAndDeleteBigQuerySubscription()
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
}

public function testCreateAndDeleteStorageSubscription()
{
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
$subscription = 'test-subscription-' . rand();
$bucket = $this->requireEnv('GOOGLE_PUBSUB_STORAGE_BUCKET');

$output = $this->runFunctionSnippet('create_cloud_storage_subscription', [
self::$projectId,
$topic,
$subscription,
$bucket,
]);

$this->assertMatchesRegularExpression('/Subscription created:/', $output);
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);

$output = $this->runFunctionSnippet('delete_subscription', [
self::$projectId,
$subscription,
]);

$this->assertMatchesRegularExpression('/Subscription deleted:/', $output);
$this->assertMatchesRegularExpression(sprintf('/%s/', $subscription), $output);
}

public function testCreateAndDetachSubscription()
{
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
Expand Down

0 comments on commit 7829067

Please sign in to comment.