Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: samples and tests for backup Admin APIs and overall spanner Admin APIs. #2882

Merged
merged 44 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
edc5bbf
fix: prevent illegal negative timeout values into thread sleep() meth…
arpan14 Feb 6, 2023
49a85df
Merge pull request #1 from arpan14/retryerror
arpan14 Feb 8, 2023
4cd497b
Fixing lint issues.
arpan14 Feb 8, 2023
4a6aa8e
Merge branch 'googleapis:main' into main
arpan14 Mar 13, 2023
b2aa09d
Merge branch 'googleapis:main' into main
arpan14 Mar 15, 2023
8d6d71e
Merge branch 'googleapis:main' into main
arpan14 May 9, 2023
77e6e7d
Merge branch 'googleapis:main' into main
arpan14 Jul 17, 2023
e8b7fad
Merge branch 'googleapis:main' into main
arpan14 Jul 25, 2023
8aa84e1
Merge branch 'googleapis:main' into main
arpan14 Oct 10, 2023
57fd405
Merge branch 'googleapis:main' into main
arpan14 Oct 27, 2023
1253563
Merge branch 'googleapis:main' into main
arpan14 Nov 20, 2023
d4f6a60
Merge branch 'googleapis:main' into main
arpan14 Dec 15, 2023
3efaf7c
Merge branch 'googleapis:main' into main
arpan14 Dec 26, 2023
f41b39f
Merge branch 'googleapis:main' into main
arpan14 Jan 3, 2024
3775996
chore: copy samples and rewrite create backup.
arpan14 Jan 10, 2024
c9d60df
chore: fix code.
arpan14 Jan 10, 2024
8989e7c
chore: fix code.
arpan14 Jan 10, 2024
1fedc6e
Merge branch 'googleapis:main' into admin-backup-samples
arpan14 Feb 13, 2024
4e84671
chore: fix code.
arpan14 Jan 11, 2024
3c0d0f6
chore: fix error.
arpan14 Feb 13, 2024
8e51bde
fix: all compilation errors.
arpan14 Feb 15, 2024
fb9ad16
fix: more compilation issues.
arpan14 Feb 15, 2024
0182569
fix: all compile issues.
arpan14 Feb 15, 2024
aa8faca
chore: compile and test PgSpannerSample.
arpan14 Feb 16, 2024
b806ef1
fix: all issues with PgSpannerSample.
arpan14 Feb 16, 2024
7d5943d
chore: compile and fix SpannerSample/SpannerSampleIT.
arpan14 Feb 17, 2024
db5cbca
chore: fix kms configs and ITs.
arpan14 Feb 19, 2024
9240fbc
chore: fix cancel backup tests.
arpan14 Feb 19, 2024
e752d32
Update samples/snippets/src/main/java/com/example/spanner/admin/gener…
arpan14 Feb 19, 2024
ca59fb6
chore: add log statements in tests.
arpan14 Feb 20, 2024
fcf4457
chore: fix lint errors and fix comments.
arpan14 Feb 20, 2024
89834e0
chore: fix lint errors.
arpan14 Feb 20, 2024
478c343
chore: fix lint errors.
arpan14 Feb 20, 2024
d719f7f
chore: fix backup samples for restore use-case.
arpan14 Feb 22, 2024
d324ed6
fix: fix restore/list backup tests.
arpan14 Feb 22, 2024
837034a
chore: fix PgSpannerSample sample and test.
arpan14 Feb 22, 2024
f7623ed
chore: fix delete backup test.
arpan14 Feb 23, 2024
3f90c1a
chore: fix lint errors.
arpan14 Feb 23, 2024
bb7a6cc
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 23, 2024
6f36d93
Merge branch 'main' into admin-backup-samples
arpan14 Feb 23, 2024
01058bc
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] Feb 23, 2024
0dcce23
chore: add IT for CopyBackupSample.
arpan14 Feb 23, 2024
fa88a45
chore: fix lint errors.
arpan14 Feb 23, 2024
e3630bc
chore: refactor test for delete backup.
arpan14 Feb 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: copy samples and rewrite create backup.
  • Loading branch information
