|
| 1 | +/* |
| 2 | + * Copyright 2021 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.admin.generated; |
| 18 | + |
| 19 | +// [START spanner_create_backup_with_encryption_key] |
| 20 | + |
| 21 | +import com.google.cloud.spanner.SpannerExceptionFactory; |
| 22 | +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; |
| 23 | +import com.google.cloud.spanner.encryption.EncryptionConfigs; |
| 24 | +import com.google.protobuf.Timestamp; |
| 25 | +import com.google.spanner.admin.database.v1.Backup; |
| 26 | +import com.google.spanner.admin.database.v1.BackupName; |
| 27 | +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; |
| 28 | +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig.EncryptionType; |
| 29 | +import com.google.spanner.admin.database.v1.CreateBackupMetadata; |
| 30 | +import com.google.spanner.admin.database.v1.CreateBackupRequest; |
| 31 | +import com.google.spanner.admin.database.v1.DatabaseName; |
| 32 | +import com.google.spanner.admin.database.v1.InstanceName; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.concurrent.ExecutionException; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import java.util.concurrent.TimeoutException; |
| 37 | +import org.threeten.bp.LocalDateTime; |
| 38 | +import org.threeten.bp.OffsetDateTime; |
| 39 | + |
| 40 | +public class CreateBackupWithEncryptionKey { |
| 41 | + |
| 42 | + static void createBackupWithEncryptionKey() throws IOException { |
| 43 | + // TODO(developer): Replace these variables before running the sample. |
| 44 | + String projectId = "my-project"; |
| 45 | + String instanceId = "my-instance"; |
| 46 | + String databaseId = "my-database"; |
| 47 | + String backupId = "my-backup"; |
| 48 | + String kmsKeyName = |
| 49 | + "projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>"; |
| 50 | + |
| 51 | + try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) { |
| 52 | + createBackupWithEncryptionKey( |
| 53 | + adminClient, |
| 54 | + projectId, |
| 55 | + instanceId, |
| 56 | + databaseId, |
| 57 | + backupId, |
| 58 | + kmsKeyName); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static Void createBackupWithEncryptionKey(DatabaseAdminClient adminClient, |
| 63 | + String projectId, String instanceId, String databaseId, String backupId, String kmsKeyName) { |
| 64 | + // Set expire time to 14 days from now. |
| 65 | + final Timestamp expireTime = |
| 66 | + Timestamp.newBuilder().setSeconds(TimeUnit.MILLISECONDS.toSeconds(( |
| 67 | + System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14)))).build(); |
| 68 | + final BackupName backupName = BackupName.of(projectId, instanceId, backupId); |
| 69 | + Backup backup = Backup.newBuilder() |
| 70 | + .setName(backupName.toString()) |
| 71 | + .setDatabase(DatabaseName.of(projectId, instanceId, databaseId).toString()) |
| 72 | + .setExpireTime(expireTime).build(); |
| 73 | + |
| 74 | + final CreateBackupRequest request = |
| 75 | + CreateBackupRequest.newBuilder() |
| 76 | + .setParent(InstanceName.of(projectId, instanceId).toString()) |
| 77 | + .setBackupId(backupId) |
| 78 | + .setBackup(backup) |
| 79 | + .setEncryptionConfig( |
| 80 | + CreateBackupEncryptionConfig.newBuilder() |
| 81 | + .setEncryptionType(EncryptionType.CUSTOMER_MANAGED_ENCRYPTION) |
| 82 | + .setKmsKeyName(kmsKeyName).build()).build(); |
| 83 | + try { |
| 84 | + System.out.println("Waiting for operation to complete..."); |
| 85 | + backup = adminClient.createBackupAsync(request).get(1200, TimeUnit.SECONDS); |
| 86 | + } catch (ExecutionException e) { |
| 87 | + // If the operation failed during execution, expose the cause. |
| 88 | + throw SpannerExceptionFactory.asSpannerException(e.getCause()); |
| 89 | + } catch (InterruptedException e) { |
| 90 | + // Throw when a thread is waiting, sleeping, or otherwise occupied, |
| 91 | + // and the thread is interrupted, either before or during the activity. |
| 92 | + throw SpannerExceptionFactory.propagateInterrupt(e); |
| 93 | + } catch (TimeoutException e) { |
| 94 | + // If the operation timed out propagates the timeout |
| 95 | + throw SpannerExceptionFactory.propagateTimeout(e); |
| 96 | + } |
| 97 | + System.out.printf( |
| 98 | + "Backup %s of size %d bytes was created at %s using encryption key %s%n", |
| 99 | + backup.getName(), |
| 100 | + backup.getSizeBytes(), |
| 101 | + LocalDateTime.ofEpochSecond( |
| 102 | + backup.getCreateTime().getSeconds(), |
| 103 | + backup.getCreateTime().getNanos(), |
| 104 | + OffsetDateTime.now().getOffset()), |
| 105 | + kmsKeyName |
| 106 | + ); |
| 107 | + |
| 108 | + return null; |
| 109 | + } |
| 110 | +} |
| 111 | +// [END spanner_create_backup_with_encryption_key] |
0 commit comments