Skip to content

Commit de27ff5

Browse files
authored
feat: add subscription detach sample (GoogleCloudPlatform#1150)
1 parent 89e0eb6 commit de27ff5

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

pubsub/api/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"require": {
33
"php": ">=5.4",
4-
"google/cloud-pubsub": "^1.5",
4+
"google/cloud-pubsub": "^1.27",
55
"symfony/console": " ^3.0"
66
},
77
"autoload": {
@@ -11,6 +11,7 @@
1111
"src/create_push_subscription.php",
1212
"src/delete_subscription.php",
1313
"src/delete_topic.php",
14+
"src/detach_subscription.php",
1415
"src/get_subscription_policy.php",
1516
"src/get_topic_policy.php",
1617
"src/list_subscriptions.php",

pubsub/api/pubsub.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
->addOption('topic', null, InputOption::VALUE_REQUIRED, 'The topic for the subscription (when using --create).')
4141
->addOption('endpoint', null, InputOption::VALUE_REQUIRED, 'An optional endpoint for push subscriptions.')
4242
->addOption('delete', null, InputOption::VALUE_NONE, 'Delete the subscription.')
43+
->addOption('detach', null, InputOption::VALUE_NONE, 'Detach the subscription.')
4344
->setCode(function ($input, $output) {
4445
$projectId = $input->getArgument('project');
4546
$subscriptionName = $input->getArgument('subscription');
@@ -56,6 +57,8 @@
5657
}
5758
} elseif ($input->getOption('delete')) {
5859
delete_subscription($projectId, $subscriptionName);
60+
} elseif ($input->getOption('detach')) {
61+
detach_subscription($projectId, $subscriptionName);
5962
} else {
6063
pull_messages($projectId, $subscriptionName);
6164
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC
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/master/pubsub/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\PubSub;
25+
26+
# [START pubsub_detach_subscription]
27+
use Google\Cloud\PubSub\PubSubClient;
28+
29+
/**
30+
* Detach a Pub/Sub subscription from a topic.
31+
*
32+
* @param string $projectId The Google project ID.
33+
* @param string $subscriptionName The Pub/Sub subscription name.
34+
*/
35+
function detach_subscription($projectId, $subscriptionName)
36+
{
37+
$pubsub = new PubSubClient([
38+
'projectId' => $projectId,
39+
]);
40+
$subscription = $pubsub->subscription($subscriptionName);
41+
$subscription->detach();
42+
43+
printf('Subscription detached: %s' . PHP_EOL, $subscription->name());
44+
}
45+
# [END pubsub_detach_subscription]

pubsub/api/test/pubsubTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,40 @@ public function testCreateAndDeletePushSubscription()
231231
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
232232
}
233233

234+
public function testCreateAndDetachSubscription()
235+
{
236+
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');
237+
$subscription = 'testdetachsubsxyz-' . rand();
238+
$output = $this->runCommand('subscription', [
239+
'subscription' => $subscription,
240+
'--topic' => $topic,
241+
'--create' => true,
242+
'project' => self::$projectId,
243+
]);
244+
245+
$this->assertRegExp('/Subscription created:/', $output);
246+
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
247+
248+
$output = $this->runCommand('subscription', [
249+
'subscription' => $subscription,
250+
'--detach' => true,
251+
'project' => self::$projectId,
252+
]);
253+
254+
$this->assertRegExp('/Subscription detached:/', $output);
255+
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
256+
257+
// delete test resource
258+
$output = $this->runCommand('subscription', [
259+
'subscription' => $subscription,
260+
'--delete' => true,
261+
'project' => self::$projectId,
262+
]);
263+
264+
$this->assertRegExp('/Subscription deleted:/', $output);
265+
$this->assertRegExp(sprintf('/%s/', $subscription), $output);
266+
}
267+
234268
public function testPullMessages()
235269
{
236270
$topic = $this->requireEnv('GOOGLE_PUBSUB_TOPIC');

0 commit comments

Comments
 (0)