-
Couldn't load subscription status.
- Fork 136
feat: Add samples for backup schedule feature APIs. #3339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2d8d7e4
Add java sample for `CreateBackupSchedule` API.
aman-19 b5d8d53
Add samples for backups schedule APIs.
aman-19 c1e8dab
Fix formatting issues.
aman-19 4218dc2
Fix `GetBackupSchedule` and `ListBackupSchedules` method names.
aman-19 73226bf
Fix tags for backup schedule samples.
aman-19 b22ae04
Refactored samples' console logs.
aman-19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
samples/snippets/src/main/java/com/example/spanner/CreateBackupScheduleSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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.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.FullBackupSpec; | ||
| import java.io.IOException; | ||
|
|
||
| class CreateBackupScheduleSample { | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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(); | ||
|
|
||
| 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())); | ||
| } | ||
| } | ||
| } | ||
| // [END spanner_create_backup_schedule_sample] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
85 changes: 85 additions & 0 deletions
85
.../snippets/src/main/java/com/example/spanner/CreateBackupScheduleWithEncryptionSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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/<location>/keyRings/<keyRing>/cryptoKeys/<keyId>"; | ||
| 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] |
70 changes: 70 additions & 0 deletions
70
...les/snippets/src/main/java/com/example/spanner/CreateIncrementalBackupScheduleSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * 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] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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())); | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| // [END spanner_create_incremental_backup_schedule_sample] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
50 changes: 50 additions & 0 deletions
50
samples/snippets/src/main/java/com/example/spanner/DeleteBackupScheduleSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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_sample] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
52 changes: 52 additions & 0 deletions
52
samples/snippets/src/main/java/com/example/spanner/GetBackupScheduleSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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_get_backup_schedule_sample] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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())); | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| // [END spanner_get_backup_schedule_sample] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
57 changes: 57 additions & 0 deletions
57
samples/snippets/src/main/java/com/example/spanner/ListBackupSchedulesSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* | ||
| * 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] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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())); | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| // [END spanner_list_backup_schedules_sample] | ||
aman-19 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.