Skip to content

Commit 5ad66db

Browse files
feat(spanner): add MR CMEK samples (#2044)
* Add create_database_with_MR_CMEK.php * Add testCreateDatabaseWithMRCMEK to spannerTest.php * Add create_backup_with_MR_CMEK * Add testCreateBackupWithMRCMEK to spannerBackupTest.php * Add restore_backup_with_MR_CMEK * Add testRestoreBackupWithMRCMEK to spannerBackupTest.php * Rename create_backup_with_MR_CMEK to create_backup_with_MR_CMEK.php * Rename restore_backup_with_MR_CMEK to restore_backup_with_MR_CMEK.php * Add copy_backup_with_MR_CMEK.php * Add testCopyBackupWithMRCMEK to spannerBackupTest.php * Update copy_backup_with_MR_CMEK.php * Update copy_backup_with_MR_CMEK.php * Update create_database_with_MR_CMEK.php Add indentation * Update copy_backup_with_MR_CMEK.php * Update copy_backup_with_MR_CMEK.php * Update create_database_with_MR_CMEK.php * Update restore_backup_with_MR_CMEK.php * Update create_backup_with_MR_CMEK.php Use encryptionInformation * Update copy_backup_with_MR_CMEK.php Use encryptionInformation * Update print_firewall_rule.php formatting * Update and rename copy_backup_with_MR_CMEK.php to copy_backup_with_mr_cmek.php Change from MR_CMEK to mr_cmek * Update and rename create_backup_with_MR_CMEK.php to create_backup_with_mr_cmek.php Change from MR_CMEK to mr_cmek * Update and rename create_database_with_MR_CMEK.php to create_database_with_mr_cmek.php Change from MR_CMEK to mr_cmek * Update and rename restore_backup_with_MR_CMEK.php to restore_backup_with_mr_cmek.php Change from MR_CMEK to mr_cmek * Update spannerBackupTest.php Change from MR_CMEK to mr_cmek * Update spannerTest.php Change from MR_CMEK to mr_cmek * Update spannerTest.php Add self::$ to kmsKeyName * Update spannerBackupTest.php Add self::$ to kmsKeyName * Update spannerTest.php * Update spannerTest.php Shorten database id * Update spannerBackupTest.php Shorten names * Update spannerTest.php Use MR instance * Update spannerTest.php Add spanner client * Update spannerBackupTest.php Add mr copy instance * Update spannerTest.php Add self::$instanceConfig * Update spannerTest.php Create instance config * Update spannerBackupTest.php * Update spannerBackupTest.php --------- Co-authored-by: Brent Shaffer <[email protected]>
1 parent 3d2ba82 commit 5ad66db

File tree

6 files changed

+559
-0
lines changed

6 files changed

+559
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_copy_backup_with_MR_CMEK]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\CopyBackupRequest;
29+
use Google\Cloud\Spanner\Admin\Database\V1\CopyBackupEncryptionConfig;
30+
use Google\Protobuf\Timestamp;
31+
32+
/**
33+
* Copy a MR CMEK backup.
34+
* Example:
35+
* ```
36+
* copy_backup_with_mr_cmek($projectId, $instanceId, $sourceBackupId, $backupId, $kmsKeyNames);
37+
* ```
38+
* @param string $projectId The Google Cloud project ID.
39+
* @param string $instanceId The Spanner instance ID.
40+
* @param string $sourceBackupId The Spanner source backup ID.
41+
* @param string $backupId The Spanner backup ID.
42+
* @param string[] $kmsKeyNames The KMS keys used for encryption.
43+
*/
44+
/**
45+
* Create a copy MR CMEK backup from another source backup.
46+
* Example:
47+
* ```
48+
* copy_backup_with_mr_cmek($projectId, $destInstanceId, $destBackupId, $sourceInstanceId, $sourceBackupId, $kmsKeyNames);
49+
* ```
50+
*
51+
* @param string $projectId The Google Cloud project ID.
52+
* @param string $destInstanceId The Spanner instance ID where the copy backup will reside.
53+
* @param string $destBackupId The Spanner backup ID of the new backup to be created.
54+
* @param string $sourceInstanceId The Spanner instance ID of the source backup.
55+
* @param string $sourceBackupId The Spanner backup ID of the source.
56+
* @param string[] $kmsKeyNames The KMS keys used for encryption.
57+
*/
58+
function copy_backup_with_mr_cmek(
59+
string $projectId,
60+
string $destInstanceId,
61+
string $destBackupId,
62+
string $sourceInstanceId,
63+
string $sourceBackupId,
64+
array $kmsKeyNames
65+
): void {
66+
$databaseAdminClient = new DatabaseAdminClient();
67+
68+
$destInstanceFullName = DatabaseAdminClient::instanceName($projectId, $destInstanceId);
69+
$expireTime = new Timestamp();
70+
$expireTime->setSeconds((new \DateTime('+8 hours'))->getTimestamp());
71+
$sourceBackupFullName = DatabaseAdminClient::backupName($projectId, $sourceInstanceId, $sourceBackupId);
72+
$request = new CopyBackupRequest([
73+
'source_backup' => $sourceBackupFullName,
74+
'parent' => $destInstanceFullName,
75+
'backup_id' => $destBackupId,
76+
'expire_time' => $expireTime,
77+
'encryption_config' => new CopyBackupEncryptionConfig([
78+
'kms_key_names' => $kmsKeyNames,
79+
'encryption_type' => CopyBackupEncryptionConfig\EncryptionType::CUSTOMER_MANAGED_ENCRYPTION
80+
])
81+
]);
82+
83+
$operationResponse = $databaseAdminClient->copyBackup($request);
84+
$operationResponse->pollUntilComplete();
85+
86+
if (!$operationResponse->operationSucceeded()) {
87+
$error = $operationResponse->getError();
88+
printf('Backup not created due to error: %s.' . PHP_EOL, $error->getMessage());
89+
return;
90+
}
91+
$destBackupInfo = $operationResponse->getResult();
92+
$kmsKeyVersions = [];
93+
foreach ($destBackupInfo->getEncryptionInformation() as $encryptionInfo) {
94+
$kmsKeyVersions[] = $encryptionInfo->getKmsKeyVersion();
95+
}
96+
printf(
97+
'Backup %s of size %d bytes was copied at %d from the source backup %s using encryption keys %s' . PHP_EOL,
98+
basename($destBackupInfo->getName()),
99+
$destBackupInfo->getSizeBytes(),
100+
$destBackupInfo->getCreateTime()->getSeconds(),
101+
$sourceBackupId,
102+
print_r($kmsKeyVersions, true)
103+
);
104+
printf('Version time of the copied backup: %d' . PHP_EOL, $destBackupInfo->getVersionTime()->getSeconds());
105+
}
106+
// [END spanner_copy_backup_with_MR_CMEK]
107+
108+
// The following 2 lines are only needed to run the samples
109+
require_once __DIR__ . '/../../testing/sample_helpers.php';
110+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_create_backup_with_MR_CMEK]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Backup;
28+
use \Google\Cloud\Spanner\Admin\Database\V1\Backup\State;
29+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
30+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupEncryptionConfig;
31+
use Google\Cloud\Spanner\Admin\Database\V1\CreateBackupRequest;
32+
use Google\Cloud\Spanner\Admin\Database\V1\GetBackupRequest;
33+
use Google\Protobuf\Timestamp;
34+
35+
/**
36+
* Create a CMEK backup.
37+
* Example:
38+
* ```
39+
* create_backup_with_mr_cmek($projectId, $instanceId, $databaseId, $backupId, $kmsKeyNames);
40+
* ```
41+
*
42+
* @param string $projectId The Google Cloud project ID.
43+
* @param string $instanceId The Spanner instance ID.
44+
* @param string $databaseId The Spanner database ID.
45+
* @param string $backupId The Spanner backup ID.
46+
* @param string[] $kmsKeyNames The KMS keys used for encryption.
47+
*/
48+
function create_backup_with_mr_cmek(
49+
string $projectId,
50+
string $instanceId,
51+
string $databaseId,
52+
string $backupId,
53+
array $kmsKeyNames
54+
): void {
55+
$databaseAdminClient = new DatabaseAdminClient();
56+
$instanceFullName = DatabaseAdminClient::instanceName($projectId, $instanceId);
57+
$databaseFullName = DatabaseAdminClient::databaseName($projectId, $instanceId, $databaseId);
58+
$expireTime = new Timestamp();
59+
$expireTime->setSeconds((new \DateTime('+14 days'))->getTimestamp());
60+
$request = new CreateBackupRequest([
61+
'parent' => $instanceFullName,
62+
'backup_id' => $backupId,
63+
'encryption_config' => new CreateBackupEncryptionConfig([
64+
'kms_key_names' => $kmsKeyNames,
65+
'encryption_type' => CreateBackupEncryptionConfig\EncryptionType::CUSTOMER_MANAGED_ENCRYPTION
66+
]),
67+
'backup' => new Backup([
68+
'database' => $databaseFullName,
69+
'expire_time' => $expireTime
70+
])
71+
]);
72+
73+
$operation = $databaseAdminClient->createBackup($request);
74+
75+
print('Waiting for operation to complete...' . PHP_EOL);
76+
$operation->pollUntilComplete();
77+
78+
$request = new GetBackupRequest();
79+
$request->setName($databaseAdminClient->backupName($projectId, $instanceId, $backupId));
80+
$info = $databaseAdminClient->getBackup($request);
81+
if (State::name($info->getState()) == 'READY') {
82+
$kmsKeyVersions = [];
83+
foreach ($info->getEncryptionInformation() as $encryptionInfo) {
84+
$kmsKeyVersions[] = $encryptionInfo->getKmsKeyVersion();
85+
}
86+
printf(
87+
'Backup %s of size %d bytes was created at %d using encryption keys %s' . PHP_EOL,
88+
basename($info->getName()),
89+
$info->getSizeBytes(),
90+
$info->getCreateTime()->getSeconds(),
91+
print_r($kmsKeyVersions, true)
92+
);
93+
} else {
94+
print('Backup is not ready!' . PHP_EOL);
95+
}
96+
}
97+
// [END spanner_create_backup_with_MR_CMEK]
98+
99+
// The following 2 lines are only needed to run the samples
100+
require_once __DIR__ . '/../../testing/sample_helpers.php';
101+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_create_database_with_MR_CMEK]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\CreateDatabaseRequest;
29+
use Google\Cloud\Spanner\Admin\Database\V1\EncryptionConfig;
30+
31+
/**
32+
* Creates a MR CMEK database with tables for sample data.
33+
* Example:
34+
* ```
35+
* create_database_with_mr_cmek($projectId, $instanceId, $databaseId, $kmsKeyNames);
36+
* ```
37+
*
38+
* @param string $projectId The Google Cloud project ID.
39+
* @param string $instanceId The Spanner instance ID.
40+
* @param string $databaseId The Spanner database ID.
41+
* @param string[] $kmsKeyNames The KMS keys used for encryption.
42+
*/
43+
function create_database_with_mr_cmek(
44+
string $projectId,
45+
string $instanceId,
46+
string $databaseId,
47+
array $kmsKeyNames
48+
): void {
49+
$databaseAdminClient = new DatabaseAdminClient();
50+
$instanceName = DatabaseAdminClient::instanceName($projectId, $instanceId);
51+
52+
$createDatabaseRequest = new CreateDatabaseRequest();
53+
$createDatabaseRequest->setParent($instanceName);
54+
$createDatabaseRequest->setCreateStatement(sprintf('CREATE DATABASE `%s`', $databaseId));
55+
$createDatabaseRequest->setExtraStatements([
56+
'CREATE TABLE Singers (
57+
SingerId INT64 NOT NULL,
58+
FirstName STRING(1024),
59+
LastName STRING(1024),
60+
SingerInfo BYTES(MAX)
61+
) PRIMARY KEY (SingerId)',
62+
'CREATE TABLE Albums (
63+
SingerId INT64 NOT NULL,
64+
AlbumId INT64 NOT NULL,
65+
AlbumTitle STRING(MAX)
66+
) PRIMARY KEY (SingerId, AlbumId),
67+
INTERLEAVE IN PARENT Singers ON DELETE CASCADE'
68+
]);
69+
70+
if (!empty($kmsKeyNames)) {
71+
$encryptionConfig = new EncryptionConfig();
72+
$encryptionConfig->setKmsKeyNames($kmsKeyNames);
73+
$createDatabaseRequest->setEncryptionConfig($encryptionConfig);
74+
}
75+
76+
$operationResponse = $databaseAdminClient->createDatabase($createDatabaseRequest);
77+
printf('Waiting for operation to complete...' . PHP_EOL);
78+
$operationResponse->pollUntilComplete();
79+
80+
if ($operationResponse->operationSucceeded()) {
81+
$database = $operationResponse->getResult();
82+
printf(
83+
'Created database %s on instance %s with encryption keys %s' . PHP_EOL,
84+
$databaseId,
85+
$instanceId,
86+
print_r($database->getEncryptionConfig()->getKmsKeyNames(), true)
87+
);
88+
} else {
89+
$error = $operationResponse->getError();
90+
printf('Failed to create encrypted database: %s' . PHP_EOL, $error->getMessage());
91+
}
92+
}
93+
// [END spanner_create_database_with_MR_CMEK]
94+
95+
// The following 2 lines are only needed to run the samples
96+
require_once __DIR__ . '/../../testing/sample_helpers.php';
97+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Copyright 2024 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/main/spanner/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Spanner;
25+
26+
// [START spanner_restore_backup_with_MR_CMEK]
27+
use Google\Cloud\Spanner\Admin\Database\V1\Client\DatabaseAdminClient;
28+
use Google\Cloud\Spanner\Admin\Database\V1\RestoreDatabaseEncryptionConfig;
29+
use Google\Cloud\Spanner\Admin\Database\V1\RestoreDatabaseRequest;
30+
31+
/**
32+
* Restore a MR CMEK database from a backup.
33+
* Example:
34+
* ```
35+
* restore_backup_with_mr_cmek($projectId, $instanceId, $databaseId, $backupId, $kmsKeyNames);
36+
* ```
37+
* @param string $projectId The Google Cloud project ID.
38+
* @param string $instanceId The Spanner instance ID.
39+
* @param string $databaseId The Spanner database ID.
40+
* @param string $backupId The Spanner backup ID.
41+
* @param string[] $kmsKeyNames The KMS keys used for encryption.
42+
*/
43+
function restore_backup_with_mr_cmek(
44+
string $projectId,
45+
string $instanceId,
46+
string $databaseId,
47+
string $backupId,
48+
array $kmsKeyNames
49+
): void {
50+
$databaseAdminClient = new DatabaseAdminClient();
51+
$instanceFullName = DatabaseAdminClient::instanceName($projectId, $instanceId);
52+
$backupFullName = DatabaseAdminClient::backupName($projectId, $instanceId, $backupId);
53+
$request = new RestoreDatabaseRequest([
54+
'parent' => $instanceFullName,
55+
'database_id' => $databaseId,
56+
'backup' => $backupFullName,
57+
'encryption_config' => new RestoreDatabaseEncryptionConfig([
58+
'kms_key_names' => $kmsKeyNames,
59+
'encryption_type' => RestoreDatabaseEncryptionConfig\EncryptionType::CUSTOMER_MANAGED_ENCRYPTION
60+
])
61+
]);
62+
63+
// Create restore operation
64+
$operation = $databaseAdminClient->restoreDatabase($request);
65+
66+
print('Waiting for operation to complete...' . PHP_EOL);
67+
$operation->pollUntilComplete();
68+
69+
// Reload new database and get restore info
70+
$database = $operation->operationSucceeded() ? $operation->getResult() : null;
71+
$restoreInfo = $database->getRestoreInfo();
72+
$backupInfo = $restoreInfo->getBackupInfo();
73+
$sourceDatabase = $backupInfo->getSourceDatabase();
74+
$sourceBackup = $backupInfo->getBackup();
75+
$encryptionConfig = $database->getEncryptionConfig();
76+
printf(
77+
'Database %s restored from backup %s using encryption keys %s' . PHP_EOL,
78+
$sourceDatabase, $sourceBackup, print_r($encryptionConfig->getKmsKeyNames(), true)
79+
);
80+
}
81+
// [END spanner_restore_backup_with_MR_CMEK]
82+
83+
// The following 2 lines are only needed to run the samples
84+
require_once __DIR__ . '/../../testing/sample_helpers.php';
85+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

0 commit comments

Comments
 (0)