Skip to content

Commit 9ef23bb

Browse files
authored
docs(compute-samples): added samples and tests for: GCE instance from… (GoogleCloudPlatform#6552)
* docs(compute-samples): added samples and tests for: GCE instance from template * updated the debian version * docs(compute-samples): added samples and tests for: GCE instance from template * docs(compute-samples): modified enums to use strings and incorporated full-lro * fix(compute-samples): fixed stdout null pointer exception
1 parent 05a17bf commit 9ef23bb

9 files changed

+625
-3
lines changed

compute/cloud-client/src/main/java/compute/CreateInstance.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public static void createInstance(String project, String zone, String instanceNa
6767
.setBoot(true)
6868
.setAutoDelete(true)
6969
.setType(Type.PERSISTENT.toString())
70+
.setDeviceName("disk-1")
7071
.setInitializeParams(
7172
AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage)
7273
.setDiskSizeGb(diskSizeGb).build())
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
18+
package compute;
19+
20+
// [START compute_instances_create_from_template]
21+
22+
import com.google.cloud.compute.v1.InsertInstanceRequest;
23+
import com.google.cloud.compute.v1.Instance;
24+
import com.google.cloud.compute.v1.InstancesClient;
25+
import com.google.cloud.compute.v1.Operation;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
public class CreateInstanceFromTemplate {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException {
33+
/* TODO(developer): Replace these variables before running the sample.
34+
projectId - ID or number of the project you want to use.
35+
zone - Name of the zone you want to check, for example: us-west3-b
36+
instanceName - Name of the new instance.
37+
instanceTemplateURL - URL of the instance template using for creating the new instance.
38+
It can be a full or partial URL.
39+
Examples:
40+
- https://www.googleapis.com/compute/v1/projects/project/global/instanceTemplates/example-instance-template
41+
- projects/project/global/instanceTemplates/example-instance-template
42+
- global/instanceTemplates/example-instance-template
43+
*/
44+
String projectId = "your-project-id";
45+
String zone = "zone-name";
46+
String instanceName = "instance-name";
47+
String instanceTemplateUrl = "instance-template-url";
48+
createInstanceFromTemplate(projectId, zone, instanceName, instanceTemplateUrl);
49+
}
50+
51+
// Create a new instance from template in the specified project and zone.
52+
public static void createInstanceFromTemplate(String projectId, String zone, String instanceName,
53+
String instanceTemplateUrl)
54+
throws IOException, ExecutionException, InterruptedException {
55+
56+
try (InstancesClient instancesClient = InstancesClient.create()) {
57+
58+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
59+
.setProject(projectId)
60+
.setZone(zone)
61+
.setInstanceResource(Instance.newBuilder().setName(instanceName).build())
62+
.setSourceInstanceTemplate(instanceTemplateUrl).build();
63+
64+
Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
65+
66+
if (response.hasError()) {
67+
System.out.println("Instance creation from template failed ! ! " + response);
68+
return;
69+
}
70+
System.out
71+
.printf("Instance creation from template: Operation Status %s: %s ", instanceName,
72+
response.getStatus());
73+
}
74+
}
75+
}
76+
// [END compute_instances_create_from_template]
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
// [START compute_instances_create_from_template_with_overrides]
20+
21+
import com.google.cloud.compute.v1.AttachedDisk;
22+
import com.google.cloud.compute.v1.AttachedDiskInitializeParams;
23+
import com.google.cloud.compute.v1.InsertInstanceRequest;
24+
import com.google.cloud.compute.v1.Instance;
25+
import com.google.cloud.compute.v1.InstanceTemplate;
26+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
27+
import com.google.cloud.compute.v1.InstancesClient;
28+
import com.google.cloud.compute.v1.Operation;
29+
import java.io.IOException;
30+
import java.util.concurrent.ExecutionException;
31+
32+
public class CreateInstanceFromTemplateWithOverrides {
33+
34+
public static void main(String[] args)
35+
throws IOException, ExecutionException, InterruptedException {
36+
/* TODO(developer): Replace these variables before running the sample.
37+
* projectId - ID or number of the project you want to use.
38+
* zone - Name of the zone you want to check, for example: us-west3-b
39+
* instanceName - Name of the new instance.
40+
* instanceTemplateName - Name of the instance template to use when creating the new instance.
41+
* machineType - Machine type you want to set in following format:
42+
* "zones/{zone}/machineTypes/{type_name}". For example:
43+
* "zones/europe-west3-c/machineTypes/f1-micro"
44+
* You can find the list of available machine types using:
45+
* https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
46+
* newDiskSourceImage - Path the the disk image you want to use for your new
47+
* disk. This can be one of the public images
48+
* (like "projects/debian-cloud/global/images/family/debian-10")
49+
* or a private image you have access to.
50+
* You can check the list of available public images using the doc:
51+
* http://cloud.google.com/compute/docs/images
52+
*/
53+
String projectId = "your-project-id";
54+
String zone = "zone-name";
55+
String instanceName = "instance-name";
56+
String instanceTemplateName = "instance-template-name";
57+
58+
createInstanceFromTemplateWithOverrides(projectId, zone, instanceName, instanceTemplateName);
59+
}
60+
61+
// Creates a Compute Engine VM instance from an instance template,
62+
// but overrides the disk and machine type options in the template.
63+
public static void createInstanceFromTemplateWithOverrides(String projectId, String zone,
64+
String instanceName, String instanceTemplateName)
65+
throws IOException, ExecutionException, InterruptedException {
66+
67+
try (InstancesClient instancesClient = InstancesClient.create();
68+
InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
69+
70+
String machineType = "n1-standard-1";
71+
String newDiskSourceImage = "projects/debian-cloud/global/images/family/debian-10";
72+
73+
// Retrieve an instance template.
74+
InstanceTemplate instanceTemplate = instanceTemplatesClient
75+
.get(projectId, instanceTemplateName);
76+
77+
AttachedDisk newdisk = AttachedDisk.newBuilder()
78+
.setInitializeParams(AttachedDiskInitializeParams.newBuilder()
79+
.setDiskSizeGb(10)
80+
.setSourceImage(newDiskSourceImage).build())
81+
.setAutoDelete(true)
82+
.setBoot(false)
83+
.setType(AttachedDisk.Type.PERSISTENT.toString()).build();
84+
85+
Instance instance = Instance.newBuilder()
86+
.setName(instanceName)
87+
.setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType))
88+
// If you override a repeated field, all repeated values
89+
// for that property are replaced with the
90+
// corresponding values provided in the request.
91+
// When adding a new disk to existing disks,
92+
// insert all existing disks as well.
93+
.addAllDisks(instanceTemplate.getProperties().getDisksList())
94+
.addDisks(newdisk)
95+
.build();
96+
97+
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder()
98+
.setProject(projectId)
99+
.setZone(zone)
100+
.setInstanceResource(instance)
101+
.setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build();
102+
103+
Operation response = instancesClient.insertAsync(insertInstanceRequest).get();
104+
105+
if (response.hasError()) {
106+
System.out.println("Instance creation from template with overrides failed ! ! " + response);
107+
return;
108+
}
109+
System.out
110+
.printf("Instance creation from template with overrides: Operation Status %s: %s ",
111+
instanceName, response.getStatus());
112+
}
113+
114+
}
115+
}
116+
// [END compute_instances_create_from_template_with_overrides]
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.InstanceTemplatesClient;
21+
import com.google.cloud.compute.v1.Operation;
22+
import java.io.IOException;
23+
import java.util.concurrent.ExecutionException;
24+
25+
public class DeleteInstanceTemplate {
26+
27+
public static void main(String[] args)
28+
throws IOException, ExecutionException, InterruptedException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
// projectId: project ID or project number of the Cloud project you use.
31+
// templateName: name of the new template to create.
32+
String projectId = "your-project-id";
33+
String templateName = "template-name";
34+
deleteInstanceTemplate(projectId, templateName);
35+
}
36+
37+
// Delete an instance template.
38+
public static void deleteInstanceTemplate(String projectId, String templateName)
39+
throws IOException, ExecutionException, InterruptedException {
40+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
41+
42+
DeleteInstanceTemplateRequest deleteInstanceTemplateRequest = DeleteInstanceTemplateRequest
43+
.newBuilder()
44+
.setProject(projectId)
45+
.setInstanceTemplate(templateName).build();
46+
47+
Operation response = instanceTemplatesClient.deleteAsync(deleteInstanceTemplateRequest).get();
48+
49+
if (response.hasError()) {
50+
System.out.println("Instance template deletion failed ! ! " + response);
51+
return;
52+
}
53+
System.out.printf("Instance template deletion operation status for %s: %s ", templateName,
54+
response.getStatus());
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)