Skip to content

Commit 34cc557

Browse files
committed
docs(compute-samples): added instance template samples
1 parent 1ae863d commit 34cc557

File tree

5 files changed

+336
-0
lines changed

5 files changed

+336
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
53+
54+
String machineType = "e2-standard-4";
55+
String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
56+
57+
// The template describes the size and source image of the boot disk
58+
// to attach to the instance.
59+
AttachedDisk attachedDisk = AttachedDisk.newBuilder()
60+
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
61+
.setSourceImage(sourceImage)
62+
.setDiskSizeGb(250).build())
63+
.setAutoDelete(true)
64+
.setBoot(true).build();
65+
66+
// The template connects the instance to the `default` network,
67+
// without specifying a subnetwork.
68+
NetworkInterface networkInterface = NetworkInterface.newBuilder()
69+
.setName("global/networks/default")
70+
// The template lets the instance use an external IP address.
71+
.addAccessConfigs(AccessConfig.newBuilder()
72+
.setName("External NAT")
73+
.setType(AccessConfig.Type.ONE_TO_ONE_NAT)
74+
.setNetworkTier(NetworkTier.PREMIUM).build()).build();
75+
76+
InstanceProperties instanceProperties = InstanceProperties.newBuilder()
77+
.addDisks(attachedDisk)
78+
.setMachineType(machineType)
79+
.addNetworkInterfaces(networkInterface).build();
80+
81+
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
82+
.newBuilder()
83+
.setProject(projectId)
84+
.setInstanceTemplateResource(InstanceTemplate.newBuilder()
85+
.setName(templateName)
86+
.setProperties(instanceProperties).build()).build();
87+
88+
// Create the Instance Template.
89+
Operation operation = instanceTemplatesClient.insertCallable()
90+
.futureCall(insertInstanceTemplateRequest).get();
91+
// Wait for the operation to complete.
92+
Operation response = globalOperationsClient.wait(projectId, operation.getName());
93+
94+
if (response.hasError()) {
95+
System.out.println("Instance Template creation failed ! ! " + response);
96+
return;
97+
}
98+
System.out.println("Instance Template Operation Status: " + response.getStatus());
99+
}
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.DiskInstantiationConfig;
20+
import com.google.cloud.compute.v1.DiskInstantiationConfig.InstantiateFrom;
21+
import com.google.cloud.compute.v1.GlobalOperationsClient;
22+
import com.google.cloud.compute.v1.InsertInstanceTemplateRequest;
23+
import com.google.cloud.compute.v1.InstanceTemplate;
24+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
25+
import com.google.cloud.compute.v1.Operation;
26+
import com.google.cloud.compute.v1.SourceInstanceParams;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
30+
public class CreateTemplateFromInstance {
31+
32+
public static void main(String[] args)
33+
throws IOException, ExecutionException, InterruptedException {
34+
// TODO(developer): Replace these variables before running the sample.
35+
// projectId: project ID or project number of the Cloud project you use.
36+
// instance: the instance to base the new template on. This value uses the following format:
37+
// **NOTE**: "projects/{project}/zones/{zone}/instances/{instance_name}"
38+
// templateName: name of the new template to create.
39+
String projectId = "";
40+
String templateName = "";
41+
String instance = "projects/{project}/zones/{zone}/instances/{instance_name}";
42+
createTemplateFromInstance(projectId, templateName, instance);
43+
}
44+
45+
// Create a new instance template based on an existing instance.
46+
// This new template specifies a different boot disk.
47+
public static void createTemplateFromInstance(String projectId, String templateName,
48+
String instance)
49+
throws IOException, ExecutionException, InterruptedException {
50+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
51+
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
52+
53+
SourceInstanceParams sourceInstanceParams = SourceInstanceParams.newBuilder()
54+
.addDiskConfigs(DiskInstantiationConfig.newBuilder()
55+
// Device name must match the name of a disk attached to the instance you are
56+
// basing your template on.
57+
.setDeviceName("disk-1")
58+
// Replace the original boot disk image used in your instance with a Rocky Linux image.
59+
.setInstantiateFrom(InstantiateFrom.CUSTOM_IMAGE)
60+
.setCustomImage("projects/rocky-linux-cloud/global/images/family/rocky-linux-8")
61+
// Override the AutoDelete setting.
62+
.setAutoDelete(true).build()).build();
63+
64+
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder()
65+
.setName(templateName)
66+
.setSourceInstance(instance)
67+
.setSourceInstanceParams(sourceInstanceParams).build();
68+
69+
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
70+
.newBuilder()
71+
.setProject(projectId)
72+
.setInstanceTemplateResource(instanceTemplate).build();
73+
74+
Operation operation = instanceTemplatesClient.insertCallable()
75+
.futureCall(insertInstanceTemplateRequest).get();
76+
77+
Operation response = globalOperationsClient.wait(projectId, operation.getName());
78+
79+
if (response.hasError()) {
80+
System.out.println("Instance creation failed ! ! " + response);
81+
return;
82+
}
83+
System.out.println("Instance creation operation status: " + response.getStatus());
84+
}
85+
}
86+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.DeleteInstanceTemplateRequest;
20+
import com.google.cloud.compute.v1.GlobalOperationsClient;
21+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
22+
import com.google.cloud.compute.v1.Operation;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
public class DeleteInstanceTemplate {
27+
28+
public static void main(String[] args)
29+
throws IOException, ExecutionException, InterruptedException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
// projectId: project ID or project number of the Cloud project you use.
32+
// templateName: name of the new template to create.
33+
String projectId = "your-project-id";
34+
String templateName = "template-name";
35+
deleteInstanceTemplate(projectId, templateName);
36+
}
37+
38+
// Delete an instance template.
39+
public static void deleteInstanceTemplate(String projectId, String templateName)
40+
throws IOException, ExecutionException, InterruptedException {
41+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
42+
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
43+
44+
DeleteInstanceTemplateRequest deleteInstanceTemplateRequest = DeleteInstanceTemplateRequest.newBuilder()
45+
.setProject(projectId)
46+
.setInstanceTemplate(templateName).build();
47+
48+
Operation operation = instanceTemplatesClient.deleteCallable().futureCall(deleteInstanceTemplateRequest).get();
49+
50+
Operation response = globalOperationsClient.wait(projectId, operation.getName());
51+
52+
if (response.hasError()) {
53+
System.out.println("Instance deletion failed ! ! " + response);
54+
return;
55+
}
56+
System.out.println("Instance deletion operation status: " + response.getStatus());
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.GetInstanceTemplateRequest;
20+
import com.google.cloud.compute.v1.InstanceTemplate;
21+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
22+
import java.io.IOException;
23+
24+
public class GetInstanceTemplate {
25+
26+
public static void main(String[] args) throws IOException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
// projectId: project ID or project number of the Cloud project you use.
29+
// templateName: name of the new template to retrieve.
30+
String projectId = "your-project-id";
31+
String templateName = "template-name";
32+
getInstanceTemplate(projectId, templateName);
33+
}
34+
35+
// Retrieve an instance template, which you can use to create virtual machine
36+
// (VM) instances and managed instance groups (MIGs).
37+
public static void getInstanceTemplate(String projectId, String templateName) throws IOException {
38+
try(InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
39+
40+
GetInstanceTemplateRequest getInstanceTemplateRequest = GetInstanceTemplateRequest.newBuilder()
41+
.setProject(projectId)
42+
.setInstanceTemplate(templateName).build();
43+
44+
InstanceTemplate instanceTemplate = instanceTemplatesClient.get(getInstanceTemplateRequest);
45+
System.out.println("Instance Template retrieved: " + instanceTemplate.getName());
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.InstanceTemplate;
20+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
21+
import java.io.IOException;
22+
23+
public class ListInstanceTemplates {
24+
25+
public static void main(String[] args) throws IOException {
26+
// TODO(developer): Replace these variables before running the sample.
27+
// projectId: project ID or project number of the Cloud project you use.
28+
String projectId = "your-project-id";
29+
listInstanceTemplates(projectId);
30+
}
31+
32+
// Get a list of InstanceTemplate objects available in a project.
33+
public static void listInstanceTemplates(String projectId) throws IOException {
34+
try(InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
35+
int count = 0;
36+
System.out.println("Listing instance templates...");
37+
for(InstanceTemplate instanceTemplate : instanceTemplatesClient.list(projectId).iterateAll()) {
38+
System.out.printf("%s. %s%n", ++count, instanceTemplate.getName());
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)