arpan14 committed Jan 10, 2024
commit 37759969d694c86f8d67a25ebaa9b0ebc4f1f888
@@ -0,0 +1,101 @@
/*
* 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.api.gax.longrunning.OperationFuture;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Backup;
import com.google.cloud.spanner.BackupId;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerException;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.spanner.admin.database.v1.CopyBackupMetadata;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class CopyBackupSample {
static void copyBackup() {
// 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 (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
DatabaseAdminClient databaseAdminClient = spanner.getDatabaseAdminClient();
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));
// Creates a copy of an existing backup.
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
Backup destinationBackup =
databaseAdminClient
.newBackupBuilder(BackupId.of(projectId, instanceId, destinationBackupId))
.setExpireTime(expireTime)
.build();

// Initiate the request which returns an OperationFuture.
System.out.println("Copying backup [" + destinationBackup.getId() + "]...");
OperationFuture<Backup, CopyBackupMetadata> operation =
databaseAdminClient.copyBackup(
BackupId.of(projectId, instanceId, sourceBackupId), destinationBackup);
try {
// Wait for the backup operation to complete.
destinationBackup = operation.get();
System.out.println("Copied backup [" + destinationBackup.getId() + "]");
} 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 = destinationBackup.reload();
System.out.println(
String.format(
"Backup %s of size %d bytes was copied at %s for version of database at %s",
destinationBackup.getId().getName(),
destinationBackup.getSize(),
LocalDateTime.ofEpochSecond(
destinationBackup.getProto().getCreateTime().getSeconds(),
destinationBackup.getProto().getCreateTime().getNanos(),
OffsetDateTime.now().getOffset()),
arpan14 marked this conversation as resolved.
Show resolved Hide resolved
LocalDateTime.ofEpochSecond(
destinationBackup.getProto().getVersionTime().getSeconds(),
destinationBackup.getProto().getVersionTime().getNanos(),
OffsetDateTime.now().getOffset())));
return;
}
}
// [END spanner_copy_backup]
@@ -0,0 +1,107 @@
/*
* 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.api.gax.longrunning.OperationFuture;
import com.google.cloud.Timestamp;
import com.google.cloud.spanner.Backup;
import com.google.cloud.spanner.BackupId;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.encryption.EncryptionConfigs;
import com.google.spanner.admin.database.v1.CreateBackupMetadata;
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 InterruptedException {
// 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 (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
createBackupWithEncryptionKey(
adminClient,
projectId,
instanceId,
databaseId,
backupId,
kmsKeyName);
}
}

static Void createBackupWithEncryptionKey(DatabaseAdminClient adminClient,
String projectId, String instanceId, String databaseId, String backupId, String kmsKeyName)
throws InterruptedException {
// Set expire time to 14 days from now.
final Timestamp expireTime = Timestamp.ofTimeMicroseconds(TimeUnit.MICROSECONDS.convert(
System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14), TimeUnit.MILLISECONDS));
final Backup backupToCreate = adminClient
.newBackupBuilder(BackupId.of(projectId, instanceId, backupId))
.setDatabase(DatabaseId.of(projectId, instanceId, databaseId))
.setExpireTime(expireTime)
.setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(kmsKeyName))
.build();
final OperationFuture<Backup, CreateBackupMetadata> operation = adminClient
.createBackup(backupToCreate);

Backup backup;
try {
System.out.println("Waiting for operation to complete...");
backup = operation.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.getId().getName(),
backup.getSize(),
LocalDateTime.ofEpochSecond(
backup.getProto().getCreateTime().getSeconds(),
backup.getProto().getCreateTime().getNanos(),
OffsetDateTime.now().getOffset()),
kmsKeyName
);

return null;
}
}
// [END spanner_create_backup_with_encryption_key]
@@ -0,0 +1,100 @@
/*
* 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_database_with_encryption_key]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.spanner.Database;
import com.google.cloud.spanner.DatabaseAdminClient;
import com.google.cloud.spanner.DatabaseId;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerExceptionFactory;
import com.google.cloud.spanner.SpannerOptions;
import com.google.cloud.spanner.encryption.EncryptionConfigs;
import com.google.spanner.admin.database.v1.CreateDatabaseMetadata;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateDatabaseWithEncryptionKey {

static void createDatabaseWithEncryptionKey() {
// TODO(developer): Replace these variables before running the sample.
String projectId = "my-project";
String instanceId = "my-instance";
String databaseId = "my-database";
String kmsKeyName =
"projects/" + projectId + "/locations/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>";

try (Spanner spanner =
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
DatabaseAdminClient adminClient = spanner.getDatabaseAdminClient();
createDatabaseWithEncryptionKey(
adminClient,
projectId,
instanceId,
databaseId,
kmsKeyName);
}
}

static void createDatabaseWithEncryptionKey(DatabaseAdminClient adminClient,
String projectId, String instanceId, String databaseId, String kmsKeyName) {
final Database databaseToCreate = adminClient
.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId))
.setEncryptionConfig(EncryptionConfigs.customerManagedEncryption(kmsKeyName))
.build();
final OperationFuture<Database, CreateDatabaseMetadata> operation = adminClient
.createDatabase(databaseToCreate, Arrays.asList(
"CREATE TABLE Singers ("
+ " SingerId INT64 NOT NULL,"
+ " FirstName STRING(1024),"
+ " LastName STRING(1024),"
+ " SingerInfo BYTES(MAX)"
+ ") PRIMARY KEY (SingerId)",
"CREATE TABLE Albums ("
+ " SingerId INT64 NOT NULL,"
+ " AlbumId INT64 NOT NULL,"
+ " AlbumTitle STRING(MAX)"
+ ") PRIMARY KEY (SingerId, AlbumId),"
+ " INTERLEAVE IN PARENT Singers ON DELETE CASCADE"
));
try {
System.out.println("Waiting for operation to complete...");
Database createdDatabase = operation.get(120, TimeUnit.SECONDS);

System.out.printf(
"Database %s created with encryption key %s%n",
createdDatabase.getId(),
createdDatabase.getEncryptionConfig().getKmsKeyName()
);
} 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);
}
}
}
// [END spanner_create_database_with_encryption_key]