From 8cd516351e7859a81f00f17cb5071edbd804ea90 Mon Sep 17 00:00:00 2001 From: aman-19 Date: Wed, 25 Sep 2024 10:43:45 +0530 Subject: [PATCH] feat: Add samples for backup schedule feature APIs. (#3339) * Add java sample for `CreateBackupSchedule` API. * Add samples for backups schedule APIs. * Fix formatting issues. * Fix `GetBackupSchedule` and `ListBackupSchedules` method names. * Fix tags for backup schedule samples. * Refactored samples' console logs. --- .../CreateFullBackupScheduleSample.java | 77 +++++++++++++++++ ...CreateIncrementalBackupScheduleSample.java | 78 +++++++++++++++++ .../spanner/DeleteBackupScheduleSample.java | 50 +++++++++++ .../spanner/GetBackupScheduleSample.java | 53 ++++++++++++ .../spanner/ListBackupSchedulesSample.java | 52 ++++++++++++ .../spanner/UpdateBackupScheduleSample.java | 83 +++++++++++++++++++ .../CreateFullBackupScheduleSampleIT.java | 50 +++++++++++ ...eateIncrementalBackupScheduleSampleIT.java | 51 ++++++++++++ .../spanner/DeleteBackupScheduleSampleIT.java | 50 +++++++++++ .../spanner/GetBackupScheduleSampleIT.java | 53 ++++++++++++ .../spanner/ListBackupSchedulesSampleIT.java | 61 ++++++++++++++ .../spanner/UpdateBackupScheduleSampleIT.java | 52 ++++++++++++ 12 files changed, 710 insertions(+) create mode 100644 samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.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/CreateFullBackupScheduleSampleIT.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/CreateFullBackupScheduleSample.java b/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.java new file mode 100644 index 00000000000..b3836092692 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/CreateFullBackupScheduleSample.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_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 CreateFullBackupScheduleSample { + + 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"; + createFullBackupSchedule(projectId, instanceId, databaseId, backupScheduleId); + } + + 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).build()) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec(CrontabSpec.newBuilder().setText("30 12 * * *").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: %s\n%s", + createdBackupSchedule.getName(), createdBackupSchedule.toString())); + } + } +} +// [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 new file mode 100644 index 00000000000..f73ebd30f23 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java @@ -0,0 +1,78 @@ +/* + * 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] + +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.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 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).build()) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec(CrontabSpec.newBuilder().setText("30 12 * * *").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 incremental backup schedule: %s\n%s", + createdBackupSchedule.getName(), createdBackupSchedule.toString())); + } + } +} +// [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 new file mode 100644 index 00000000000..e87a1fcb66e --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.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; + +// [START spanner_delete_backup_schedule] + +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] 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..3cd7e21f9b1 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.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_get_backup_schedule] + +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( + "Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString())); + } + } +} +// [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 new file mode 100644 index 00000000000..fba708937c0 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.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; + +// [START spanner_list_backup_schedules] + +import com.google.cloud.spanner.admin.database.v1.DatabaseAdminClient; +import com.google.spanner.admin.database.v1.BackupSchedule; +import com.google.spanner.admin.database.v1.DatabaseName; +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); + + System.out.println( + String.format("Backup schedules for database '%s'", databaseName.toString())); + for (BackupSchedule backupSchedule : + databaseAdminClient.listBackupSchedules(databaseName).iterateAll()) { + System.out.println( + String.format( + "Backup schedule: %s\n%s", backupSchedule.getName(), backupSchedule.toString())); + } + } + } +} +// [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 new file mode 100644 index 00000000000..b49ec4901f6 --- /dev/null +++ b/samples/snippets/src/main/java/com/example/spanner/UpdateBackupScheduleSample.java @@ -0,0 +1,83 @@ +/* + * 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] + +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.CreateBackupEncryptionConfig; +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 CreateBackupEncryptionConfig encryptionConfig = + CreateBackupEncryptionConfig.newBuilder() + .setEncryptionType(CreateBackupEncryptionConfig.EncryptionType.USE_DATABASE_ENCRYPTION) + .build(); + final BackupSchedule backupSchedule = + BackupSchedule.newBuilder() + .setName(backupScheduleName.toString()) + .setRetentionDuration(Duration.newBuilder().setSeconds(3600 * 48)) + .setSpec( + BackupScheduleSpec.newBuilder() + .setCronSpec(CrontabSpec.newBuilder().setText("45 15 * * *").build()) + .build()) + .setEncryptionConfig(encryptionConfig) + .build(); + + try (DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.create()) { + final FieldMask fieldMask = + FieldMask.newBuilder() + .addPaths("retention_duration") + .addPaths("spec.cron_spec.text") + .addPaths("encryption_config") + .build(); + final BackupSchedule updatedBackupSchedule = + databaseAdminClient.updateBackupSchedule( + UpdateBackupScheduleRequest.newBuilder() + .setBackupSchedule(backupSchedule) + .setUpdateMask(fieldMask) + .build()); + System.out.println( + String.format( + "Updated backup schedule: %s\n%s", + updatedBackupSchedule.getName(), updatedBackupSchedule.toString())); + } + } +} +// [END spanner_update_backup_schedule] diff --git a/samples/snippets/src/test/java/com/example/spanner/CreateFullBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateFullBackupScheduleSampleIT.java new file mode 100644 index 00000000000..15ec04fe306 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/CreateFullBackupScheduleSampleIT.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 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 testCreateFullBackupScheduleSample() throws Exception { + String backupScheduleId = String.format("schedule-%s", UUID.randomUUID()); + BackupScheduleName backupScheduleName = + BackupScheduleName.of(projectId, instanceId, databaseId, backupScheduleId); + String out = + SampleRunner.runSample( + () -> { + try { + CreateFullBackupScheduleSample.createFullBackupSchedule( + 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/CreateIncrementalBackupScheduleSampleIT.java b/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java new file mode 100644 index 00000000000..74136562e10 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/CreateIncrementalBackupScheduleSampleIT.java @@ -0,0 +1,51 @@ +/* + * 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..3d11bd8dce1 --- /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 { + CreateFullBackupScheduleSample.createFullBackupSchedule( + 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..fa006355a23 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/GetBackupScheduleSampleIT.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; + +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 { + CreateFullBackupScheduleSample.createFullBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + GetBackupScheduleSample.getBackupSchedule( + projectId, instanceId, + databaseId, backupScheduleId); + } finally { + DeleteBackupScheduleSample.deleteBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId); + } + }); + assertThat(out).contains(String.format("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..386b9442c01 --- /dev/null +++ b/samples/snippets/src/test/java/com/example/spanner/ListBackupSchedulesSampleIT.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; + +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 { + CreateFullBackupScheduleSample.createFullBackupSchedule( + projectId, instanceId, databaseId, backupScheduleId1); + CreateFullBackupScheduleSample.createFullBackupSchedule( + 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..ea299571f5d --- /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 { + CreateFullBackupScheduleSample.createFullBackupSchedule( + 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)); + } +}