From 2d8d7e4f3dfa3a1eb14bb117016a784019f05fc9 Mon Sep 17 00:00:00 2001 From: Aman Agarwal Date: Tue, 17 Sep 2024 08:07:20 +0000 Subject: [PATCH 1/6] Add java sample for `CreateBackupSchedule` API. --- .../spanner/CreateBackupScheduleSample.java | 84 +++++++++++++++++++ .../spanner/CreateBackupScheduleSampleIT.java | 44 ++++++++++ 2 files changed, 128 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java new file mode 100644 index 00000000000..f6e9e7a995f --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java @@ -0,0 +1,84 @@ +/* + * Copyright 2024 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; + +// [START spanner_create_backup_schedule_sample] + +import com.google.cloud.spanner.SpannerException; +import com.google.cloud.spanner.SpannerExceptionFactory; +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.protobuf.Duration; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; +import com.google.spanner.admin.database.v1.CrontabSpec; +import com.google.spanner.admin.database.v1.DatabaseName; +import java.io.IOException; +import java.util.concurrent.ExecutionException; + +class CreateBackupScheduleSample { + + static void createBackupSchedule() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + createBackupScheduleSample(projectId, instanceId, databaseId, backupScheduleId); + } + + static void createBackupSchedule(String projectId, + String instanceId, + String databaseId, + String backupScheduleId) { + final BackupSchedule backupSchedule = BackupSchedule.newBuilder() + .setFullBackupSpec(new FullBackupSpec()) + .setRetentionDuration(Duration.newBuilder().setSeconds(60*24*7).build()); + .setSpec(BackupScheduleSpec + .newBuilder() + .setCronSpec(CrontabSpec + .newBuilder() + .setText("0 0 * * *") + .build() + ) + .build() + ) + .build(); + + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + DatabaseName databaseName = DatabaseName.of(projectId, + instanceId, + databaseId); + final BackupSchedule createdBackupSchedule = databaseAdminClient + .createBackupSchedule(CreateBackupScheduleRequest + .newBuilder() + .setParent(databaseName) + .setBackupScheduleId(backupScheduleId) + .setBackupSchedule(backupSchedule) + .build() + ) + .get(); + System.out.println(String.format("Created backup schedule: %s", + createBackupSchedule.getName())); + } catch (ExecutionException e) { + throw (SpannerException) e.getCause(); + } catch (InterruptedException e) { + throw SpannerExceptionFactory.propagateInterrupt(e); + } + } +} +// [START spanner_create_backup_schedule_sample] diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java new file mode 100644 index 00000000000..02748cb3f81 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java @@ -0,0 +1,44 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import java.util.UUID; +import org.junit.Test; + +public class CreateBackupScheduleSampleIT extends SampleTestBase { + + @Test + public void testCreateBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", uuid.randomUUID()); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleSample.createBackupSchedule(projectId, + instanceId, + databaseId, + backupScheduleId); + } finally { + // spanner.getDatabaseAdminClinet().deleteBackupSchedule(backupScheduleId); + } + } + ) + assertThat(out).contains(String.format("Created backup schedule")); + } +} \ No newline at end of file From b5d8d53b8a0adbf384dd7a6893f5463c2be97e75 Mon Sep 17 00:00:00 2001 From: Aman Agarwal Date: Wed, 18 Sep 2024 18:14:38 +0530 Subject: [PATCH 2/6] Add samples for backups schedule APIs. --- .../spanner/CreateBackupScheduleSample.java | 89 ++++++++----------- ...ateBackupScheduleWithEncryptionSample.java | 88 ++++++++++++++++++ ...CreateIncrementalBackupScheduleSample.java | 77 ++++++++++++++++ .../spanner/DeleteBackupScheduleSample.java | 53 +++++++++++ .../spanner/GetBackupScheduleSample.java | 55 ++++++++++++ .../spanner/ListBackupSchedulesSample.java | 61 +++++++++++++ .../spanner/UpdateBackupScheduleSample.java | 76 ++++++++++++++++ .../spanner/CreateBackupScheduleSampleIT.java | 46 +++++----- ...eBackupScheduleWithEncryptionSampleIT.java | 67 ++++++++++++++ ...eateIncrementalBackupScheduleSampleIT.java | 50 +++++++++++ .../spanner/DeleteBackupScheduleSampleIT.java | 50 +++++++++++ .../spanner/GetBackupScheduleSampleIT.java | 52 +++++++++++ .../spanner/ListBackupSchedulesSampleIT.java | 63 +++++++++++++ .../spanner/UpdateBackupScheduleSampleIT.java | 52 +++++++++++ 14 files changed, 809 insertions(+), 70 deletions(-) create mode 100644 samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java create mode 100644 samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java create mode 100644 samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java index f6e9e7a995f..315d53504ad 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java @@ -18,8 +18,6 @@ // [START spanner_create_backup_schedule_sample] -import com.google.cloud.spanner.SpannerException; -import com.google.cloud.spanner.SpannerExceptionFactory; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.protobuf.Duration; import com.google.spanner.admin.database.v1.BackupSchedule; @@ -27,58 +25,49 @@ import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; import com.google.spanner.admin.database.v1.CrontabSpec; import com.google.spanner.admin.database.v1.DatabaseName; +import com.google.spanner.admin.database.v1.FullBackupSpec; import java.io.IOException; -import java.util.concurrent.ExecutionException; class CreateBackupScheduleSample { - static void createBackupSchedule() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project"; - String instanceId = "my-instance"; - String databaseId = "my-database"; - String backupScheduleId = "my-backup-schedule"; - createBackupScheduleSample(projectId, instanceId, databaseId, backupScheduleId); - } + static void createBackupSchedule() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + createBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); + } + + static void createBackupSchedule(String projectId, String instanceId, + String databaseId, String backupScheduleId) + throws IOException { + final BackupSchedule backupSchedule = + BackupSchedule.newBuilder() + .setFullBackupSpec(FullBackupSpec.newBuilder().build()) + .setRetentionDuration( + Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec( + CrontabSpec.newBuilder().setText("0 0 * * *").build()) + .build()) + .build(); - static void createBackupSchedule(String projectId, - String instanceId, - String databaseId, - String backupScheduleId) { - final BackupSchedule backupSchedule = BackupSchedule.newBuilder() - .setFullBackupSpec(new FullBackupSpec()) - .setRetentionDuration(Duration.newBuilder().setSeconds(60*24*7).build()); - .setSpec(BackupScheduleSpec - .newBuilder() - .setCronSpec(CrontabSpec - .newBuilder() - .setText("0 0 * * *") - .build() - ) - .build() - ) - .build(); - - try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { - DatabaseName databaseName = DatabaseName.of(projectId, - instanceId, - databaseId); - final BackupSchedule createdBackupSchedule = databaseAdminClient - .createBackupSchedule(CreateBackupScheduleRequest - .newBuilder() - .setParent(databaseName) - .setBackupScheduleId(backupScheduleId) - .setBackupSchedule(backupSchedule) - .build() - ) - .get(); - System.out.println(String.format("Created backup schedule: %s", - createBackupSchedule.getName())); - } catch (ExecutionException e) { - throw (SpannerException) e.getCause(); - } catch (InterruptedException e) { - throw SpannerExceptionFactory.propagateInterrupt(e); - } + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + DatabaseName databaseName = + DatabaseName.of(projectId, instanceId, databaseId); + final BackupSchedule createdBackupSchedule = + databaseAdminClient.createBackupSchedule( + CreateBackupScheduleRequest.newBuilder() + .setParent(databaseName.toString()) + .setBackupScheduleId(backupScheduleId) + .setBackupSchedule(backupSchedule) + .build()); + System.out.println(String.format("Created backup schedule: %s", + createdBackupSchedule.getName())); } + } } -// [START spanner_create_backup_schedule_sample] +// [END spanner_create_backup_schedule_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java new file mode 100644 index 00000000000..16f5babaea1 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java @@ -0,0 +1,88 @@ +/* + * Copyright 2024 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; + +// [START spanner_create_backup_schedule_with_encryption_sample] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.protobuf.Duration; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig.EncryptionType; +import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; +import com.google.spanner.admin.database.v1.CrontabSpec; +import com.google.spanner.admin.database.v1.DatabaseName; +import com.google.spanner.admin.database.v1.FullBackupSpec; +import java.io.IOException; + +class CreateBackupScheduleWithEncryptionSample { + + static void createBackupScheduleWithEncryption() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + String kmsKeyName = + "projects/" + projectId + + "/locations//keyRings//cryptoKeys/"; + createBackupScheduleWithEncryption(projectId, instanceId, databaseId, + backupScheduleId, kmsKeyName); + } + + static void + createBackupScheduleWithEncryption(String projectId, String instanceId, + String databaseId, String backupScheduleId, + String kmsKeyName) throws IOException { + final CreateBackupEncryptionConfig encryptionConfig = + CreateBackupEncryptionConfig.newBuilder() + .setEncryptionType(CreateBackupEncryptionConfig.EncryptionType + .CUSTOMER_MANAGED_ENCRYPTION) + .setKmsKeyName(kmsKeyName) + .build(); + final BackupSchedule backupSchedule = + BackupSchedule.newBuilder() + .setFullBackupSpec(FullBackupSpec.newBuilder().build()) + .setRetentionDuration( + Duration.newBuilder().setSeconds(3600 * 24 * 7)) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec( + CrontabSpec.newBuilder().setText("0 0 * * *").build()) + .build()) + .setEncryptionConfig(encryptionConfig) + .build(); + + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + DatabaseName databaseName = + DatabaseName.of(projectId, instanceId, databaseId); + final BackupSchedule createdBackupSchedule = + databaseAdminClient.createBackupSchedule( + CreateBackupScheduleRequest.newBuilder() + .setParent(databaseName.toString()) + .setBackupScheduleId(backupScheduleId) + .setBackupSchedule(backupSchedule) + .build()); + System.out.println( + String.format("Created backup schedule with encryption: %s", + createdBackupSchedule.getName())); + } + } +} +// [END spanner_create_backup_schedule_with_encryption_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java new file mode 100644 index 00000000000..f47be965f25 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java @@ -0,0 +1,77 @@ +/* + * Copyright 2024 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; + +// [START spanner_create_incremental_backup_schedule_sample] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.protobuf.Duration; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; +import com.google.spanner.admin.database.v1.CrontabSpec; +import com.google.spanner.admin.database.v1.DatabaseName; +import com.google.spanner.admin.database.v1.IncrementalBackupSpec; +import java.io.IOException; + +class CreateIncrementalBackupScheduleSample { + + static void createIncrementalBackupSchedule() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + createIncrementalBackupSchedule(projectId, instanceId, databaseId, + backupScheduleId); + } + + static void + createIncrementalBackupSchedule(String projectId, String instanceId, + String databaseId, String backupScheduleId) + throws IOException { + final BackupSchedule backupSchedule = + BackupSchedule.newBuilder() + .setIncrementalBackupSpec( + IncrementalBackupSpec.newBuilder().build()) + .setRetentionDuration( + Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec( + CrontabSpec.newBuilder().setText("0 */6 * * *").build()) + .build()) + .build(); + + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + DatabaseName databaseName = + DatabaseName.of(projectId, instanceId, databaseId); + final BackupSchedule createdBackupSchedule = + databaseAdminClient.createBackupSchedule( + CreateBackupScheduleRequest.newBuilder() + .setParent(databaseName.toString()) + .setBackupScheduleId(backupScheduleId) + .setBackupSchedule(backupSchedule) + .build()); + System.out.println( + String.format("Created incremental backup schedule: %s", + createdBackupSchedule.getName())); + } + } +} +// [END spanner_create_incremental_backup_schedule_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java new file mode 100644 index 00000000000..93fe14c59f1 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java @@ -0,0 +1,53 @@ +/* + * Copyright 2024 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; + +// [START spanner_delete_backup_schedule_sample] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.spanner.admin.database.v1.BackupScheduleName; +import com.google.spanner.admin.database.v1.DeleteBackupScheduleRequest; +import java.io.IOException; + +class DeleteBackupScheduleSample { + + static void deleteBackupSchedule() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + deleteBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); + } + + static void deleteBackupSchedule(String projectId, String instanceId, + String databaseId, String backupScheduleId) + throws IOException { + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + databaseAdminClient.deleteBackupSchedule( + DeleteBackupScheduleRequest.newBuilder() + .setName(backupScheduleName.toString()) + .build()); + System.out.println(String.format("Deleted backup schedule: %s", + backupScheduleName.toString())); + } + } +} +// [END spanner_delete_backup_schedule_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java new file mode 100644 index 00000000000..2f3a4ffa2f6 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java @@ -0,0 +1,55 @@ +/* + * Copyright 2024 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; + +// [START spanner_get_backup_schedule_sample] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.BackupScheduleName; +import com.google.spanner.admin.database.v1.GetBackupScheduleRequest; +import java.io.IOException; + +class GetBackupScheduleSample { + + static void GetBackupSchedule() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + getBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); + } + + static void getBackupSchedule(String projectId, String instanceId, + String databaseId, String backupScheduleId) + throws IOException { + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + final BackupSchedule backupSchedule = + databaseAdminClient.getBackupSchedule( + GetBackupScheduleRequest.newBuilder() + .setName(backupScheduleName.toString()) + .build()); + System.out.println(String.format("Retrieved backup schedule: %s", + backupScheduleName.toString())); + } + } +} +// [END spanner_get_backup_schedule_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java new file mode 100644 index 00000000000..f5226c5ec62 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java @@ -0,0 +1,61 @@ +/* + * Copyright 2024 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; + +// [START spanner_list_backup_schedules_sample] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPage; +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPagedResponse; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.DatabaseName; +import com.google.spanner.admin.database.v1.ListBackupSchedulesRequest; +import java.io.IOException; + +class ListBackupSchedulesSample { + + static void ListBackupSchedules() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + listBackupSchedules(projectId, instanceId, databaseId); + } + + static void listBackupSchedules(String projectId, String instanceId, + String databaseId) throws IOException { + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + DatabaseName databaseName = + DatabaseName.of(projectId, instanceId, databaseId); + ListBackupSchedulesPagedResponse backupSchedules = + databaseAdminClient.listBackupSchedules( + ListBackupSchedulesRequest.newBuilder() + .setParent(databaseName.toString()) + .build()); + + System.out.println(String.format("Backup schedules for database '%s'", + databaseName.toString())); + for (ListBackupSchedulesPage page : backupSchedules.iteratePages()) { + for (BackupSchedule backupSchedule : page.iterateAll()) + System.out.println( + String.format("Backup schedule: %s", backupSchedule.getName())); + } + } + } +} +// [END spanner_list_backup_schedules_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java new file mode 100644 index 00000000000..6af5391ffc3 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java @@ -0,0 +1,76 @@ +/* + * Copyright 2024 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; + +// [START spanner_update_backup_schedule_sample] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.protobuf.Duration; +import com.google.protobuf.FieldMask; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.BackupScheduleName; +import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CrontabSpec; +import com.google.spanner.admin.database.v1.UpdateBackupScheduleRequest; +import java.io.IOException; + +class UpdateBackupScheduleSample { + + static void updateBackupSchedule() throws IOException { + // TODO(developer): Replace these variables before running the sample. + String projectId = "my-project"; + String instanceId = "my-instance"; + String databaseId = "my-database"; + String backupScheduleId = "my-backup-schedule"; + updateBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); + } + + static void updateBackupSchedule(String projectId, String instanceId, + String databaseId, String backupScheduleId) + throws IOException { + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + final BackupSchedule backupSchedule = + BackupSchedule.newBuilder() + .setName(backupScheduleName.toString()) + .setRetentionDuration( + Duration.newBuilder().setSeconds(3600 * 24 * 14)) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec( + CrontabSpec.newBuilder().setText("0 12 * * *").build()) + .build()) + .build(); + + try (DatabaseAdminClient databaseAdminClient = + DatabaseAdminClient.create()) { + final FieldMask fieldMask = FieldMask.newBuilder() + .addPaths("retention_duration") + .addPaths("spec.cron_spec.text") + .build(); + final BackupSchedule updatedBackupSchedule = + databaseAdminClient.updateBackupSchedule( + UpdateBackupScheduleRequest.newBuilder() + .setBackupSchedule(backupSchedule) + .setUpdateMask(fieldMask) + .build()); + System.out.println(String.format("Updated backup schedule: %s", + updatedBackupSchedule.getName())); + } + } +} +// [END spanner_update_backup_schedule_sample] diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java index 02748cb3f81..870887720d7 100644 --- a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java @@ -18,27 +18,33 @@ import static com.google.common.truth.Truth.assertThat; +import com.google.spanner.admin.database.v1.BackupScheduleName; import java.util.UUID; import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; -public class CreateBackupScheduleSampleIT extends SampleTestBase { +@RunWith(JUnit4.class) +public class CreateBackupScheduleSampleIT extends SampleTestBaseV2 { + // Default instance and given db should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); - @Test - public void testCreateBackupScheduleSample() throws Exception { - String backupScheduleId = String.format("schedule-%s", uuid.randomUUID()); - String out = - SampleRunner.runSample( - () -> { - try { - CreateBackupScheduleSample.createBackupSchedule(projectId, - instanceId, - databaseId, - backupScheduleId); - } finally { - // spanner.getDatabaseAdminClinet().deleteBackupSchedule(backupScheduleId); - } - } - ) - assertThat(out).contains(String.format("Created backup schedule")); - } -} \ No newline at end of file + @Test + public void testCreateBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + String out = SampleRunner.runSample(() -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains( + String.format("Created backup schedule: %s", backupScheduleName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java new file mode 100644 index 00000000000..4883818d27c --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java @@ -0,0 +1,67 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.common.base.Preconditions; +import com.google.spanner.admin.database.v1.BackupScheduleName; +import java.util.UUID; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateBackupScheduleWithEncryptionSampleIT + extends SampleTestBaseV2 { + // Default instance, given db and kms key should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); + private static String key; + + @BeforeClass + public static void setUp() { + String keyLocation = Preconditions.checkNotNull( + System.getProperty("spanner.test.key.location")); + String keyRing = + Preconditions.checkNotNull(System.getProperty("spanner.test.key.ring")); + String keyName = + Preconditions.checkNotNull(System.getProperty("spanner.test.key.name")); + key = "projects/" + projectId + "/locations/" + keyLocation + "/keyRings/" + + keyRing + "/cryptoKeys/" + keyName; + } + + @Test + public void testCreateBackupScheduleWithEncryptionSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + String out = SampleRunner.runSample(() -> { + try { + CreateBackupScheduleWithEncryptionSample + .createBackupScheduleWithEncryption( + projectId, instanceId, databaseId, backupScheduleId, key); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format( + "Created backup schedule with encryption: %s", backupScheduleName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java new file mode 100644 index 00000000000..0e2e7d7ed55 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java @@ -0,0 +1,50 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.spanner.admin.database.v1.BackupScheduleName; +import java.util.UUID; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class CreateIncrementalBackupScheduleSampleIT extends SampleTestBaseV2 { + // Default instance and given db should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); + + @Test + public void testCreateIncrementalBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + String out = SampleRunner.runSample(() -> { + try { + CreateIncrementalBackupScheduleSample.createIncrementalBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format( + "Created incremental backup schedule: %s", backupScheduleName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java new file mode 100644 index 00000000000..6e1d47902dd --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java @@ -0,0 +1,50 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.spanner.admin.database.v1.BackupScheduleName; +import java.util.UUID; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class DeleteBackupScheduleSampleIT extends SampleTestBaseV2 { + // Default instance and given db should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); + + @Test + public void testDeleteBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + String out = SampleRunner.runSample(() -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains( + String.format("Deleted backup schedule: %s", backupScheduleName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java new file mode 100644 index 00000000000..ef5b98afd00 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java @@ -0,0 +1,52 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.spanner.admin.database.v1.BackupScheduleName; +import java.util.UUID; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class GetBackupScheduleSampleIT extends SampleTestBaseV2 { + // Default instance and given db should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); + + @Test + public void testGetBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + String out = SampleRunner.runSample(() -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + GetBackupScheduleSample.getBackupSchedule(projectId, instanceId, + databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains( + String.format("Retrieved backup schedule: %s", backupScheduleName)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java new file mode 100644 index 00000000000..1f6dab82445 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java @@ -0,0 +1,63 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.spanner.admin.database.v1.BackupScheduleName; +import java.util.UUID; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class ListBackupSchedulesSampleIT extends SampleTestBaseV2 { + // Default instance and given db should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); + + @Test + public void testListBackupSchedulesSample() throws Exception { + String backupScheduleId1 = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName1 = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId1); + + String backupScheduleId2 = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName2 = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId2); + + String out = SampleRunner.runSample(() -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId1); + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId2); + ListBackupSchedulesSample.listBackupSchedules(projectId, instanceId, + databaseId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId1); + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId2); + } + }); + assertThat(out).contains( + String.format("Backup schedule: %s", backupScheduleName1)); + assertThat(out).contains( + String.format("Backup schedule: %s", backupScheduleName2)); + } +} diff --git a/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java new file mode 100644 index 00000000000..d26ff3adeb4 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java @@ -0,0 +1,52 @@ +/* + * Copyright 2024 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; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.spanner.admin.database.v1.BackupScheduleName; +import java.util.UUID; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class UpdateBackupScheduleSampleIT extends SampleTestBaseV2 { + // Default instance and given db should exist for tests to pass. + private static String databaseId = + System.getProperty("spanner.sample.database", "mysample"); + + @Test + public void testUpdateBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = BackupScheduleName.of( + projectId, instanceId, databaseId, backupScheduleId); + String out = SampleRunner.runSample(() -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + UpdateBackupScheduleSample.updateBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains( + String.format("Updated backup schedule: %s", backupScheduleName)); + } +} From c1e8dab702dcc1b223cc0c4ea60b93689929b6e1 Mon Sep 17 00:00:00 2001 From: Aman Agarwal Date: Thu, 19 Sep 2024 16:02:54 +0530 Subject: [PATCH 3/6] Fix formatting issues. --- .../spanner/CreateBackupScheduleSample.java | 20 +++---- ...ateBackupScheduleWithEncryptionSample.java | 39 ++++++------- ...CreateIncrementalBackupScheduleSample.java | 27 ++++----- .../spanner/DeleteBackupScheduleSample.java | 19 +++---- .../spanner/GetBackupScheduleSample.java | 19 +++---- .../spanner/ListBackupSchedulesSample.java | 21 +++---- .../spanner/UpdateBackupScheduleSample.java | 30 +++++----- .../spanner/CreateBackupScheduleSampleIT.java | 30 +++++----- ...eBackupScheduleWithEncryptionSampleIT.java | 56 ++++++++++--------- ...eateIncrementalBackupScheduleSampleIT.java | 31 +++++----- .../spanner/DeleteBackupScheduleSampleIT.java | 30 +++++----- .../spanner/GetBackupScheduleSampleIT.java | 35 ++++++------ .../spanner/ListBackupSchedulesSampleIT.java | 48 ++++++++-------- .../spanner/UpdateBackupScheduleSampleIT.java | 34 +++++------ 14 files changed, 208 insertions(+), 231 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java index 315d53504ad..845d2243c37 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java @@ -39,25 +39,21 @@ static void createBackupSchedule() throws IOException { createBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); } - static void createBackupSchedule(String projectId, String instanceId, - String databaseId, String backupScheduleId) + static void createBackupSchedule( + String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { final BackupSchedule backupSchedule = BackupSchedule.newBuilder() .setFullBackupSpec(FullBackupSpec.newBuilder().build()) - .setRetentionDuration( - Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec( - CrontabSpec.newBuilder().setText("0 0 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("0 0 * * *").build()) .build()) .build(); - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - DatabaseName databaseName = - DatabaseName.of(projectId, instanceId, databaseId); + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); final BackupSchedule createdBackupSchedule = databaseAdminClient.createBackupSchedule( CreateBackupScheduleRequest.newBuilder() @@ -65,8 +61,8 @@ static void createBackupSchedule(String projectId, String instanceId, .setBackupScheduleId(backupScheduleId) .setBackupSchedule(backupSchedule) .build()); - System.out.println(String.format("Created backup schedule: %s", - createdBackupSchedule.getName())); + System.out.println( + String.format("Created backup schedule: %s", createdBackupSchedule.getName())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java index 16f5babaea1..0333009c6bd 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java @@ -23,7 +23,6 @@ import com.google.spanner.admin.database.v1.BackupSchedule; import com.google.spanner.admin.database.v1.BackupScheduleSpec; import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; -import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig.EncryptionType; import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; import com.google.spanner.admin.database.v1.CrontabSpec; import com.google.spanner.admin.database.v1.DatabaseName; @@ -39,39 +38,37 @@ static void createBackupScheduleWithEncryption() throws IOException { String databaseId = "my-database"; String backupScheduleId = "my-backup-schedule"; String kmsKeyName = - "projects/" + projectId + - "/locations//keyRings//cryptoKeys/"; - createBackupScheduleWithEncryption(projectId, instanceId, databaseId, - backupScheduleId, kmsKeyName); + "projects/" + projectId + "/locations//keyRings//cryptoKeys/"; + createBackupScheduleWithEncryption( + projectId, instanceId, databaseId, backupScheduleId, kmsKeyName); } - static void - createBackupScheduleWithEncryption(String projectId, String instanceId, - String databaseId, String backupScheduleId, - String kmsKeyName) throws IOException { + static void createBackupScheduleWithEncryption( + String projectId, + String instanceId, + String databaseId, + String backupScheduleId, + String kmsKeyName) + throws IOException { final CreateBackupEncryptionConfig encryptionConfig = CreateBackupEncryptionConfig.newBuilder() - .setEncryptionType(CreateBackupEncryptionConfig.EncryptionType - .CUSTOMER_MANAGED_ENCRYPTION) + .setEncryptionType( + CreateBackupEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION) .setKmsKeyName(kmsKeyName) .build(); final BackupSchedule backupSchedule = BackupSchedule.newBuilder() .setFullBackupSpec(FullBackupSpec.newBuilder().build()) - .setRetentionDuration( - Duration.newBuilder().setSeconds(3600 * 24 * 7)) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7)) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec( - CrontabSpec.newBuilder().setText("0 0 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("0 0 * * *").build()) .build()) .setEncryptionConfig(encryptionConfig) .build(); - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - DatabaseName databaseName = - DatabaseName.of(projectId, instanceId, databaseId); + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); final BackupSchedule createdBackupSchedule = databaseAdminClient.createBackupSchedule( CreateBackupScheduleRequest.newBuilder() @@ -80,8 +77,8 @@ static void createBackupScheduleWithEncryption() throws IOException { .setBackupSchedule(backupSchedule) .build()); System.out.println( - String.format("Created backup schedule with encryption: %s", - createdBackupSchedule.getName())); + String.format( + "Created backup schedule with encryption: %s", createdBackupSchedule.getName())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java index f47be965f25..7a539f86001 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java @@ -36,31 +36,24 @@ static void createIncrementalBackupSchedule() throws IOException { String instanceId = "my-instance"; String databaseId = "my-database"; String backupScheduleId = "my-backup-schedule"; - createIncrementalBackupSchedule(projectId, instanceId, databaseId, - backupScheduleId); + createIncrementalBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); } - static void - createIncrementalBackupSchedule(String projectId, String instanceId, - String databaseId, String backupScheduleId) + static void createIncrementalBackupSchedule( + String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { final BackupSchedule backupSchedule = BackupSchedule.newBuilder() - .setIncrementalBackupSpec( - IncrementalBackupSpec.newBuilder().build()) - .setRetentionDuration( - Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) + .setIncrementalBackupSpec(IncrementalBackupSpec.newBuilder().build()) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec( - CrontabSpec.newBuilder().setText("0 */6 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("0 */6 * * *").build()) .build()) .build(); - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - DatabaseName databaseName = - DatabaseName.of(projectId, instanceId, databaseId); + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); final BackupSchedule createdBackupSchedule = databaseAdminClient.createBackupSchedule( CreateBackupScheduleRequest.newBuilder() @@ -69,8 +62,8 @@ static void createIncrementalBackupSchedule() throws IOException { .setBackupSchedule(backupSchedule) .build()); System.out.println( - String.format("Created incremental backup schedule: %s", - createdBackupSchedule.getName())); + String.format( + "Created incremental backup schedule: %s", createdBackupSchedule.getName())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java index 93fe14c59f1..bc4cc545c2c 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java @@ -34,19 +34,16 @@ static void deleteBackupSchedule() throws IOException { deleteBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); } - static void deleteBackupSchedule(String projectId, String instanceId, - String databaseId, String backupScheduleId) + static void deleteBackupSchedule( + String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); databaseAdminClient.deleteBackupSchedule( - DeleteBackupScheduleRequest.newBuilder() - .setName(backupScheduleName.toString()) - .build()); - System.out.println(String.format("Deleted backup schedule: %s", - backupScheduleName.toString())); + DeleteBackupScheduleRequest.newBuilder().setName(backupScheduleName.toString()).build()); + System.out.println( + String.format("Deleted backup schedule: %s", backupScheduleName.toString())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java index 2f3a4ffa2f6..619d4ad9539 100644 --- a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java @@ -35,20 +35,17 @@ static void GetBackupSchedule() throws IOException { getBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); } - static void getBackupSchedule(String projectId, String instanceId, - String databaseId, String backupScheduleId) + static void getBackupSchedule( + String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); final BackupSchedule backupSchedule = databaseAdminClient.getBackupSchedule( - GetBackupScheduleRequest.newBuilder() - .setName(backupScheduleName.toString()) - .build()); - System.out.println(String.format("Retrieved backup schedule: %s", - backupScheduleName.toString())); + GetBackupScheduleRequest.newBuilder().setName(backupScheduleName.toString()).build()); + System.out.println( + String.format("Retrieved backup schedule: %s", backupScheduleName.toString())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java index f5226c5ec62..2eb109c09ed 100644 --- a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java @@ -36,24 +36,19 @@ static void ListBackupSchedules() throws IOException { listBackupSchedules(projectId, instanceId, databaseId); } - static void listBackupSchedules(String projectId, String instanceId, - String databaseId) throws IOException { - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - DatabaseName databaseName = - DatabaseName.of(projectId, instanceId, databaseId); + static void listBackupSchedules(String projectId, String instanceId, String databaseId) + throws IOException { + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); ListBackupSchedulesPagedResponse backupSchedules = databaseAdminClient.listBackupSchedules( - ListBackupSchedulesRequest.newBuilder() - .setParent(databaseName.toString()) - .build()); + ListBackupSchedulesRequest.newBuilder().setParent(databaseName.toString()).build()); - System.out.println(String.format("Backup schedules for database '%s'", - databaseName.toString())); + System.out.println( + String.format("Backup schedules for database '%s'", databaseName.toString())); for (ListBackupSchedulesPage page : backupSchedules.iteratePages()) { for (BackupSchedule backupSchedule : page.iterateAll()) - System.out.println( - String.format("Backup schedule: %s", backupSchedule.getName())); + System.out.println(String.format("Backup schedule: %s", backupSchedule.getName())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java index 6af5391ffc3..6b399ac897e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java @@ -39,37 +39,35 @@ static void updateBackupSchedule() throws IOException { updateBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); } - static void updateBackupSchedule(String projectId, String instanceId, - String databaseId, String backupScheduleId) + static void updateBackupSchedule( + String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); final BackupSchedule backupSchedule = BackupSchedule.newBuilder() .setName(backupScheduleName.toString()) - .setRetentionDuration( - Duration.newBuilder().setSeconds(3600 * 24 * 14)) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 14)) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec( - CrontabSpec.newBuilder().setText("0 12 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("0 12 * * *").build()) .build()) .build(); - try (DatabaseAdminClient databaseAdminClient = - DatabaseAdminClient.create()) { - final FieldMask fieldMask = FieldMask.newBuilder() - .addPaths("retention_duration") - .addPaths("spec.cron_spec.text") - .build(); + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + final FieldMask fieldMask = + FieldMask.newBuilder() + .addPaths("retention_duration") + .addPaths("spec.cron_spec.text") + .build(); final BackupSchedule updatedBackupSchedule = databaseAdminClient.updateBackupSchedule( UpdateBackupScheduleRequest.newBuilder() .setBackupSchedule(backupSchedule) .setUpdateMask(fieldMask) .build()); - System.out.println(String.format("Updated backup schedule: %s", - updatedBackupSchedule.getName())); + System.out.println( + String.format("Updated backup schedule: %s", updatedBackupSchedule.getName())); } } } diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java index 870887720d7..6433a3ff105 100644 --- a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java @@ -27,24 +27,24 @@ @RunWith(JUnit4.class) public class CreateBackupScheduleSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test public void testCreateBackupScheduleSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); - String out = SampleRunner.runSample(() -> { - try { - CreateBackupScheduleSample.createBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out).contains( - String.format("Created backup schedule: %s", backupScheduleName)); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format("Created backup schedule: %s", backupScheduleName)); } } diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java index 4883818d27c..7f19f6e0969 100644 --- a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java @@ -27,41 +27,45 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class CreateBackupScheduleWithEncryptionSampleIT - extends SampleTestBaseV2 { +public class CreateBackupScheduleWithEncryptionSampleIT extends SampleTestBaseV2 { // Default instance, given db and kms key should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); private static String key; @BeforeClass public static void setUp() { - String keyLocation = Preconditions.checkNotNull( - System.getProperty("spanner.test.key.location")); - String keyRing = - Preconditions.checkNotNull(System.getProperty("spanner.test.key.ring")); - String keyName = - Preconditions.checkNotNull(System.getProperty("spanner.test.key.name")); - key = "projects/" + projectId + "/locations/" + keyLocation + "/keyRings/" + - keyRing + "/cryptoKeys/" + keyName; + String keyLocation = + Preconditions.checkNotNull(System.getProperty("spanner.test.key.location")); + String keyRing = Preconditions.checkNotNull(System.getProperty("spanner.test.key.ring")); + String keyName = Preconditions.checkNotNull(System.getProperty("spanner.test.key.name")); + key = + "projects/" + + projectId + + "/locations/" + + keyLocation + + "/keyRings/" + + keyRing + + "/cryptoKeys/" + + keyName; } @Test public void testCreateBackupScheduleWithEncryptionSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); - String out = SampleRunner.runSample(() -> { - try { - CreateBackupScheduleWithEncryptionSample - .createBackupScheduleWithEncryption( - projectId, instanceId, databaseId, backupScheduleId, key); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out).contains(String.format( - "Created backup schedule with encryption: %s", backupScheduleName)); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleWithEncryptionSample.createBackupScheduleWithEncryption( + projectId, instanceId, databaseId, backupScheduleId, key); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out) + .contains(String.format("Created backup schedule with encryption: %s", backupScheduleName)); } } diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java index 0e2e7d7ed55..74136562e10 100644 --- a/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java @@ -27,24 +27,25 @@ @RunWith(JUnit4.class) public class CreateIncrementalBackupScheduleSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test public void testCreateIncrementalBackupScheduleSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); - String out = SampleRunner.runSample(() -> { - try { - CreateIncrementalBackupScheduleSample.createIncrementalBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out).contains(String.format( - "Created incremental backup schedule: %s", backupScheduleName)); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateIncrementalBackupScheduleSample.createIncrementalBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out) + .contains(String.format("Created incremental backup schedule: %s", backupScheduleName)); } } diff --git a/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java index 6e1d47902dd..0aecc248387 100644 --- a/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java @@ -27,24 +27,24 @@ @RunWith(JUnit4.class) public class DeleteBackupScheduleSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test public void testDeleteBackupScheduleSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); - String out = SampleRunner.runSample(() -> { - try { - CreateBackupScheduleSample.createBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out).contains( - String.format("Deleted backup schedule: %s", backupScheduleName)); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format("Deleted backup schedule: %s", backupScheduleName)); } } diff --git a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java index ef5b98afd00..c1cc0661b7d 100644 --- a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java @@ -27,26 +27,27 @@ @RunWith(JUnit4.class) public class GetBackupScheduleSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test public void testGetBackupScheduleSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); - String out = SampleRunner.runSample(() -> { - try { - CreateBackupScheduleSample.createBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - GetBackupScheduleSample.getBackupSchedule(projectId, instanceId, - databaseId, backupScheduleId); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out).contains( - String.format("Retrieved backup schedule: %s", backupScheduleName)); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + GetBackupScheduleSample.getBackupSchedule( + projectId, instanceId, + databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format("Retrieved backup schedule: %s", backupScheduleName)); } } diff --git a/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java index 1f6dab82445..330027e6217 100644 --- a/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java @@ -27,37 +27,35 @@ @RunWith(JUnit4.class) public class ListBackupSchedulesSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test public void testListBackupSchedulesSample() throws Exception { String backupScheduleId1 = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName1 = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId1); + BackupScheduleName backupScheduleName1 = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId1); String backupScheduleId2 = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName2 = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId2); + BackupScheduleName backupScheduleName2 = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId2); - String out = SampleRunner.runSample(() -> { - try { - CreateBackupScheduleSample.createBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId1); - CreateBackupScheduleSample.createBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId2); - ListBackupSchedulesSample.listBackupSchedules(projectId, instanceId, - databaseId); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId1); - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId2); - } - }); - assertThat(out).contains( - String.format("Backup schedule: %s", backupScheduleName1)); - assertThat(out).contains( - String.format("Backup schedule: %s", backupScheduleName2)); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId1); + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId2); + ListBackupSchedulesSample.listBackupSchedules(projectId, instanceId, databaseId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId1); + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId2); + } + }); + assertThat(out).contains(String.format("Backup schedule: %s", backupScheduleName1)); + assertThat(out).contains(String.format("Backup schedule: %s", backupScheduleName2)); } } diff --git a/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java index d26ff3adeb4..8b885ac5959 100644 --- a/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java @@ -27,26 +27,26 @@ @RunWith(JUnit4.class) public class UpdateBackupScheduleSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. - private static String databaseId = - System.getProperty("spanner.sample.database", "mysample"); + private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test public void testUpdateBackupScheduleSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = BackupScheduleName.of( - projectId, instanceId, databaseId, backupScheduleId); - String out = SampleRunner.runSample(() -> { - try { - CreateBackupScheduleSample.createBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - UpdateBackupScheduleSample.updateBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out).contains( - String.format("Updated backup schedule: %s", backupScheduleName)); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateBackupScheduleSample.createBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + UpdateBackupScheduleSample.updateBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format("Updated backup schedule: %s", backupScheduleName)); } } From 4218dc206516b7fc50dcfb4366300d4f33f2b9a9 Mon Sep 17 00:00:00 2001 From: Aman Agarwal Date: Thu, 19 Sep 2024 19:26:34 +0530 Subject: [PATCH 4/6] Fix `GetBackupSchedule` and `ListBackupSchedules` method names. --- .../java/com/example/spanner/GetBackupScheduleSample.java | 2 +- .../java/com/example/spanner/ListBackupSchedulesSample.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java index 619d4ad9539..30cc5e2216e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java @@ -26,7 +26,7 @@ class GetBackupScheduleSample { - static void GetBackupSchedule() throws IOException { + static void getBackupSchedule() throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; diff --git a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java index 2eb109c09ed..9825607600d 100644 --- a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java @@ -28,7 +28,7 @@ class ListBackupSchedulesSample { - static void ListBackupSchedules() throws IOException { + static void listBackupSchedules() throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; @@ -47,8 +47,9 @@ static void listBackupSchedules(String projectId, String instanceId, String data System.out.println( String.format("Backup schedules for database '%s'", databaseName.toString())); for (ListBackupSchedulesPage page : backupSchedules.iteratePages()) { - for (BackupSchedule backupSchedule : page.iterateAll()) + for (BackupSchedule backupSchedule : page.iterateAll()) { System.out.println(String.format("Backup schedule: %s", backupSchedule.getName())); + } } } } From 73226bf2b4b6e7a0a3b26c21fa16112fc6e72806 Mon Sep 17 00:00:00 2001 From: Aman Agarwal Date: Fri, 20 Sep 2024 11:52:02 +0530 Subject: [PATCH 5/6] Fix tags for backup schedule samples. --- ...ateBackupScheduleWithEncryptionSample.java | 85 ------------------- ...va => CreateFullBackupScheduleSample.java} | 22 +++-- ...CreateIncrementalBackupScheduleSample.java | 15 +++- .../spanner/DeleteBackupScheduleSample.java | 4 +- .../spanner/GetBackupScheduleSample.java | 4 +- .../spanner/ListBackupSchedulesSample.java | 4 +- .../spanner/UpdateBackupScheduleSample.java | 15 +++- ...eBackupScheduleWithEncryptionSampleIT.java | 71 ---------------- ... => CreateFullBackupScheduleSampleIT.java} | 6 +- .../spanner/DeleteBackupScheduleSampleIT.java | 2 +- .../spanner/GetBackupScheduleSampleIT.java | 2 +- .../spanner/ListBackupSchedulesSampleIT.java | 4 +- .../spanner/UpdateBackupScheduleSampleIT.java | 2 +- 13 files changed, 50 insertions(+), 186 deletions(-) delete mode 100644 samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java rename samples/snippets/src/main/java/com/example/spanner/{CreateBackupScheduleSample.java => CreateFullBackupScheduleSample.java} (77%) delete mode 100644 samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java rename samples/snippets/src/test/java/com/example/spanner/{CreateBackupScheduleSampleIT.java => CreateFullBackupScheduleSampleIT.java} (88%) diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java deleted file mode 100644 index 0333009c6bd..00000000000 --- a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Copyright 2024 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; - -// [START spanner_create_backup_schedule_with_encryption_sample] - -import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; -import com.google.protobuf.Duration; -import com.google.spanner.admin.database.v1.BackupSchedule; -import com.google.spanner.admin.database.v1.BackupScheduleSpec; -import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; -import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; -import com.google.spanner.admin.database.v1.CrontabSpec; -import com.google.spanner.admin.database.v1.DatabaseName; -import com.google.spanner.admin.database.v1.FullBackupSpec; -import java.io.IOException; - -class CreateBackupScheduleWithEncryptionSample { - - static void createBackupScheduleWithEncryption() throws IOException { - // TODO(developer): Replace these variables before running the sample. - String projectId = "my-project"; - String instanceId = "my-instance"; - String databaseId = "my-database"; - String backupScheduleId = "my-backup-schedule"; - String kmsKeyName = - "projects/" + projectId + "/locations//keyRings//cryptoKeys/"; - createBackupScheduleWithEncryption( - projectId, instanceId, databaseId, backupScheduleId, kmsKeyName); - } - - static void createBackupScheduleWithEncryption( - String projectId, - String instanceId, - String databaseId, - String backupScheduleId, - String kmsKeyName) - throws IOException { - final CreateBackupEncryptionConfig encryptionConfig = - CreateBackupEncryptionConfig.newBuilder() - .setEncryptionType( - CreateBackupEncryptionConfig.EncryptionType.CUSTOMER_MANAGED_ENCRYPTION) - .setKmsKeyName(kmsKeyName) - .build(); - final BackupSchedule backupSchedule = - BackupSchedule.newBuilder() - .setFullBackupSpec(FullBackupSpec.newBuilder().build()) - .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7)) - .setSpec( - BackupScheduleSpec.newBuilder() - .setCronSpec(CrontabSpec.newBuilder().setText("0 0 * * *").build()) - .build()) - .setEncryptionConfig(encryptionConfig) - .build(); - - try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { - DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); - final BackupSchedule createdBackupSchedule = - databaseAdminClient.createBackupSchedule( - CreateBackupScheduleRequest.newBuilder() - .setParent(databaseName.toString()) - .setBackupScheduleId(backupScheduleId) - .setBackupSchedule(backupSchedule) - .build()); - System.out.println( - String.format( - "Created backup schedule with encryption: %s", createdBackupSchedule.getName())); - } - } -} -// [END spanner_create_backup_schedule_with_encryption_sample] diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java similarity index 77% rename from samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java rename to samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java index 845d2243c37..0f482354abc 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java @@ -16,40 +16,46 @@ package com.example.spanner; -// [START spanner_create_backup_schedule_sample] +// [START spanner_create_full_backup_schedule] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.protobuf.Duration; import com.google.spanner.admin.database.v1.BackupSchedule; import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; import com.google.spanner.admin.database.v1.CrontabSpec; import com.google.spanner.admin.database.v1.DatabaseName; import com.google.spanner.admin.database.v1.FullBackupSpec; import java.io.IOException; -class CreateBackupScheduleSample { +class CreateFullBackupScheduleSample { - static void createBackupSchedule() throws IOException { + static void createFullBackupSchedule() throws IOException { // TODO(developer): Replace these variables before running the sample. String projectId = "my-project"; String instanceId = "my-instance"; String databaseId = "my-database"; String backupScheduleId = "my-backup-schedule"; - createBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); + createFullBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); } - static void createBackupSchedule( + static void createFullBackupSchedule( String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { + final CreateBackupEncryptionConfig encryptionConfig = + CreateBackupEncryptionConfig.newBuilder() + .setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION) + .build(); final BackupSchedule backupSchedule = BackupSchedule.newBuilder() .setFullBackupSpec(FullBackupSpec.newBuilder().build()) - .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24).build()) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec(CrontabSpec.newBuilder().setText("0 0 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("30 12 * * *").build()) .build()) + .setEncryptionConfig(encryptionConfig) .build(); try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { @@ -66,4 +72,4 @@ static void createBackupSchedule( } } } -// [END spanner_create_backup_schedule_sample] +// [END spanner_create_full_backup_schedule] diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java index 7a539f86001..dc9ec50f4e0 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java @@ -16,12 +16,13 @@ package com.example.spanner; -// [START spanner_create_incremental_backup_schedule_sample] +// [START spanner_create_incremental_backup_schedule] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.protobuf.Duration; import com.google.spanner.admin.database.v1.BackupSchedule; import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; import com.google.spanner.admin.database.v1.CreateBackupScheduleRequest; import com.google.spanner.admin.database.v1.CrontabSpec; import com.google.spanner.admin.database.v1.DatabaseName; @@ -42,14 +43,20 @@ static void createIncrementalBackupSchedule() throws IOException { static void createIncrementalBackupSchedule( String projectId, String instanceId, String databaseId, String backupScheduleId) throws IOException { + final CreateBackupEncryptionConfig encryptionConfig = + CreateBackupEncryptionConfig.newBuilder() + .setEncryptionType( + CreateBackupEncryptionConfig.EncryptionType.GOOGLE_DEFAULT_ENCRYPTION) + .build(); final BackupSchedule backupSchedule = BackupSchedule.newBuilder() .setIncrementalBackupSpec(IncrementalBackupSpec.newBuilder().build()) - .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 7).build()) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24).build()) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec(CrontabSpec.newBuilder().setText("0 */6 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("30 12 * * *").build()) .build()) + .setEncryptionConfig(encryptionConfig) .build(); try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { @@ -67,4 +74,4 @@ static void createIncrementalBackupSchedule( } } } -// [END spanner_create_incremental_backup_schedule_sample] +// [END spanner_create_incremental_backup_schedule] diff --git a/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java index bc4cc545c2c..e87a1fcb66e 100644 --- a/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java @@ -16,7 +16,7 @@ package com.example.spanner; -// [START spanner_delete_backup_schedule_sample] +// [START spanner_delete_backup_schedule] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.BackupScheduleName; @@ -47,4 +47,4 @@ static void deleteBackupSchedule( } } } -// [END spanner_delete_backup_schedule_sample] +// [END spanner_delete_backup_schedule] diff --git a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java index 30cc5e2216e..2adfd3d0d37 100644 --- a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java @@ -16,7 +16,7 @@ package com.example.spanner; -// [START spanner_get_backup_schedule_sample] +// [START spanner_get_backup_schedule] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.spanner.admin.database.v1.BackupSchedule; @@ -49,4 +49,4 @@ static void getBackupSchedule( } } } -// [END spanner_get_backup_schedule_sample] +// [END spanner_get_backup_schedule] diff --git a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java index 9825607600d..af52be82d00 100644 --- a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java @@ -16,7 +16,7 @@ package com.example.spanner; -// [START spanner_list_backup_schedules_sample] +// [START spanner_list_backup_schedules] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPage; @@ -54,4 +54,4 @@ static void listBackupSchedules(String projectId, String instanceId, String data } } } -// [END spanner_list_backup_schedules_sample] +// [END spanner_list_backup_schedules] diff --git a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java index 6b399ac897e..ce2e505a5f1 100644 --- a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java @@ -16,7 +16,7 @@ package com.example.spanner; -// [START spanner_update_backup_schedule_sample] +// [START spanner_update_backup_schedule] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; import com.google.protobuf.Duration; @@ -24,6 +24,7 @@ import com.google.spanner.admin.database.v1.BackupSchedule; import com.google.spanner.admin.database.v1.BackupScheduleName; import com.google.spanner.admin.database.v1.BackupScheduleSpec; +import com.google.spanner.admin.database.v1.CreateBackupEncryptionConfig; import com.google.spanner.admin.database.v1.CrontabSpec; import com.google.spanner.admin.database.v1.UpdateBackupScheduleRequest; import java.io.IOException; @@ -44,14 +45,19 @@ static void updateBackupSchedule( throws IOException { BackupScheduleName backupScheduleName = BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + final CreateBackupEncryptionConfig encryptionConfig = + CreateBackupEncryptionConfig.newBuilder() + .setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION) + .build(); final BackupSchedule backupSchedule = BackupSchedule.newBuilder() .setName(backupScheduleName.toString()) - .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 24 * 14)) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 48)) .setSpec( BackupScheduleSpec.newBuilder() - .setCronSpec(CrontabSpec.newBuilder().setText("0 12 * * *").build()) + .setCronSpec(CrontabSpec.newBuilder().setText("45 15 * * *").build()) .build()) + .setEncryptionConfig(encryptionConfig) .build(); try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { @@ -59,6 +65,7 @@ static void updateBackupSchedule( FieldMask.newBuilder() .addPaths("retention_duration") .addPaths("spec.cron_spec.text") + .addPaths("encryption_config") .build(); final BackupSchedule updatedBackupSchedule = databaseAdminClient.updateBackupSchedule( @@ -71,4 +78,4 @@ static void updateBackupSchedule( } } } -// [END spanner_update_backup_schedule_sample] +// [END spanner_update_backup_schedule] diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java deleted file mode 100644 index 7f19f6e0969..00000000000 --- a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleWithEncryptionSampleIT.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright 2024 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; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.common.base.Preconditions; -import com.google.spanner.admin.database.v1.BackupScheduleName; -import java.util.UUID; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class CreateBackupScheduleWithEncryptionSampleIT extends SampleTestBaseV2 { - // Default instance, given db and kms key should exist for tests to pass. - private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); - private static String key; - - @BeforeClass - public static void setUp() { - String keyLocation = - Preconditions.checkNotNull(System.getProperty("spanner.test.key.location")); - String keyRing = Preconditions.checkNotNull(System.getProperty("spanner.test.key.ring")); - String keyName = Preconditions.checkNotNull(System.getProperty("spanner.test.key.name")); - key = - "projects/" - + projectId - + "/locations/" - + keyLocation - + "/keyRings/" - + keyRing - + "/cryptoKeys/" - + keyName; - } - - @Test - public void testCreateBackupScheduleWithEncryptionSample() throws Exception { - String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); - BackupScheduleName backupScheduleName = - BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); - String out = - SampleRunner.runSample( - () -> { - try { - CreateBackupScheduleWithEncryptionSample.createBackupScheduleWithEncryption( - projectId, instanceId, databaseId, backupScheduleId, key); - } finally { - DeleteBackupScheduleSample.deleteBackupSchedule( - projectId, instanceId, databaseId, backupScheduleId); - } - }); - assertThat(out) - .contains(String.format("Created backup schedule with encryption: %s", backupScheduleName)); - } -} diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateFullBackupScheduleSampleIT.java similarity index 88% rename from samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java rename to samples/snippets/src/test/java/com/example/spanner/CreateFullBackupScheduleSampleIT.java index 6433a3ff105..15ec04fe306 100644 --- a/samples/snippets/src/test/java/com/example/spanner/CreateBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/CreateFullBackupScheduleSampleIT.java @@ -25,12 +25,12 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) -public class CreateBackupScheduleSampleIT extends SampleTestBaseV2 { +public class CreateFullBackupScheduleSampleIT extends SampleTestBaseV2 { // Default instance and given db should exist for tests to pass. private static String databaseId = System.getProperty("spanner.sample.database", "mysample"); @Test - public void testCreateBackupScheduleSample() throws Exception { + public void testCreateFullBackupScheduleSample() throws Exception { String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); BackupScheduleName backupScheduleName = BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); @@ -38,7 +38,7 @@ public void testCreateBackupScheduleSample() throws Exception { SampleRunner.runSample( () -> { try { - CreateBackupScheduleSample.createBackupSchedule( + CreateFullBackupScheduleSample.createFullBackupSchedule( projectId, instanceId, databaseId, backupScheduleId); } finally { DeleteBackupScheduleSample.deleteBackupSchedule( diff --git a/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java index 0aecc248387..3d11bd8dce1 100644 --- a/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/DeleteBackupScheduleSampleIT.java @@ -38,7 +38,7 @@ public void testDeleteBackupScheduleSample() throws Exception { SampleRunner.runSample( () -> { try { - CreateBackupScheduleSample.createBackupSchedule( + CreateFullBackupScheduleSample.createFullBackupSchedule( projectId, instanceId, databaseId, backupScheduleId); } finally { DeleteBackupScheduleSample.deleteBackupSchedule( diff --git a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java index c1cc0661b7d..1c8cbd51870 100644 --- a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java @@ -38,7 +38,7 @@ public void testGetBackupScheduleSample() throws Exception { SampleRunner.runSample( () -> { try { - CreateBackupScheduleSample.createBackupSchedule( + CreateFullBackupScheduleSample.createFullBackupSchedule( projectId, instanceId, databaseId, backupScheduleId); GetBackupScheduleSample.getBackupSchedule( projectId, instanceId, diff --git a/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java index 330027e6217..386b9442c01 100644 --- a/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.java @@ -43,9 +43,9 @@ public void testListBackupSchedulesSample() throws Exception { SampleRunner.runSample( () -> { try { - CreateBackupScheduleSample.createBackupSchedule( + CreateFullBackupScheduleSample.createFullBackupSchedule( projectId, instanceId, databaseId, backupScheduleId1); - CreateBackupScheduleSample.createBackupSchedule( + CreateFullBackupScheduleSample.createFullBackupSchedule( projectId, instanceId, databaseId, backupScheduleId2); ListBackupSchedulesSample.listBackupSchedules(projectId, instanceId, databaseId); } finally { diff --git a/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java index 8b885ac5959..ea299571f5d 100644 --- a/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/UpdateBackupScheduleSampleIT.java @@ -38,7 +38,7 @@ public void testUpdateBackupScheduleSample() throws Exception { SampleRunner.runSample( () -> { try { - CreateBackupScheduleSample.createBackupSchedule( + CreateFullBackupScheduleSample.createFullBackupSchedule( projectId, instanceId, databaseId, backupScheduleId); UpdateBackupScheduleSample.updateBackupSchedule( projectId, instanceId, databaseId, backupScheduleId); From b22ae047b869fb5234bd8e780caa323f78f6d2ab Mon Sep 17 00:00:00 2001 From: Aman Agarwal Date: Mon, 23 Sep 2024 22:23:02 +0530 Subject: [PATCH 6/6] Refactored samples' console logs. --- .../spanner/CreateFullBackupScheduleSample.java | 4 +++- .../CreateIncrementalBackupScheduleSample.java | 3 ++- .../example/spanner/GetBackupScheduleSample.java | 3 ++- .../spanner/ListBackupSchedulesSample.java | 15 +++++---------- .../spanner/UpdateBackupScheduleSample.java | 4 +++- .../spanner/GetBackupScheduleSampleIT.java | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java index 0f482354abc..b3836092692 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java @@ -68,7 +68,9 @@ static void createFullBackupSchedule( .setBackupSchedule(backupSchedule) .build()); System.out.println( - String.format("Created backup schedule: %s", createdBackupSchedule.getName())); + String.format( + "Created backup schedule: %s\n%s", + createdBackupSchedule.getName(), createdBackupSchedule.toString())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java index dc9ec50f4e0..f73ebd30f23 100644 --- a/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java @@ -70,7 +70,8 @@ static void createIncrementalBackupSchedule( .build()); System.out.println( String.format( - "Created incremental backup schedule: %s", createdBackupSchedule.getName())); + "Created incremental backup schedule: %s\n%s", + createdBackupSchedule.getName(), createdBackupSchedule.toString())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java index 2adfd3d0d37..3cd7e21f9b1 100644 --- a/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java @@ -45,7 +45,8 @@ static void getBackupSchedule( databaseAdminClient.getBackupSchedule( GetBackupScheduleRequest.newBuilder().setName(backupScheduleName.toString()).build()); System.out.println( - String.format("Retrieved backup schedule: %s", backupScheduleName.toString())); + String.format( + "Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java index af52be82d00..fba708937c0 100644 --- a/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java @@ -19,11 +19,8 @@ // [START spanner_list_backup_schedules] import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; -import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPage; -import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient.ListBackupSchedulesPagedResponse; import com.google.spanner.admin.database.v1.BackupSchedule; import com.google.spanner.admin.database.v1.DatabaseName; -import com.google.spanner.admin.database.v1.ListBackupSchedulesRequest; import java.io.IOException; class ListBackupSchedulesSample { @@ -40,16 +37,14 @@ static void listBackupSchedules(String projectId, String instanceId, String data throws IOException { try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { DatabaseName databaseName = DatabaseName.of(projectId, instanceId, databaseId); - ListBackupSchedulesPagedResponse backupSchedules = - databaseAdminClient.listBackupSchedules( - ListBackupSchedulesRequest.newBuilder().setParent(databaseName.toString()).build()); System.out.println( String.format("Backup schedules for database '%s'", databaseName.toString())); - for (ListBackupSchedulesPage page : backupSchedules.iteratePages()) { - for (BackupSchedule backupSchedule : page.iterateAll()) { - System.out.println(String.format("Backup schedule: %s", backupSchedule.getName())); - } + for (BackupSchedule backupSchedule : + databaseAdminClient.listBackupSchedules(databaseName).iterateAll()) { + System.out.println( + String.format( + "Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString())); } } } diff --git a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java index ce2e505a5f1..b49ec4901f6 100644 --- a/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java +++ b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java @@ -74,7 +74,9 @@ static void updateBackupSchedule( .setUpdateMask(fieldMask) .build()); System.out.println( - String.format("Updated backup schedule: %s", updatedBackupSchedule.getName())); + String.format( + "Updated backup schedule: %s\n%s", + updatedBackupSchedule.getName(), updatedBackupSchedule.toString())); } } } diff --git a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java index 1c8cbd51870..fa006355a23 100644 --- a/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java +++ b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.java @@ -48,6 +48,6 @@ public void testGetBackupScheduleSample() throws Exception { projectId, instanceId, databaseId, backupScheduleId); } }); - assertThat(out).contains(String.format("Retrieved backup schedule: %s", backupScheduleName)); + assertThat(out).contains(String.format("Backup schedule: %s", backupScheduleName)); } }