-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: samples and tests for backup Admin APIs and overall spanner Adm…
…in APIs. (#2882) * fix: prevent illegal negative timeout values into thread sleep() method while retrying exceptions in unit tests. * For details on issue see - #2206 * Fixing lint issues. * chore: copy samples and rewrite create backup. * chore: fix code. * chore: fix code. * chore: fix code. * chore: fix error. * fix: all compilation errors. * fix: more compilation issues. * fix: all compile issues. * chore: compile and test PgSpannerSample. * fix: all issues with PgSpannerSample. * chore: compile and fix SpannerSample/SpannerSampleIT. * chore: fix kms configs and ITs. * chore: fix cancel backup tests. * Update samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java Co-authored-by: Knut Olav Løite <[email protected]> * chore: add log statements in tests. * chore: fix lint errors and fix comments. * chore: fix lint errors. * chore: fix lint errors. * chore: fix backup samples for restore use-case. * fix: fix restore/list backup tests. * chore: fix PgSpannerSample sample and test. * chore: fix delete backup test. * chore: fix lint errors. * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore: add IT for CopyBackupSample. * chore: fix lint errors. * chore: refactor test for delete backup. --------- Co-authored-by: Knut Olav Løite <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
704ad74
commit de13636
Showing
14 changed files
with
5,523 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright 2022 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner.admin.generated; | ||
|
||
// [START spanner_copy_backup] | ||
|
||
import com.google.cloud.Timestamp; | ||
import com.google.cloud.spanner.SpannerException; | ||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.spanner.admin.database.v1.Backup; | ||
import com.google.spanner.admin.database.v1.BackupName; | ||
import com.google.spanner.admin.database.v1.InstanceName; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneId; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class CopyBackupSample { | ||
|
||
static void copyBackup() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String sourceBackupId = "my-backup"; | ||
String destinationBackupId = "my-destination-backup"; | ||
|
||
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { | ||
copyBackup(databaseAdminClient, projectId, instanceId, sourceBackupId, destinationBackupId); | ||
} | ||
} | ||
|
||
static void copyBackup( | ||
DatabaseAdminClient databaseAdminClient, | ||
String projectId, | ||
String instanceId, | ||
String sourceBackupId, | ||
String destinationBackupId) { | ||
|
||
Timestamp expireTime = | ||
Timestamp.ofTimeMicroseconds( | ||
TimeUnit.MICROSECONDS.convert( | ||
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), | ||
TimeUnit.MILLISECONDS)); | ||
|
||
// Initiate the request which returns an OperationFuture. | ||
System.out.println("Copying backup [" + destinationBackupId + "]..."); | ||
Backup destinationBackup; | ||
try { | ||
// Creates a copy of an existing backup. | ||
// Wait for the backup operation to complete. | ||
destinationBackup = databaseAdminClient.copyBackupAsync( | ||
InstanceName.of(projectId, instanceId), destinationBackupId, | ||
BackupName.of(projectId, instanceId, sourceBackupId), expireTime.toProto()).get(); | ||
System.out.println("Copied backup [" + destinationBackup.getName() + "]"); | ||
} catch (ExecutionException e) { | ||
throw (SpannerException) e.getCause(); | ||
} catch (InterruptedException e) { | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} | ||
// Load the metadata of the new backup from the server. | ||
destinationBackup = databaseAdminClient.getBackup(destinationBackup.getName()); | ||
System.out.println( | ||
String.format( | ||
"Backup %s of size %d bytes was copied at %s for version of database at %s", | ||
destinationBackup.getName(), | ||
destinationBackup.getSizeBytes(), | ||
OffsetDateTime.ofInstant( | ||
Instant.ofEpochSecond(destinationBackup.getCreateTime().getSeconds(), | ||
destinationBackup.getCreateTime().getNanos()), | ||
ZoneId.systemDefault()), | ||
OffsetDateTime.ofInstant( | ||
Instant.ofEpochSecond(destinationBackup.getVersionTime().getSeconds(), | ||
destinationBackup.getVersionTime().getNanos()), | ||
ZoneId.systemDefault()))); | ||
} | ||
} | ||
// [END spanner_copy_backup] |
111 changes: 111 additions & 0 deletions
111
...pets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.spanner.admin.generated; | ||
|
||
// [START spanner_create_backup_with_encryption_key] | ||
|
||
import com.google.cloud.spanner.SpannerExceptionFactory; | ||
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; | ||
import com.google.cloud.spanner.encryption.EncryptionConfigs; | ||
import com.google.protobuf.Timestamp; | ||
import com.google.spanner.admin.database.v1.Backup; | ||
import com.google.spanner.admin.database.v1.BackupName; | ||
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; | ||
import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig.EncryptionType; | ||
import com.google.spanner.admin.database.v1.CreateBackupMetadata; | ||
import com.google.spanner.admin.database.v1.CreateBackupRequest; | ||
import com.google.spanner.admin.database.v1.DatabaseName; | ||
import com.google.spanner.admin.database.v1.InstanceName; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import org.threeten.bp.LocalDateTime; | ||
import org.threeten.bp.OffsetDateTime; | ||
|
||
public class CreateBackupWithEncryptionKey { | ||
|
||
static void createBackupWithEncryptionKey() throws IOException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "my-project"; | ||
String instanceId = "my-instance"; | ||
String databaseId = "my-database"; | ||
String backupId = "my-backup"; | ||
String kmsKeyName = | ||
"projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>"; | ||
|
||
try (DatabaseAdminClient adminClient = DatabaseAdminClient.create()) { | ||
createBackupWithEncryptionKey( | ||
adminClient, | ||
projectId, | ||
instanceId, | ||
databaseId, | ||
backupId, | ||
kmsKeyName); | ||
} | ||
} | ||
|
||
static Void createBackupWithEncryptionKey(DatabaseAdminClient adminClient, | ||
String projectId, String instanceId, String databaseId, String backupId, String kmsKeyName) { | ||
// Set expire time to 14 days from now. | ||
final Timestamp expireTime = | ||
Timestamp.newBuilder().setSeconds(TimeUnit.MILLISECONDS.toSeconds(( | ||
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14)))).build(); | ||
final BackupName backupName = BackupName.of(projectId, instanceId, backupId); | ||
Backup backup = Backup.newBuilder() | ||
.setName(backupName.toString()) | ||
.setDatabase(DatabaseName.of(projectId, instanceId, databaseId).toString()) | ||
.setExpireTime(expireTime).build(); | ||
|
||
final CreateBackupRequest request = | ||
CreateBackupRequest.newBuilder() | ||
.setParent(InstanceName.of(projectId, instanceId).toString()) | ||
.setBackupId(backupId) | ||
.setBackup(backup) | ||
.setEncryptionConfig( | ||
CreateBackupEncryptionConfig.newBuilder() | ||
.setEncryptionType(EncryptionType.CUSTOMER_MANAGED_ENCRYPTION) | ||
.setKmsKeyName(kmsKeyName).build()).build(); | ||
try { | ||
System.out.println("Waiting for operation to complete..."); | ||
backup = adminClient.createBackupAsync(request).get(1200, TimeUnit.SECONDS); | ||
} catch (ExecutionException e) { | ||
// If the operation failed during execution, expose the cause. | ||
throw SpannerExceptionFactory.asSpannerException(e.getCause()); | ||
} catch (InterruptedException e) { | ||
// Throw when a thread is waiting, sleeping, or otherwise occupied, | ||
// and the thread is interrupted, either before or during the activity. | ||
throw SpannerExceptionFactory.propagateInterrupt(e); | ||
} catch (TimeoutException e) { | ||
// If the operation timed out propagates the timeout | ||
throw SpannerExceptionFactory.propagateTimeout(e); | ||
} | ||
System.out.printf( | ||
"Backup %s of size %d bytes was created at %s using encryption key %s%n", | ||
backup.getName(), | ||
backup.getSizeBytes(), | ||
LocalDateTime.ofEpochSecond( | ||
backup.getCreateTime().getSeconds(), | ||
backup.getCreateTime().getNanos(), | ||
OffsetDateTime.now().getOffset()), | ||
kmsKeyName | ||
); | ||
|
||
return null; | ||
} | ||
} | ||
// [END spanner_create_backup_with_encryption_key] |
Oops, something went wrong.