|
| 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] |
0 commit comments