Skip to content

Commit de13636

Browse files
arpan14olavloitegcf-owl-bot[bot]
authored
docs: samples and tests for backup Admin APIs and overall spanner Admin 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>
1 parent 704ad74 commit de13636

File tree

14 files changed

+5523
-6
lines changed

14 files changed

+5523
-6
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
515515
| Add Numeric Column Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/AddNumericColumnSample.java) |
516516
| Alter Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterSequenceSample.java) |
517517
| Alter Table With Foreign Key Delete Cascade Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/AlterTableWithForeignKeyDeleteCascadeSample.java) |
518+
| Copy Backup Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/CopyBackupSample.java) |
519+
| Create Backup With Encryption Key | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateBackupWithEncryptionKey.java) |
520+
| Create Database With Encryption Key | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithEncryptionKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithEncryptionKey.java) |
518521
| Create Database With Version Retention Period Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithVersionRetentionPeriodSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateDatabaseWithVersionRetentionPeriodSample.java) |
519522
| Create Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceConfigSample.java) |
520523
| Create Instance Example | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/CreateInstanceExample.java) |
@@ -537,6 +540,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-spanner/tree/
537540
| Pg Create Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCreateSequenceSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/PgCreateSequenceSample.java) |
538541
| Pg Drop Sequence Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgDropSequenceSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/PgDropSequenceSample.java) |
539542
| Pg Interleaved Table Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgInterleavedTableSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/PgInterleavedTableSample.java) |
543+
| Pg Spanner Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/PgSpannerSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/PgSpannerSample.java) |
544+
| Restore Backup With Encryption Key | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/RestoreBackupWithEncryptionKey.java) |
545+
| Spanner Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/SpannerSample.java) |
540546
| Update Database Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseSample.java) |
541547
| Update Database With Default Leader Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateDatabaseWithDefaultLeaderSample.java) |
542548
| Update Instance Config Sample | [source code](https://github.com/googleapis/java-spanner/blob/main/samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-spanner&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/spanner/admin/generated/UpdateInstanceConfigSample.java) |

samples/install-without-bom/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@
147147
<spanner.test.instance.mr>java-client-mr-integration-test</spanner.test.instance.mr>
148148
<spanner.test.instance.config>nam11</spanner.test.instance.config>
149149
<spanner.test.key.location>us-east1</spanner.test.key.location>
150-
<spanner.test.key.ring>cmek-test-key-ring</spanner.test.key.ring>
151-
<spanner.test.key.name>cmek-test-key</spanner.test.key.name>
150+
<spanner.test.key.ring>java-client-integration-test-cmek-ring</spanner.test.key.ring>
151+
<spanner.test.key.name>java-client-integration-test-cmek-key</spanner.test.key.name>
152152
<spanner.sample.database>mysample</spanner.sample.database>
153153
<spanner.quickstart.database>quick-db</spanner.quickstart.database>
154154
</systemPropertyVariables>

samples/snapshot/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@
146146
<spanner.test.instance.mr>java-client-mr-integration-test</spanner.test.instance.mr>
147147
<spanner.test.instance.config>nam11</spanner.test.instance.config>
148148
<spanner.test.key.location>us-east1</spanner.test.key.location>
149-
<spanner.test.key.ring>cmek-test-key-ring</spanner.test.key.ring>
150-
<spanner.test.key.name>cmek-test-key</spanner.test.key.name>
149+
<spanner.test.key.ring>java-client-integration-test-cmek-ring</spanner.test.key.ring>
150+
<spanner.test.key.name>java-client-integration-test-cmek-key</spanner.test.key.name>
151151
<spanner.sample.database>mysample</spanner.sample.database>
152152
<spanner.sample.instance>mysample-instance</spanner.sample.instance>
153153
<spanner.quickstart.database>quick-db</spanner.quickstart.database>

samples/snippets/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@
182182
<spanner.test.instance.mr>java-client-mr-integration-test</spanner.test.instance.mr>
183183
<spanner.test.instance.config>nam11</spanner.test.instance.config>
184184
<spanner.test.key.location>us-east1</spanner.test.key.location>
185-
<spanner.test.key.ring>cmek-test-key-ring</spanner.test.key.ring>
186-
<spanner.test.key.name>cmek-test-key</spanner.test.key.name>
185+
<spanner.test.key.ring>java-client-integration-test-cmek-ring</spanner.test.key.ring>
186+
<spanner.test.key.name>java-client-integration-test-cmek-key</spanner.test.key.name>
187187
<spanner.sample.database>mysample</spanner.sample.database>
188188
<spanner.quickstart.database>quick-db</spanner.quickstart.database>
189189
</systemPropertyVariables>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2022 Google Inc.
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_copy_backup]
20+
21+
import com.google.cloud.Timestamp;
22+
import com.google.cloud.spanner.SpannerException;
23+
import com.google.cloud.spanner.SpannerExceptionFactory;
24+
import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient;
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.InstanceName;
28+
import java.io.IOException;
29+
import java.time.Instant;
30+
import java.time.OffsetDateTime;
31+
import java.time.ZoneId;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
35+
public class CopyBackupSample {
36+
37+
static void copyBackup() throws IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String projectId = "my-project";
40+
String instanceId = "my-instance";
41+
String sourceBackupId = "my-backup";
42+
String destinationBackupId = "my-destination-backup";
43+
44+
try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) {
45+
copyBackup(databaseAdminClient, projectId, instanceId, sourceBackupId, destinationBackupId);
46+
}
47+
}
48+
49+
static void copyBackup(
50+
DatabaseAdminClient databaseAdminClient,
51+
String projectId,
52+
String instanceId,
53+
String sourceBackupId,
54+
String destinationBackupId) {
55+
56+
Timestamp expireTime =
57+
Timestamp.ofTimeMicroseconds(
58+
TimeUnit.MICROSECONDS.convert(
59+
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14),
60+
TimeUnit.MILLISECONDS));
61+
62+
// Initiate the request which returns an OperationFuture.
63+
System.out.println("Copying backup [" + destinationBackupId + "]...");
64+
Backup destinationBackup;
65+
try {
66+
// Creates a copy of an existing backup.
67+
// Wait for the backup operation to complete.
68+
destinationBackup = databaseAdminClient.copyBackupAsync(
69+
InstanceName.of(projectId, instanceId), destinationBackupId,
70+
BackupName.of(projectId, instanceId, sourceBackupId), expireTime.toProto()).get();
71+
System.out.println("Copied backup [" + destinationBackup.getName() + "]");
72+
} catch (ExecutionException e) {
73+
throw (SpannerException) e.getCause();
74+
} catch (InterruptedException e) {
75+
throw SpannerExceptionFactory.propagateInterrupt(e);
76+
}
77+
// Load the metadata of the new backup from the server.
78+
destinationBackup = databaseAdminClient.getBackup(destinationBackup.getName());
79+
System.out.println(
80+
String.format(
81+
"Backup %s of size %d bytes was copied at %s for version of database at %s",
82+
destinationBackup.getName(),
83+
destinationBackup.getSizeBytes(),
84+
OffsetDateTime.ofInstant(
85+
Instant.ofEpochSecond(destinationBackup.getCreateTime().getSeconds(),
86+
destinationBackup.getCreateTime().getNanos()),
87+
ZoneId.systemDefault()),
88+
OffsetDateTime.ofInstant(
89+
Instant.ofEpochSecond(destinationBackup.getVersionTime().getSeconds(),
90+
destinationBackup.getVersionTime().getNanos()),
91+
ZoneId.systemDefault())));
92+
}
93+
}
94+
// [END spanner_copy_backup]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)