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 compute ;
18
+
19
+ import com .google .cloud .compute .v1 .AccessConfig ;
20
+ import com .google .cloud .compute .v1 .AccessConfig .NetworkTier ;
21
+ import com .google .cloud .compute .v1 .AttachedDisk ;
22
+ import com .google .cloud .compute .v1 .AttachedDiskInitializeParams ;
23
+ import com .google .cloud .compute .v1 .GlobalOperationsClient ;
24
+ import com .google .cloud .compute .v1 .InsertInstanceTemplateRequest ;
25
+ import com .google .cloud .compute .v1 .InstanceProperties ;
26
+ import com .google .cloud .compute .v1 .InstanceTemplate ;
27
+ import com .google .cloud .compute .v1 .InstanceTemplatesClient ;
28
+ import com .google .cloud .compute .v1 .NetworkInterface ;
29
+ import com .google .cloud .compute .v1 .Operation ;
30
+ import java .io .IOException ;
31
+ import java .util .concurrent .ExecutionException ;
32
+
33
+ public class CreateInstanceTemplate {
34
+
35
+ public static void main (String [] args )
36
+ throws IOException , ExecutionException , InterruptedException {
37
+ // TODO(developer): Replace these variables before running the sample.
38
+ // projectId: project ID or project number of the Cloud project you use.
39
+ // templateName: name of the new template to create.
40
+ String projectId = "your-project-id" ;
41
+ String templateName = "template-name" ;
42
+ createInstanceTemplate (projectId , templateName );
43
+ }
44
+
45
+ /*
46
+ Create a new instance template with the provided name and a specific
47
+ instance configuration.
48
+ */
49
+ public static void createInstanceTemplate (String projectId , String templateName )
50
+ throws IOException , ExecutionException , InterruptedException {
51
+ try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient .create ()) {
52
+
53
+ String machineType = "e2-standard-4" ;
54
+ String sourceImage = "projects/debian-cloud/global/images/family/debian-11" ;
55
+
56
+ // The template describes the size and source image of the boot disk
57
+ // to attach to the instance.
58
+ AttachedDisk attachedDisk = AttachedDisk .newBuilder ()
59
+ .setInitializeParams (AttachedDiskInitializeParams .newBuilder ()
60
+ .setSourceImage (sourceImage )
61
+ .setDiskSizeGb (250 ).build ())
62
+ .setAutoDelete (true )
63
+ .setBoot (true ).build ();
64
+
65
+ // The template connects the instance to the `default` network,
66
+ // without specifying a subnetwork.
67
+ NetworkInterface networkInterface = NetworkInterface .newBuilder ()
68
+ .setName ("global/networks/default" )
69
+ // The template lets the instance use an external IP address.
70
+ .addAccessConfigs (AccessConfig .newBuilder ()
71
+ .setName ("External NAT" )
72
+ .setType (AccessConfig .Type .ONE_TO_ONE_NAT .toString ())
73
+ .setNetworkTier (NetworkTier .PREMIUM .toString ()).build ()).build ();
74
+
75
+ InstanceProperties instanceProperties = InstanceProperties .newBuilder ()
76
+ .addDisks (attachedDisk )
77
+ .setMachineType (machineType )
78
+ .addNetworkInterfaces (networkInterface ).build ();
79
+
80
+ InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
81
+ .newBuilder ()
82
+ .setProject (projectId )
83
+ .setInstanceTemplateResource (InstanceTemplate .newBuilder ()
84
+ .setName (templateName )
85
+ .setProperties (instanceProperties ).build ()).build ();
86
+
87
+ // Create the Instance Template.
88
+ Operation response = instanceTemplatesClient .insertAsync (insertInstanceTemplateRequest ).get ();
89
+
90
+ if (response .hasError ()) {
91
+ System .out .println ("Instance Template creation failed ! ! " + response );
92
+ return ;
93
+ }
94
+ System .out
95
+ .printf ("Instance Template Operation Status %s: %s" , templateName , response .getStatus ());
96
+ }
97
+ }
98
+
99
+ public static void createInstanceTemplateWithDiskType (String projectId , String templateName )
100
+ throws IOException , ExecutionException , InterruptedException {
101
+ try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient .create ();
102
+ GlobalOperationsClient globalOperationsClient = GlobalOperationsClient .create ()) {
103
+
104
+ AttachedDisk disk = AttachedDisk .newBuilder ()
105
+ .setInitializeParams (AttachedDiskInitializeParams .newBuilder ()
106
+ .setDiskSizeGb (10 )
107
+ .setSourceImage ("projects/debian-cloud/global/images/family/debian-10" ).build ())
108
+ .setAutoDelete (true )
109
+ .setBoot (true )
110
+ .setType (AttachedDisk .Type .PERSISTENT .toString ()).build ();
111
+
112
+ InstanceTemplate instanceTemplate = InstanceTemplate .newBuilder ()
113
+ .setName (templateName )
114
+ .setProperties (InstanceProperties .newBuilder ()
115
+ .setMachineType ("n1-standard-1" )
116
+ .addDisks (disk )
117
+ .addNetworkInterfaces (NetworkInterface .newBuilder ()
118
+ .setName ("global/networks/default" ).build ()).build ()).build ();
119
+
120
+ InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
121
+ .newBuilder ()
122
+ .setProject (projectId )
123
+ .setInstanceTemplateResource (instanceTemplate ).build ();
124
+
125
+ Operation response = instanceTemplatesClient .insertAsync (insertInstanceTemplateRequest ).get ();
126
+
127
+ if (response .hasError ()) {
128
+ System .out .println ("Instance Template creation failed ! ! " + response );
129
+ return ;
130
+ }
131
+ System .out
132
+ .printf ("Instance Template Operation Status %s: %s" , templateName , response .getStatus ());
133
+ }
134
+ }
135
+ }
0 commit comments