Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6bb99e7

Browse files
authored
docs(compute-samples): init add create templates from instance and su… (GoogleCloudPlatform#6683)
* docs(compute-samples): init add create templates from instance and subnet * docs(compute-samples): lint fix
1 parent 31a721b commit 6bb99e7

File tree

6 files changed

+346
-28
lines changed

6 files changed

+346
-28
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_from_instance]
20+
21+
import com.google.cloud.compute.v1.DiskInstantiationConfig;
22+
import com.google.cloud.compute.v1.DiskInstantiationConfig.InstantiateFrom;
23+
import com.google.cloud.compute.v1.GlobalOperationsClient;
24+
import com.google.cloud.compute.v1.InsertInstanceTemplateRequest;
25+
import com.google.cloud.compute.v1.InstanceTemplate;
26+
import com.google.cloud.compute.v1.InstanceTemplatesClient;
27+
import com.google.cloud.compute.v1.Operation;
28+
import com.google.cloud.compute.v1.SourceInstanceParams;
29+
import java.io.IOException;
30+
import java.util.concurrent.ExecutionException;
31+
32+
public class CreateTemplateFromInstance {
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: project ID or project number of the Cloud project you use.
38+
// instance: the instance to base the new template on. This value uses the following format:
39+
// **NOTE**: "projects/{project}/zones/{zone}/instances/{instance_name}"
40+
// templateName: name of the new template to create.
41+
String projectId = "your-project-id";
42+
String templateName = "template-name";
43+
String instance = String.format("projects/%s/zones/%s/instances/%s", projectId, "zone",
44+
"instanceName");
45+
createTemplateFromInstance(projectId, templateName, instance);
46+
}
47+
48+
// Create a new instance template based on an existing instance.
49+
// This new template specifies a different boot disk.
50+
public static void createTemplateFromInstance(String projectId, String templateName,
51+
String instance)
52+
throws IOException, ExecutionException, InterruptedException {
53+
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
54+
GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
55+
56+
SourceInstanceParams sourceInstanceParams = SourceInstanceParams.newBuilder()
57+
.addDiskConfigs(DiskInstantiationConfig.newBuilder()
58+
// Device name must match the name of a disk attached to the instance you are
59+
// basing your template on.
60+
.setDeviceName("disk-1")
61+
// Replace the original boot disk image used in your instance
62+
// with a Rocky Linux image.
63+
.setInstantiateFrom(InstantiateFrom.CUSTOM_IMAGE.toString())
64+
.setCustomImage(
65+
String.format("projects/%s/global/images/family/%s", "rocky-linux-cloud",
66+
"rocky-linux-8"))
67+
// Override the AutoDelete setting.
68+
.setAutoDelete(true).build())
69+
.build();
70+
71+
InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder()
72+
.setName(templateName)
73+
.setSourceInstance(instance)
74+
.setSourceInstanceParams(sourceInstanceParams)
75+
.build();
76+
77+
InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest
78+
.newBuilder()
79+
.setProject(projectId)
80+
.setInstanceTemplateResource(instanceTemplate)
81+
.build();
82+
83+
Operation operation = instanceTemplatesClient.insertCallable()
84+
.futureCall(insertInstanceTemplateRequest).get();
85+
86+
Operation response = globalOperationsClient.wait(projectId, operation.getName());
87+
88+
if (response.hasError()) {
89+
System.out.println("Instance Template creation failed ! ! " + response);
90+
return;
91+
}
92+
System.out.printf("Instance Template creation operation status %s: %s", templateName,
93+
response.getStatus());
94+
}
95+
}
96+
}
97+
// [END compute_template_create_from_instance]
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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]

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Google LLC
2+
* Copyright 2022 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import com.google.cloud.compute.v1.AggregatedListInstancesRequest;
2222
import com.google.cloud.compute.v1.Instance;
2323
import com.google.cloud.compute.v1.InstancesClient;
24+
import com.google.cloud.compute.v1.InstancesClient.AggregatedListPagedResponse;
2425
import com.google.cloud.compute.v1.InstancesScopedList;
2526
import java.io.IOException;
2627
import java.util.Map;
@@ -34,10 +35,10 @@ public static void main(String[] args) throws IOException {
3435
}
3536

3637
// List all instances in the specified project ID.
37-
public static void listAllInstances(String project) throws IOException {
38+
public static AggregatedListPagedResponse listAllInstances(String project) throws IOException {
3839
// Initialize client that will be used to send requests. This client only needs to be created
3940
// once, and can be reused for multiple requests. After completing all of your requests, call
40-
// the `instancesClient.close()` method on the client to
41+
// the `instancesClient.close()` method on the client to
4142
// safely clean up any remaining background resources.
4243
try (InstancesClient instancesClient = InstancesClient.create()) {
4344

@@ -68,8 +69,9 @@ public static void listAllInstances(String project) throws IOException {
6869
}
6970
}
7071
System.out.println("####### Listing all instances complete #######");
72+
return response;
7173
}
7274
}
7375

7476
}
75-
// [END compute_instances_list_all]
77+
// [END compute_instances_list_all]

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Google LLC
2+
* Copyright 2022 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import com.google.cloud.compute.v1.InstanceTemplate;
2020
import com.google.cloud.compute.v1.InstanceTemplatesClient;
21+
import com.google.cloud.compute.v1.InstanceTemplatesClient.ListPagedResponse;
2122
import java.io.IOException;
2223

2324
public class ListInstanceTemplates {
@@ -30,14 +31,15 @@ public static void main(String[] args) throws IOException {
3031
}
3132

3233
// Get a list of InstanceTemplate objects available in a project.
33-
public static void listInstanceTemplates(String projectId) throws IOException {
34+
public static ListPagedResponse listInstanceTemplates(String projectId) throws IOException {
3435
try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
3536
int count = 0;
3637
System.out.println("Listing instance templates...");
37-
for (InstanceTemplate instanceTemplate : instanceTemplatesClient.list(projectId)
38-
.iterateAll()) {
38+
ListPagedResponse templates = instanceTemplatesClient.list(projectId);
39+
for (InstanceTemplate instanceTemplate : templates.iterateAll()) {
3940
System.out.printf("%s. %s%n", ++count, instanceTemplate.getName());
4041
}
42+
return templates;
4143
}
4244
}
4345
}

0 commit comments

Comments
 (0)