Search in sources :

Example 1 with InsertInstanceTemplateRequest

use of com.google.cloud.compute.v1.InsertInstanceTemplateRequest in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceTemplate method createInstanceTemplateWithDiskType.

public static void createInstanceTemplateWithDiskType(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
        GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
        AttachedDisk disk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(10).setSourceImage("projects/debian-cloud/global/images/family/debian-10").build()).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
        InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setProperties(InstanceProperties.newBuilder().setMachineType("n1-standard-1").addDisks(disk).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).build()).build();
        InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
        Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
        if (response.hasError()) {
            System.out.println("Instance Template creation failed ! ! " + response);
            return;
        }
        System.out.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
    }
}
Also used : AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) GlobalOperationsClient(com.google.cloud.compute.v1.GlobalOperationsClient) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 2 with InsertInstanceTemplateRequest

use of com.google.cloud.compute.v1.InsertInstanceTemplateRequest in project java-docs-samples by GoogleCloudPlatform.

the class CreateTemplateFromInstance method createTemplateFromInstance.

// Create a new instance template based on an existing instance.
// This new template specifies a different boot disk.
public static void createTemplateFromInstance(String projectId, String templateName, String instance) throws IOException, ExecutionException, InterruptedException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
        GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
        SourceInstanceParams sourceInstanceParams = SourceInstanceParams.newBuilder().addDiskConfigs(DiskInstantiationConfig.newBuilder().setDeviceName("disk-1").setInstantiateFrom(InstantiateFrom.CUSTOM_IMAGE.toString()).setCustomImage(String.format("projects/%s/global/images/family/%s", "rocky-linux-cloud", "rocky-linux-8")).setAutoDelete(true).build()).build();
        InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setSourceInstance(instance).setSourceInstanceParams(sourceInstanceParams).build();
        InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
        Operation operation = instanceTemplatesClient.insertCallable().futureCall(insertInstanceTemplateRequest).get();
        Operation response = globalOperationsClient.wait(projectId, operation.getName());
        if (response.hasError()) {
            System.out.println("Instance Template creation failed ! ! " + response);
            return;
        }
        System.out.printf("Instance Template creation operation status %s: %s", templateName, response.getStatus());
    }
}
Also used : SourceInstanceParams(com.google.cloud.compute.v1.SourceInstanceParams) GlobalOperationsClient(com.google.cloud.compute.v1.GlobalOperationsClient) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Example 3 with InsertInstanceTemplateRequest

use of com.google.cloud.compute.v1.InsertInstanceTemplateRequest in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceTemplate method createInstanceTemplate.

/*
    Create a new instance template with the provided name and a specific
    instance configuration.
   */
public static void createInstanceTemplate(String projectId, String templateName) throws IOException, ExecutionException, InterruptedException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
        String machineType = "e2-standard-4";
        String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
        // The template describes the size and source image of the boot disk
        // to attach to the instance.
        AttachedDisk attachedDisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(250).build()).setAutoDelete(true).setBoot(true).build();
        // The template connects the instance to the `default` network,
        // without specifying a subnetwork.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName("global/networks/default").addAccessConfigs(AccessConfig.newBuilder().setName("External NAT").setType(AccessConfig.Type.ONE_TO_ONE_NAT.toString()).setNetworkTier(NetworkTier.PREMIUM.toString()).build()).build();
        InstanceProperties instanceProperties = InstanceProperties.newBuilder().addDisks(attachedDisk).setMachineType(machineType).addNetworkInterfaces(networkInterface).build();
        InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(InstanceTemplate.newBuilder().setName(templateName).setProperties(instanceProperties).build()).build();
        // Create the Instance Template.
        Operation response = instanceTemplatesClient.insertAsync(insertInstanceTemplateRequest).get();
        if (response.hasError()) {
            System.out.println("Instance Template creation failed ! ! " + response);
            return;
        }
        System.out.printf("Instance Template Operation Status %s: %s", templateName, response.getStatus());
    }
}
Also used : InstanceProperties(com.google.cloud.compute.v1.InstanceProperties) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient)

Example 4 with InsertInstanceTemplateRequest

use of com.google.cloud.compute.v1.InsertInstanceTemplateRequest in project java-docs-samples by GoogleCloudPlatform.

the class CreateTemplateWithSubnet method createTemplateWithSubnet.

// Create an instance template that uses a provided subnet.
public static void createTemplateWithSubnet(String projectId, String network, String subnetwork, String templateName) throws IOException, ExecutionException, InterruptedException {
    try (InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create();
        GlobalOperationsClient globalOperationsClient = GlobalOperationsClient.create()) {
        AttachedDisk disk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(String.format("projects/%s/global/images/family/%s", "debian-cloud", "debian-11")).setDiskSizeGb(250).build()).setAutoDelete(true).setBoot(true).build();
        InstanceProperties instanceProperties = InstanceProperties.newBuilder().addDisks(disk).setMachineType("e2-standard-4").addNetworkInterfaces(NetworkInterface.newBuilder().setNetwork(network).setSubnetwork(subnetwork).build()).build();
        InstanceTemplate instanceTemplate = InstanceTemplate.newBuilder().setName(templateName).setProperties(instanceProperties).build();
        InsertInstanceTemplateRequest insertInstanceTemplateRequest = InsertInstanceTemplateRequest.newBuilder().setProject(projectId).setInstanceTemplateResource(instanceTemplate).build();
        Operation operation = instanceTemplatesClient.insertCallable().futureCall(insertInstanceTemplateRequest).get();
        Operation response = globalOperationsClient.wait(projectId, operation.getName());
        if (response.hasError()) {
            System.out.println("Template creation from subnet failed ! ! " + response);
            return;
        }
        System.out.printf("Template creation from subnet operation status %s: %s", templateName, response.getStatus());
    }
}
Also used : InstanceProperties(com.google.cloud.compute.v1.InstanceProperties) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) GlobalOperationsClient(com.google.cloud.compute.v1.GlobalOperationsClient) Operation(com.google.cloud.compute.v1.Operation) InsertInstanceTemplateRequest(com.google.cloud.compute.v1.InsertInstanceTemplateRequest) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

Aggregations

InsertInstanceTemplateRequest (com.google.cloud.compute.v1.InsertInstanceTemplateRequest)4 InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)4 Operation (com.google.cloud.compute.v1.Operation)4 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)3 GlobalOperationsClient (com.google.cloud.compute.v1.GlobalOperationsClient)3 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)3 InstanceProperties (com.google.cloud.compute.v1.InstanceProperties)2 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)1 SourceInstanceParams (com.google.cloud.compute.v1.SourceInstanceParams)1