Skip to content

Commit 35a3231

Browse files
feat(storage): Turbo Replication Samples added (GoogleCloudPlatform#1546)
* Storage: Added samples/tests for turbo replication Co-authored-by: Brent Shaffer
1 parent 3beaa75 commit 35a3231

5 files changed

+338
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2021 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/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_create_bucket_turbo_replication]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Create a Cloud Storage Bucket with Turbo Replication set to `ASYNC_TURBO`.
31+
* The bucket must be a dual-region bucket for this setting to take effect.
32+
*
33+
* @param string $bucketName The name of your Cloud Storage bucket.
34+
* @param string $location The Dual Region location where you want your bucket to reside. (Read more at https://cloud.google.com/storage/docs/locations#location-dr)
35+
*/
36+
function create_bucket_turbo_replication($bucketName, $location = 'nam4')
37+
{
38+
// $bucketName = 'my-bucket';
39+
40+
$storage = new StorageClient();
41+
$rpo = 'ASYNC_TURBO';
42+
43+
// providing a location which is a dual region location
44+
// makes sure the locationType is set to 'dual-region' implicitly
45+
// we can pass 'locationType' => 'dual-region'
46+
// to make it explicit
47+
$bucket = $storage->createBucket($bucketName, [
48+
'location' => $location,
49+
'rpo' => $rpo
50+
]);
51+
printf('Bucket with Turbo Replication set to \'ASYNC_TURBO\' created: %s' . PHP_EOL, $bucket->name());
52+
}
53+
# [END storage_create_bucket_turbo_replication]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
/**
3+
* Copyright 2021 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/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_get_rpo]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Get the bucket's Turbo Replication(rpo) setting.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function get_turbo_replication_status($bucketName)
35+
{
36+
// $bucketName = 'my-bucket';
37+
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
41+
printf(
42+
'The bucket\'s RPO value is: %s.' . PHP_EOL,
43+
$bucket->info()['rpo']
44+
);
45+
}
46+
# [END storage_get_rpo]
47+
48+
// The following 2 lines are only needed to run the samples
49+
require_once __DIR__ . '/../../testing/sample_helpers.php';
50+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
/**
3+
* Copyright 2021 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/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_set_rpo_async_turbo]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Set the bucket's Turbo Replication(rpo) setting to `ASYNC_TURBO`.
31+
* The bucket must be a dual-region bucket.
32+
*
33+
* @param string $bucketName the name of your Cloud Storage bucket.
34+
*/
35+
function set_turbo_replication_async_turbo($bucketName)
36+
{
37+
// $bucketName = 'my-bucket';
38+
39+
$storage = new StorageClient();
40+
$bucket = $storage->bucket($bucketName);
41+
$rpo = 'ASYNC_TURBO';
42+
43+
$bucket->update([
44+
'rpo' => $rpo
45+
]);
46+
47+
printf(
48+
'Turbo Replication has been set to ASYNC_TURBO for %s.' . PHP_EOL,
49+
$bucketName
50+
);
51+
}
52+
# [END storage_set_rpo_async_turbo]
53+
54+
// The following 2 lines are only needed to run the samples
55+
require_once __DIR__ . '/../../testing/sample_helpers.php';
56+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
/**
3+
* Copyright 2021 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/tree/master/storage/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Storage;
25+
26+
# [START storage_set_rpo_default]
27+
use Google\Cloud\Storage\StorageClient;
28+
29+
/**
30+
* Set the bucket's Turbo Replication(rpo) setting to `DEFAULT`.
31+
*
32+
* @param string $bucketName the name of your Cloud Storage bucket.
33+
*/
34+
function set_turbo_replication_default($bucketName)
35+
{
36+
// $bucketName = 'my-bucket';
37+
38+
$storage = new StorageClient();
39+
$bucket = $storage->bucket($bucketName);
40+
$rpo = 'DEFAULT';
41+
42+
// Updating the rpo value of a multi-region bucket to DEFAULT has no effect
43+
// and updating the rpo value of a regional bucket will throw an exception.
44+
$bucket->update([
45+
'rpo' => $rpo
46+
]);
47+
48+
printf(
49+
'Turbo Replication has been set to DEFAULT for %s.' . PHP_EOL,
50+
$bucketName
51+
);
52+
}
53+
# [END storage_set_rpo_default]
54+
55+
// The following 2 lines are only needed to run the samples
56+
require_once __DIR__ . '/../../testing/sample_helpers.php';
57+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

storage/test/TurboReplicationTest.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
/**
3+
* Copyright 2021 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+
namespace Google\Cloud\Samples\Storage\Tests;
19+
20+
use Google\Cloud\Storage\StorageClient;
21+
use Google\Cloud\TestUtils\TestTrait;
22+
use PHPUnit\Framework\TestCase;
23+
24+
/**
25+
* Unit tests for Turbo Replication(RPO)
26+
*/
27+
class TurboReplicationTest extends TestCase
28+
{
29+
use TestTrait;
30+
31+
private static $storage;
32+
private static $bucketName;
33+
private static $bucket;
34+
35+
public static function setUpBeforeClass(): void
36+
{
37+
self::$storage = new StorageClient();
38+
self::$bucketName = uniqid('samples-turbo-replication-');
39+
}
40+
41+
public static function tearDownAfterClass(): void
42+
{
43+
self::$bucket->delete();
44+
}
45+
46+
public function testCreateBucketWithTurboReplication()
47+
{
48+
$output = self::runFunctionSnippet('create_bucket_turbo_replication', [
49+
self::$bucketName,
50+
'asia1'
51+
]);
52+
53+
$this->assertStringContainsString(
54+
sprintf(
55+
'Bucket with Turbo Replication set to \'ASYNC_TURBO\' created: %s',
56+
self::$bucketName
57+
),
58+
$output
59+
);
60+
61+
self::$bucket = self::$storage->bucket(self::$bucketName);
62+
$this->assertEquals('ASYNC_TURBO', self::$bucket->info()['rpo']);
63+
}
64+
65+
/** @depends testCreateBucketWithTurboReplication */
66+
public function testGetTurboReplicationStatus()
67+
{
68+
$output = self::runFunctionSnippet('get_turbo_replication_status', [
69+
self::$bucketName,
70+
]);
71+
72+
$this->assertEquals(
73+
sprintf(
74+
'The bucket\'s RPO value is: %s.' . PHP_EOL,
75+
'ASYNC_TURBO'
76+
),
77+
$output
78+
);
79+
}
80+
81+
/** @depends testCreateBucketWithTurboReplication */
82+
public function testSetTurboReplicationStatusDefault()
83+
{
84+
$output = self::runFunctionSnippet('set_turbo_replication_default', [
85+
self::$bucketName,
86+
]);
87+
88+
$this->assertEquals(
89+
sprintf(
90+
'Turbo Replication has been set to DEFAULT for %s.' . PHP_EOL,
91+
self::$bucketName
92+
),
93+
$output
94+
);
95+
96+
self::$bucket->reload();
97+
$this->assertEquals('DEFAULT', self::$bucket->info()['rpo']);
98+
}
99+
100+
/** @depends testCreateBucketWithTurboReplication */
101+
public function testSetTurboReplicationStatusAsyncTurbo()
102+
{
103+
$output = self::runFunctionSnippet('set_turbo_replication_async_turbo', [
104+
self::$bucketName,
105+
]);
106+
107+
$this->assertEquals(
108+
sprintf(
109+
'Turbo Replication has been set to ASYNC_TURBO for %s.' . PHP_EOL,
110+
self::$bucketName
111+
),
112+
$output
113+
);
114+
115+
self::$bucket->reload();
116+
$this->assertEquals('ASYNC_TURBO', self::$bucket->info()['rpo']);
117+
}
118+
}

0 commit comments

Comments
 (0)