Skip to content

Commit abfc6b8

Browse files
committed
Adding additional functions for bucket lock samples
1 parent 95fba1a commit abfc6b8

File tree

6 files changed

+146
-4
lines changed

6 files changed

+146
-4
lines changed

storage/composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud-storage": "^1.5.2",
3+
"google/cloud-storage": "^1.9.0",
44
"paragonie/random_compat": "^2.0",
55
"symfony/console": " ^3.0"
66
},
@@ -35,6 +35,8 @@
3535
"src/get_object_acl.php",
3636
"src/get_object_acl_for_entity.php",
3737
"src/get_requester_pays_status.php",
38+
"src/get_retention_policy.php",
39+
"src/get_default_event_based_hold.php",
3840
"src/list_buckets.php",
3941
"src/list_objects.php",
4042
"src/list_objects_with_prefix.php",
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
/**
3+
* Copyright 2018 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/tree/master/storage/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_default_event_based_hold]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Enables a default event based hold for a bucket.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*
34+
* @return void
35+
*/
36+
function get_default_event_based_hold($bucketName)
37+
{
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
41+
if ($bucket->info()['defaultEventBasedHold']) {
42+
printf('Default event-based hold is enabled for ' . $bucketName . PHP_EOL);
43+
} else {
44+
printf('Default event-based hold is not enabled for ' . $bucketName . PHP_EOL);
45+
}
46+
47+
}
48+
# [END storage_get_default_event_based_hold]

storage/src/get_retention_policy.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/**
3+
* Copyright 2018 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/tree/master/storage/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_retention_policy]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Gets a bucket's retention policy.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*
34+
* @return void
35+
*/
36+
function get_retention_policy($bucketName)
37+
{
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
$bucket->reload();
41+
42+
printf('Retention Policy for ' . $bucketName . PHP_EOL);
43+
printf('Retention Period: ' . $bucket->info()['retentionPolicy']['retentionPeriod'] . PHP_EOL);
44+
if ($bucket->info()['retentionPolicy']['isLocked']) {
45+
printf('Retention Policy is locked' . PHP_EOL);
46+
}
47+
if ($bucket->info()['retentionPolicy']['effectiveTime']) {
48+
printf('Effective Time: ' . $bucket->info()['retentionPolicy']['effectiveTime'] . PHP_EOL);
49+
}
50+
}
51+
# [END storage_get_retention_policy]

storage/src/object_metadata.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ function object_metadata($bucketName, $objectName)
5252
printf('Crc32c: %s' . PHP_EOL, $info['crc32c']);
5353
printf('MD5 Hash: %s' . PHP_EOL, $info['md5Hash']);
5454
printf('Content-type: %s' . PHP_EOL, $info['contentType']);
55+
printf("Temporary hold: " . ($info['temporaryHold'] ? "enabled" : "disabled") . PHP_EOL);
56+
printf("Event based hold: " . ($info['eventBasedHold'] ? "enabled" : "disabled") . PHP_EOL);
57+
if ($info['retentionExpirationTime']) {
58+
printf("retentionExpirationTime: " . $info['retentionExpirationTime'] . PHP_EOL);
59+
}
5560
if (isset($info['metadata'])) {
5661
printf('Metadata: %s', print_r($info['metadata'], true));
5762
}

