Search in sources :

Example 26 with Disk

use of com.google.cloud.compute.v1.Disk 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, TimeoutException {
    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(3, TimeUnit.MINUTES);
        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)

Example 27 with Disk

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

the class CreatePreemptibleInstance method createPremptibleInstance.

// Send an instance creation request with preemptible settings to the Compute Engine API
// and wait for it to complete.
public static void createPremptibleInstance(String projectId, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    String machineType = String.format("zones/%s/machineTypes/e2-small", zone);
    String sourceImage = "projects/debian-cloud/global/images/family/debian-11";
    long diskSizeGb = 10L;
    String networkName = "default";
    try (InstancesClient instancesClient = InstancesClient.create()) {
        AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(AttachedDisk.Type.PERSISTENT.toString()).setInitializeParams(// Describe the size and source image of the boot disk to attach to the instance.
        AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).build();
        // Use the default VPC network.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
        // Collect information into the Instance object.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).setScheduling(Scheduling.newBuilder().setPreemptible(true).build()).build();
        System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
        // Prepare the request to insert an instance.
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instanceResource).build();
        // Wait for the create operation to complete.
        Operation response = instancesClient.insertAsync(insertInstanceRequest).get(3, TimeUnit.MINUTES);
        ;
        if (response.hasError()) {
            System.out.println("Instance creation failed ! ! " + response);
            return;
        }
        System.out.printf("Instance created : %s\n", instanceName);
        System.out.println("Operation Status: " + response.getStatus());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Example 28 with Disk

use of com.google.cloud.compute.v1.Disk in project java-compute by googleapis.

the class ITSmokeInstancesTest method insertInstance.

private Instance insertInstance() throws ExecutionException, InterruptedException {
    Instance instanceResource = Instance.newBuilder().setName(INSTANCE).setMachineType(MACHINE_TYPE).addDisks(DISK).addNetworkInterfaces(NETWORK_INTERFACE).setDescription("test").build();
    instancesClient.insertAsync(DEFAULT_PROJECT, DEFAULT_ZONE, instanceResource).get();
    instances.add(instanceResource);
    return getInstance();
}
Also used : Instance(com.google.cloud.compute.v1.Instance)

Aggregations

Operation (com.google.cloud.compute.v1.Operation)22 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)12 Instance (com.google.cloud.compute.v1.Instance)12 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)8 DisksClient (com.google.cloud.compute.v1.DisksClient)7 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)7 Image (com.google.cloud.compute.v1.Image)5 ImagesClient (com.google.cloud.compute.v1.ImagesClient)5 InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)5 InsertInstanceTemplateRequest (com.google.cloud.compute.v1.InsertInstanceTemplateRequest)4 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)4 Disk (com.google.cloud.compute.v1.Disk)3 GlobalOperationsClient (com.google.cloud.compute.v1.GlobalOperationsClient)3 Test (org.junit.Test)3 InstanceProperties (com.google.cloud.compute.v1.InstanceProperties)2 Snapshot (com.google.cloud.compute.v1.Snapshot)2 SnapshotsClient (com.google.cloud.compute.v1.SnapshotsClient)2 Vector (java.util.Vector)2 CustomerEncryptionKeyProtectedDisk (com.google.cloud.compute.v1.CustomerEncryptionKeyProtectedDisk)1