Search in sources :

Example 6 with Disk

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

the class SnippetsIT method deleteDisk.

private static void deleteDisk(Disk disk) throws IOException, InterruptedException, ExecutionException {
    try (DisksClient disksClient = DisksClient.create()) {
        OperationFuture<Operation, Operation> operation = disksClient.deleteAsync(PROJECT_ID, ZONE, disk.getName());
        operation.get();
    }
}
Also used : Operation(com.google.cloud.compute.v1.Operation) DisksClient(com.google.cloud.compute.v1.DisksClient)

Example 7 with Disk

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

the class WindowsOsImageIT method setup.

@BeforeAll
public static void setup() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    final PrintStream out = System.out;
    ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
    System.setOut(new PrintStream(stdOut));
    requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
    requireEnvVar("GOOGLE_CLOUD_PROJECT");
    // Cleanup existing test instances.
    Util.cleanUpExistingInstances("windowsimage-test-instance-", PROJECT_ID, ZONE);
    String randomUUID = UUID.randomUUID().toString().split("-")[0];
    INSTANCE_NAME = "windowsimage-test-instance-" + randomUUID;
    DISK_NAME = "windowsimage-test-disk-" + randomUUID;
    IMAGE_NAME = "windowsimage-test-image-" + randomUUID;
    // Create Instance with Windows source image.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        AttachedDisk attachedDisk = AttachedDisk.newBuilder().setDeviceName(DISK_NAME).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.name()).setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskName(DISK_NAME).setDiskSizeGb(64).setSourceImage("projects/windows-cloud/global/images/windows-server-2012-r2-dc-core-v20220314").build()).build();
        Instance instance = Instance.newBuilder().setName(INSTANCE_NAME).setMachineType(String.format("zones/%s/machineTypes/n1-standard-1", ZONE)).addNetworkInterfaces(NetworkInterface.newBuilder().setName("global/networks/default").build()).addDisks(attachedDisk).build();
        InsertInstanceRequest request = InsertInstanceRequest.newBuilder().setProject(PROJECT_ID).setZone(ZONE).setInstanceResource(instance).build();
        Operation response = instancesClient.insertAsync(request).get();
        Assert.assertFalse(response.hasError());
    }
    stdOut.close();
    System.setOut(out);
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) PrintStream(java.io.PrintStream) Instance(com.google.cloud.compute.v1.Instance) DeleteInstance(compute.DeleteInstance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Operation(com.google.cloud.compute.v1.Operation) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with Disk

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

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

the class CreateInstance method createInstance.

// Create a new instance with the provided "instanceName" value in the specified project and zone.
public static void createInstance(String project, String zone, String instanceName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // Below are sample values that can be replaced.
    // machineType: machine type of the VM being created.
    // *   This value uses the format zones/{zone}/machineTypes/{type_name}.
    // *   For a list of machine types, see https://cloud.google.com/compute/docs/machine-types
    // sourceImage: path to the operating system image to mount.
    // *   For details about images you can mount, see https://cloud.google.com/compute/docs/images
    // diskSizeGb: storage size of the boot disk to attach to the instance.
    // networkName: network interface to associate with the instance.
    String machineType = String.format("zones/%s/machineTypes/n1-standard-1", zone);
    String sourceImage = String.format("projects/debian-cloud/global/images/family/%s", "debian-11");
    long diskSizeGb = 10L;
    String networkName = "default";
    // clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Instance creation requires at least one persistent disk and one network interface.
        AttachedDisk disk = AttachedDisk.newBuilder().setBoot(true).setAutoDelete(true).setType(Type.PERSISTENT.toString()).setDeviceName("disk-1").setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).build();
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
        // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addDisks(disk).addNetworkInterfaces(networkInterface).build();
        System.out.printf("Creating instance: %s at %s %n", instanceName, zone);
        // Insert the instance in the specified project and zone.
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstanceResource(instanceResource).build();
        OperationFuture<Operation, Operation> operation = instancesClient.insertAsync(insertInstanceRequest);
        // Wait for the operation to complete.
        Operation response = operation.get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance creation failed ! ! " + response);
            return;
        }
        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 10 with Disk

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

the class CreateInstanceFromTemplateWithOverrides method createInstanceFromTemplateWithOverrides.

// Creates a Compute Engine VM instance from an instance template,
// but overrides the disk and machine type options in the template.
public static void createInstanceFromTemplateWithOverrides(String projectId, String zone, String instanceName, String instanceTemplateName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (InstancesClient instancesClient = InstancesClient.create();
        InstanceTemplatesClient instanceTemplatesClient = InstanceTemplatesClient.create()) {
        String machineType = "n1-standard-1";
        String newDiskSourceImage = "projects/debian-cloud/global/images/family/debian-10";
        // Retrieve an instance template.
        InstanceTemplate instanceTemplate = instanceTemplatesClient.get(projectId, instanceTemplateName);
        AttachedDisk newdisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(10).setSourceImage(newDiskSourceImage).build()).setAutoDelete(true).setBoot(false).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
        Instance instance = Instance.newBuilder().setName(instanceName).setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType)).addAllDisks(instanceTemplate.getProperties().getDisksList()).addDisks(newdisk).build();
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instance).setSourceInstanceTemplate(instanceTemplate.getSelfLink()).build();
        Operation response = instancesClient.insertAsync(insertInstanceRequest).get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance creation from template with overrides failed ! ! " + response);
            return;
        }
        System.out.printf("Instance creation from template with overrides: Operation Status %s: %s ", instanceName, 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) Operation(com.google.cloud.compute.v1.Operation) InstanceTemplatesClient(com.google.cloud.compute.v1.InstanceTemplatesClient) InstanceTemplate(com.google.cloud.compute.v1.InstanceTemplate)

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