Skip to content

Commit 7c47898

Browse files
committed
docs(compute-samples): added instance template create from subnet
1 parent 34cc557 commit 7c47898

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.AttachedDisk;
20+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
21+
import com.google.cloud.compute.v1.GlobalOperationsClient;
22+
import com.google.cloud.compute.v1.InsertInstanceTemplateRequest;
23+
import com.google.cloud.compute.v1.InstanceProperties;
24+
import com.google.cloud.compute.v1.InstanceTemplate;
25+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
26+
import com.google.cloud.compute.v1.NetworkInterface;
27+
import com.google.cloud.compute.v1.Operation;
28+
import java.io.IOException;
29+
import java.util.concurrent.ExecutionException;
30+
31+
public class CreateTemplateWithSubnet {
32+
33+
public static void main(String[] args)
34+
throws IOException, ExecutionException, InterruptedException {
35+
/* TODO(developer): Replace these variables before running the sample.
36+
projectId: project ID or project number of the Cloud project you use.
37+
network: the network to be used in the new template. This value uses
38+
the following format: "projects/{project}/global/networks/{network}"
39+
subnetwork: the subnetwork to be used in the new template. This value
40+
uses the following format: "projects/{project}/regions/{region}/subnetworks/{subnetwork}"
41+
templateName: name of the new template to create.
42+
*/
43+
String projectId = "your-project-id";
44+
String network = "projects/{project}/global/networks/{network}";
45+
String subnetwork = "projects/{project}/regions/{region}/subnetworks/{subnetwork}";
46+
String templateName = "template-name";
47+
createTemplateWithSubnet(projectId, network, subnetwork, templateName);
48+
}
49+
50+
// Create an instance template that uses a provided subnet.
51+
public static void createTemplateWithSubnet(String projectId, String network, String subnetwork,
52+
String templateName)
53+
throws IOException, ExecutionException, InterruptedException {
54+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
55+
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
56+
57+
AttachedDisk disk = AttachedDisk.newBuilder()
58+
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
59+
.setSourceImage("projects/debian-cloud/global/images/family/debian-11")
60+
.setDiskSizeGb(250).build())
61+
.setAutoDelete(true)
62+
.setBoot(true).build();
63+
64+
InstanceProperties instanceProperties = InstanceProperties.newBuilder()
65+
.addDisks(disk)
66+
.setMachineType("e2-standard-4")
67+
.addNetworkInterfaces(NetworkInterface.newBuilder()
68+
.setNetwork(network)
69+
.setSubnetwork(subnetwork).build()).build();
70+
71+
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder()
72+
.setName(templateName)
73+
.setProperties(instanceProperties).build();
74+
75+
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
76+
.newBuilder()
77+
.setProject(projectId)
78+
.setInstanceTemplateResource(instanceTemplate).build();
79+
80+
Operation operation = instanceTemplatesClient.insertCallable()
81+
.futureCall(insertInstanceTemplateRequest).get();
82+
83+
Operation response = globalOperationsClient.wait(projectId, operation.getName());
84+
85+
if (response.hasError()) {
86+
System.out.println("Template creation from subnet failed ! ! " + response);
87+
return;
88+
}
89+
System.out.println("Template creation from subnet operation status: " + response.getStatus());
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)