1
+ /*
2
+ * Copyright 2022 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
+ // [START compute_template_create_with_subnet]
20
+
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 CreateTemplateWithSubnet {
34
+
35
+ public static void main (String [] args )
36
+ throws IOException , ExecutionException , InterruptedException {
37
+ /*
38
+ TODO(developer): Replace these variables before running the sample.
39
+ projectId: project ID or project number of the Cloud project you use.
40
+ network: the network to be used in the new template. This value uses
41
+ the following format: "projects/{project}/global/networks/{network}"
42
+ subnetwork: the subnetwork to be used in the new template. This value
43
+ uses the following format: "projects/{project}/regions/{region}/subnetworks/{subnetwork}"
44
+ templateName: name of the new template to create.
45
+ */
46
+ String projectId = "your-project-id" ;
47
+ String network = String .format ("projects/%s/global/networks/%s" , projectId , "network" );
48
+ String subnetwork = String .format ("projects/%s/regions/%s/subnetworks/%s" , projectId , "region" ,
49
+ "subnetwork" );
50
+ String templateName = "template-name" ;
51
+ createTemplateWithSubnet (projectId , network , subnetwork , templateName );
52
+ }
53
+
54
+ // Create an instance template that uses a provided subnet.
55
+ public static void createTemplateWithSubnet (String projectId , String network , String subnetwork ,
56
+ String templateName )
57
+ throws IOException , ExecutionException , InterruptedException {
58
+ try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient .create ();
59
+ GlobalOperationsClient globalOperationsClient = GlobalOperationsClient .create ()) {
60
+
61
+ AttachedDisk disk = AttachedDisk .newBuilder ()
62
+ .setInitializeParams (AttachedDiskInitializeParams .newBuilder ()
63
+ .setSourceImage (
64
+ String .format ("projects/%s/global/images/family/%s" , "debian-cloud" , "debian-11" ))
65
+ .setDiskSizeGb (250 ).build ())
66
+ .setAutoDelete (true )
67
+ .setBoot (true )
68
+ .build ();
69
+
70
+ InstanceProperties instanceProperties = InstanceProperties .newBuilder ()
71
+ .addDisks (disk )
72
+ .setMachineType ("e2-standard-4" )
73
+ .addNetworkInterfaces (NetworkInterface .newBuilder ()
74
+ .setNetwork (network )
75
+ .setSubnetwork (subnetwork ).build ())
76
+ .build ();
77
+
78
+ InstanceTemplate instanceTemplate = InstanceTemplate .newBuilder ()
79
+ .setName (templateName )
80
+ .setProperties (instanceProperties )
81
+ .build ();
82
+
83
+ InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
84
+ .newBuilder ()
85
+ .setProject (projectId )
86
+ .setInstanceTemplateResource (instanceTemplate )
87
+ .build ();
88
+
89
+ Operation operation = instanceTemplatesClient .insertCallable ()
90
+ .futureCall (insertInstanceTemplateRequest ).get ();
91
+
92
+ Operation response = globalOperationsClient .wait (projectId , operation .getName ());
93
+
94
+ if (response .hasError ()) {
95
+ System .out .println ("Template creation from subnet failed ! ! " + response );
96
+ return ;
97
+ }
98
+ System .out .printf ("Template creation from subnet operation status %s: %s" , templateName ,
99
+ response .getStatus ());
100
+ }
101
+ }
102
+ }
103
+ // [END compute_template_create_with_subnet]
0 commit comments