1
+ /*
2
+ * Copyright 2021 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_with_autoscaling]
20
+
21
+ import com .google .api .gax .longrunning .OperationFuture ;
22
+ import com .google .cloud .spanner .Instance ;
23
+ import com .google .cloud .spanner .InstanceAdminClient ;
24
+ import com .google .cloud .spanner .InstanceConfigId ;
25
+ import com .google .cloud .spanner .InstanceId ;
26
+ import com .google .cloud .spanner .InstanceInfo ;
27
+ import com .google .cloud .spanner .Spanner ;
28
+ import com .google .cloud .spanner .SpannerOptions ;
29
+ import com .google .spanner .admin .instance .v1 .AutoscalingConfig ;
30
+ import com .google .spanner .admin .instance .v1 .CreateInstanceMetadata ;
31
+
32
+ class CreateInstanceWithAutoscalingExample {
33
+
34
+ static void createAutoscalingInstance () {
35
+ // TODO(developer): Replace these variables before running the sample.
36
+ String projectId = "my-project" ;
37
+ String instanceId = "my-instance" ;
38
+ createAutoscalingInstance (projectId , instanceId );
39
+ }
40
+
41
+ static void createAutoscalingInstance (String projectId , String instanceId ) {
42
+ Spanner spanner = SpannerOptions .newBuilder ().setProjectId (projectId ).build ().getService ();
43
+ InstanceAdminClient instanceAdminClient = spanner .getInstanceAdminClient ();
44
+
45
+ // Set Instance configuration.
46
+ String configId = "regional-us-central1" ;
47
+ String displayName = "Descriptive name" ;
48
+ // This will create an autoscaling config that can be passed to the InstanceInfo.
49
+ AutoscalingConfig autoscaling_config = AutoscalingConfig .newBuilder ()
50
+ .setAutoscalingLimits (AutoscalingConfig .AutoscalingLimits .newBuilder ()
51
+ .setMinNodes (2 )
52
+ .setMaxNodes (10 ))
53
+ .setAutoscalingTargets (AutoscalingConfig .AutoscalingTargets .newBuilder ()
54
+ .setHighPriorityCpuUtilizationPercent (65 )
55
+ .setStorageUtilizationPercent (95 ))
56
+ .build ();
57
+
58
+ try {
59
+ // Creates a new instance
60
+ System .out .printf ("Creating autoscaling instance %s.%n" , instanceId );
61
+ OperationFuture <Instance , CreateInstanceMetadata > operation =
62
+ instanceAdminClient .createInstance (InstanceInfo
63
+ .newBuilder (InstanceId .of (projectId , instanceId ))
64
+ .setInstanceConfigId (InstanceConfigId .of (projectId , configId ))
65
+ .setDisplayName (displayName )
66
+ .setAutoscalingConfig (autoscaling_config )
67
+ .build ());
68
+
69
+ // Wait for the createInstance operation to finish.
70
+ System .out .printf ("Waiting for operation on %s to complete...%n" , instanceId );
71
+ Instance createdInstance = operation .get ();
72
+
73
+ System .out .printf ("Created autoscaling instance %s.%n" , createdInstance .getId ().getInstance ());
74
+
75
+ Instance instance = instanceAdminClient .getInstance (instanceId );
76
+ System .out .printf ("Instance %s autoscaling config: %s.%n" , instance .getId ().getInstance (),
77
+ instance .getAutoscalingConfig ());
78
+ } catch (Exception e ) {
79
+ System .out .printf ("Error: %s.%n" , e .getMessage ());
80
+ }
81
+ spanner .close ();
82
+ }
83
+ }
84
+ //[END spanner_create_instance_with_autoscaling]
85
+
0 commit comments