|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.spanner; |
| 18 | + |
| 19 | +// [START spanner_restore_backup_with_MR_CMEK] |
| 20 | + |
| 21 | +import static com.google.spanner.admin.database.v1.RestoreDatabaseEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION; |
| 22 | + |
| 23 | +import com.google.cloud.spanner.Spanner; |
| 24 | +import com.google.cloud.spanner.SpannerExceptionFactory; |
| 25 | +import com.google.cloud.spanner.SpannerOptions; |
| 26 | +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
| 27 | +import com.google.common.collect.ImmutableList; |
| 28 | +import com.google.spanner.admin.database.v1.*; |
| 29 | +import java.util.concurrent.ExecutionException; |
| 30 | + |
| 31 | +public class RestoreBackupWithMultiRegionEncryptionKey { |
| 32 | + |
| 33 | + static void restoreBackupWithMultiRegionEncryptionKey() { |
| 34 | + // TODO(developer): Replace these variables before running the sample. |
| 35 | + String projectId = "my-project"; |
| 36 | + String instanceId = "my-instance"; |
| 37 | + String databaseId = "my-database"; |
| 38 | + String backupId = "my-backup"; |
| 39 | + String[] kmsKeyNames = |
| 40 | + new String[] { |
| 41 | + "projects/" + projectId + "/locations/<location1>/keyRings/<keyRing>/cryptoKeys/<keyId>", |
| 42 | + "projects/" + projectId + "/locations/<location2>/keyRings/<keyRing>/cryptoKeys/<keyId>", |
| 43 | + "projects/" + projectId + "/locations/<location3>/keyRings/<keyRing>/cryptoKeys/<keyId>" |
| 44 | + }; |
| 45 | + |
| 46 | + try (Spanner spanner = |
| 47 | + SpannerOptions.newBuilder().setProjectId(projectId).build().getService(); |
| 48 | + DatabaseAdminClient adminClient = spanner.createDatabaseAdminClient()) { |
| 49 | + restoreBackupWithMultiRegionEncryptionKey( |
| 50 | + adminClient, projectId, instanceId, backupId, databaseId, kmsKeyNames); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + static Void restoreBackupWithMultiRegionEncryptionKey( |
| 55 | + DatabaseAdminClient adminClient, |
| 56 | + String projectId, |
| 57 | + String instanceId, |
| 58 | + String backupId, |
| 59 | + String restoreId, |
| 60 | + String[] kmsKeyNames) { |
| 61 | + RestoreDatabaseRequest request = |
| 62 | + RestoreDatabaseRequest.newBuilder() |
| 63 | + .setParent(InstanceName.of(projectId, instanceId).toString()) |
| 64 | + .setDatabaseId(restoreId) |
| 65 | + .setBackup(BackupName.of(projectId, instanceId, backupId).toString()) |
| 66 | + .setEncryptionConfig( |
| 67 | + RestoreDatabaseEncryptionConfig.newBuilder() |
| 68 | + .setEncryptionType(CUSTOMER_MANAGED_ENCRYPTION) |
| 69 | + .addAllKmsKeyNames(ImmutableList.copyOf(kmsKeyNames))) |
| 70 | + .build(); |
| 71 | + Database database; |
| 72 | + try { |
| 73 | + System.out.println("Waiting for operation to complete..."); |
| 74 | + database = adminClient.restoreDatabaseAsync(request).get(); |
| 75 | + ; |
| 76 | + } catch (ExecutionException e) { |
| 77 | + // If the operation failed during execution, expose the cause. |
| 78 | + throw SpannerExceptionFactory.asSpannerException(e.getCause()); |
| 79 | + } catch (InterruptedException e) { |
| 80 | + // Throw when a thread is waiting, sleeping, or otherwise occupied, |
| 81 | + // and the thread is interrupted, either before or during the activity. |
| 82 | + throw SpannerExceptionFactory.propagateInterrupt(e); |
| 83 | + } |
| 84 | + |
| 85 | + System.out.printf( |
| 86 | + "Database %s restored to %s from backup %s using encryption keys %s%n", |
| 87 | + database.getRestoreInfo().getBackupInfo().getSourceDatabase(), |
| 88 | + database.getName(), |
| 89 | + database.getRestoreInfo().getBackupInfo().getBackup(), |
| 90 | + database.getEncryptionConfig().getKmsKeyNamesList()); |
| 91 | + return null; |
| 92 | + } |
| 93 | +} |
| 94 | +// [END spanner_restore_backup_with_MR_CMEK] |
0 commit comments