|
| 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 | +} |
0 commit comments