storage/storage.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,12 @@
164164
->addOption('set-retention-policy', null, InputOption::VALUE_NONE, 'Set the retention policy')
165165
->addOption('remove-retention-policy', null, InputOption::VALUE_NONE, 'Remove the retention policy')
166166
->addOption('lock-retention-policy', null, InputOption::VALUE_NONE, 'Lock the retention policy')
167+
->addOption('get-retention-policy', null, InputOption::VALUE_NONE, 'Gets the retention policy')
167168
->addOption('set-event-based-hold', null, InputOption::VALUE_NONE, 'Set an event based hold')
168169
->addOption('release-event-based-hold', null, InputOption::VALUE_NONE, 'Release an event based hold')
169170
->addOption('enable-default-event-based-hold', null, InputOption::VALUE_NONE, 'Enable default event based hold')
170171
->addOption('disable-default-event-based-hold', null, InputOption::VALUE_NONE, 'Disable default event based hold')
172+
->addOption('get-default-event-based-hold', null, InputOption::VALUE_NONE, 'Gets default event based hold')
171173
->addOption('set-temporary-hold', null, InputOption::VALUE_NONE, 'Set a temporary hold')
172174
->addOption('release-temporary-hold', null, InputOption::VALUE_NONE, 'Release a temporary hold')
173175
->setCode(function ($input, $output) {
@@ -177,10 +179,14 @@
177179
remove_retention_policy($bucketName);
178180
} elseif ($input->getOption('lock-retention-policy')) {
179181
lock_retention_policy($bucketName);
182+
} elseif ($input->getOption('get-retention-policy')) {
183+
get_retention_policy($bucketName);
180184
} elseif ($input->getOption('enable-default-event-based-hold')) {
181185
enable_default_event_based_hold($bucketName);
182186
} elseif ($input->getOption('disable-default-event-based-hold')) {
183187
disable_default_event_based_hold($bucketName);
188+
} elseif ($input->getOption('get-default-event-based-hold')) {
189+
get_default_event_based_hold($bucketName);
184190
} elseif ($input->getOption('set-retention-policy')) {
185191
if ($retentionPeriod = $input->getArgument('retention-period')) {
186192
set_retention_policy($bucketName, $retentionPeriod);

storage/test/BucketLockCommandTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,20 @@ public function testRetentionPolicyNoLock()
8080
['interactive' => false]
8181
);
8282
$this->bucket->reload();
83+
$effectiveTime = $this->bucket->info()['retentionPolicy']['effectiveTime'];
8384

84-
$this->assertFalse($this->bucket->info()['retentionPolicy']['isLocked']);
85-
$this->assertNotNull($this->bucket->info()['retentionPolicy']['effectiveTime']);
85+
$this->assertNull($this->bucket->info()['retentionPolicy']['isLocked']);
86+
$this->assertNotNull($effectiveTime);
8687
$this->assertEquals($this->bucket->info()['retentionPolicy']['retentionPeriod'], $retentionPeriod);
8788

89+
$this->commandTester->execute(
90+
[
91+
'bucket' => $this->bucket->name(),
92+
'--get-retention-policy' => true,
93+
],
94+
['interactive' => false]
95+
);
96+
8897
$this->uploadObject();
8998
$this->assertNotNull($this->object->info()['retentionExpirationTime']);
9099

@@ -101,6 +110,9 @@ public function testRetentionPolicyNoLock()
101110

102111
$outputString = <<
103112
Bucket {$this->bucket->name()} retention period set for $retentionPeriod seconds
113+
Retention Policy for {$this->bucket->name()}
114+
Retention Period: 5
115+
Effective Time: $effectiveTime
104116
Removed bucket {$this->bucket->name()} retention policy
105117
106118
EOF;
@@ -142,7 +154,7 @@ public function testRetentionPolicyLock()
142154
$this->expectOutputString($outputString);
143155
}
144156

145-
public function testEnableDisableDefaultEventBasedHold()
157+
public function testEnableDisableGetDefaultEventBasedHold()
146158
{
147159
$this->commandTester->execute(
148160
[
@@ -155,6 +167,14 @@ public function testEnableDisableDefaultEventBasedHold()
155167

156168
$this->assertTrue($this->bucket->info()['defaultEventBasedHold']);
157169

170+
$this->commandTester->execute(
171+
[
172+
'bucket' => $this->bucket->name(),
173+
'--get-default-event-based-hold' => true,
174+
],
175+
['interactive' => false]
176+
);
177+
158178
$this->uploadObject();
159179
$this->assertTrue($this->object->info()['eventBasedHold']);
160180

@@ -179,10 +199,20 @@ public function testEnableDisableDefaultEventBasedHold()
179199
$this->bucket->reload();
180200
$this->assertFalse($this->bucket->info()['defaultEventBasedHold']);
181201

202+
$this->commandTester->execute(
203+
[
204+
'bucket' => $this->bucket->name(),
205+
'--get-default-event-based-hold' => true,
206+
],
207+
['interactive' => false]
208+
);
209+
182210
$outputString = <<
183211
Default event based hold was enabled for {$this->bucket->name()}
212+
Default event-based hold is enabled for {$this->bucket->name()}
184213
Event based hold was released for {$this->object->name()}
185214
Default event based hold was disabled for {$this->bucket->name()}
215+
Default event-based hold is not enabled for {$this->bucket->name()}
186216
187217
EOF;
188218
$this->expectOutputString($outputString);

0 commit comments

Comments
 (0)