Skip to content

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 6 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix formatting issues.
  • Loading branch information
aman-19 committed Sep 19, 2024
commit c1e8dab702dcc1b223cc0c4ea60b93689929b6e1
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,30 @@ 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()
.setParent(databaseName.toString())
.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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,39 +38,37 @@ static void createBackupScheduleWithEncryption() throws IOException {
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);
"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 {
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()
Expand All @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Loading
Loading