Skip to content

Commit 0a46070

Browse files
authored
chore(samples): Add samples for Cloud Spanner Default Backup Schedules (#3450)
* chore(samples): Add samples for Cloud Spanner Default Backup Schedules * chore(samples): Add samples for Cloud Spanner Default Backup Schedules * chore(samples): Add samples for Cloud Spanner Default Backup Schedules. Adding `s` to create instance sample class name * chore(samples): Add samples for Cloud Spanner Default Backup Schedules. Adding `s` to create instance sample class name
1 parent 8822e3b commit 0a46070

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_create_instance_without_default_backup_schedule]
20+
21+
import com.google.cloud.spanner.Spanner;
22+
import com.google.cloud.spanner.SpannerOptions;
23+
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient;
24+
import com.google.spanner.admin.instance.v1.CreateInstanceRequest;
25+
import com.google.spanner.admin.instance.v1.Instance;
26+
import com.google.spanner.admin.instance.v1.InstanceConfigName;
27+
import com.google.spanner.admin.instance.v1.ProjectName;
28+
import java.util.concurrent.ExecutionException;
29+
30+
class CreateInstanceWithoutDefaultBackupSchedulesExample {
31+
32+
static void createInstanceWithoutDefaultBackupSchedules() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "my-project";
35+
String instanceId = "my-instance";
36+
createInstanceWithoutDefaultBackupSchedules(projectId, instanceId);
37+
}
38+
39+
static void createInstanceWithoutDefaultBackupSchedules(String projectId, String instanceId) {
40+
// Set Instance configuration.
41+
int nodeCount = 2;
42+
String displayName = "Descriptive name";
43+
44+
// Create an Instance object that will be used to create the instance.
45+
Instance instance =
46+
Instance.newBuilder()
47+
.setDisplayName(displayName)
48+
.setDefaultBackupScheduleType(Instance.DefaultBackupScheduleType.NONE)
49+
.setNodeCount(nodeCount)
50+
.setConfig(InstanceConfigName.of(projectId, "regional-us-east4").toString())
51+
.build();
52+
53+
try (Spanner spanner =
54+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
55+
InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) {
56+
57+
// Wait for the createInstance operation to finish.
58+
Instance createdInstance =
59+
instanceAdminClient
60+
.createInstanceAsync(
61+
CreateInstanceRequest.newBuilder()
62+
.setParent(ProjectName.of(projectId).toString())
63+
.setInstanceId(instanceId)
64+
.setInstance(instance)
65+
.build())
66+
.get();
67+
System.out.printf("Instance %s was successfully created%n", createdInstance.getName());
68+
} catch (ExecutionException e) {
69+
System.out.printf(
70+
"Error: Creating instance %s failed with error message %s%n",
71+
instance.getName(), e.getMessage());
72+
} catch (InterruptedException e) {
73+
System.out.println("Error: Waiting for createInstance operation to finish was interrupted");
74+
}
75+
}
76+
}
77+
// [END spanner_create_instance_without_default_backup_schedule]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
// [START spanner_update_instance_default_backup_schedule_type]
20+
21+
import com.google.cloud.spanner.Spanner;
22+
import com.google.cloud.spanner.SpannerOptions;
23+
import com.google.cloud.spanner.admin.instance.v1.InstanceAdminClient;
24+
import com.google.common.collect.Lists;
25+
import com.google.protobuf.FieldMask;
26+
import com.google.spanner.admin.instance.v1.Instance;
27+
import com.google.spanner.admin.instance.v1.InstanceConfigName;
28+
import com.google.spanner.admin.instance.v1.InstanceName;
29+
import com.google.spanner.admin.instance.v1.UpdateInstanceRequest;
30+
import java.util.concurrent.ExecutionException;
31+
32+
public class UpdateInstanceDefaultBackupScheduleTypeExample {
33+
34+
static void updateInstanceDefaultBackupScheduleType() {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String projectId = "my-project";
37+
String instanceId = "my-instance";
38+
updateInstanceDefaultBackupScheduleType(projectId, instanceId);
39+
}
40+
41+
static void updateInstanceDefaultBackupScheduleType(String projectId, String instanceId) {
42+
// Set Instance configuration.
43+
int nodeCount = 2;
44+
String displayName = "Updated name";
45+
46+
// Update an Instance object that will be used to update the instance.
47+
Instance instance =
48+
Instance.newBuilder()
49+
.setName(InstanceName.of(projectId, instanceId).toString())
50+
.setDisplayName(displayName)
51+
.setNodeCount(nodeCount)
52+
.setDefaultBackupScheduleType(Instance.DefaultBackupScheduleType.AUTOMATIC)
53+
.setConfig(InstanceConfigName.of(projectId, "regional-us-east4").toString())
54+
.build();
55+
56+
try (Spanner spanner =
57+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
58+
InstanceAdminClient instanceAdminClient = spanner.createInstanceAdminClient()) {
59+
60+
// Wait for the updatedInstance operation to finish.
61+
Instance updatedInstance =
62+
instanceAdminClient
63+
.updateInstanceAsync(
64+
UpdateInstanceRequest.newBuilder()
65+
.setFieldMask(
66+
FieldMask.newBuilder()
67+
.addAllPaths(Lists.newArrayList("default_backup_schedule_type")))
68+
.setInstance(instance)
69+
.build())
70+
.get();
71+
System.out.printf("Instance %s was successfully updated%n", updatedInstance.getName());
72+
} catch (ExecutionException e) {
73+
System.out.printf(
74+
"Error: Updating instance %s failed with error message %s%n",
75+
instance.getName(), e.getMessage());
76+
} catch (InterruptedException e) {
77+
System.out.println("Error: Waiting for updateInstance operation to finish was interrupted");
78+
}
79+
}
80+
}
81+
82+
// [END spanner_update_instance_default_backup_schedule_type]

samples/snippets/src/test/java/com/example/spanner/SpannerSampleIT.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,38 @@ public void testCreateAndUpdateInstanceSample() {
692692
InstanceId.of(dbId.getInstanceId().getProject(), instanceId)));
693693
}
694694

695+
@Test
696+
public void testCreateAndUpdateInstanceDefaultBackupScheduleTypeSample() {
697+
String databaseId = idGenerator.generateDatabaseId();
698+
DatabaseId dbId = DatabaseId.of(projectId, instanceId, databaseId);
699+
700+
String instanceId = formatForTest("sample-inst");
701+
String out =
702+
runSampleRunnable(
703+
() -> {
704+
try {
705+
CreateInstanceWithoutDefaultBackupSchedulesExample
706+
.createInstanceWithoutDefaultBackupSchedules(
707+
dbId.getInstanceId().getProject(), instanceId);
708+
UpdateInstanceDefaultBackupScheduleTypeExample
709+
.updateInstanceDefaultBackupScheduleType(
710+
dbId.getInstanceId().getProject(), instanceId);
711+
} finally {
712+
spanner.getInstanceAdminClient().deleteInstance(instanceId);
713+
}
714+
});
715+
assertThat(out)
716+
.contains(
717+
String.format(
718+
"Instance %s was successfully created",
719+
InstanceId.of(dbId.getInstanceId().getProject(), instanceId)));
720+
assertThat(out)
721+
.contains(
722+
String.format(
723+
"Instance %s was successfully updated",
724+
InstanceId.of(dbId.getInstanceId().getProject(), instanceId)));
725+
}
726+
695727
private static int countOccurrences(String input, String search) {
696728
return input.split(search).length - 1;
697729
}

0 commit comments

Comments
 (0)