Search in sources :

Example 66 with Instance

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

the class SnippetsIT method testWaitForOperation.

@Test
public void testWaitForOperation() throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // Construct a delete request and get the operation instance.
    InstancesClient instancesClient = InstancesClient.create();
    OperationFuture<Operation, Operation> operation = instancesClient.deleteAsync(PROJECT_ID, ZONE, MACHINE_NAME_WAIT_FOR_OP);
    // Wait for the operation to complete.
    operation.get(3, TimeUnit.MINUTES);
    assertThat(stdOut.toString().contains("Operation Status: DONE"));
}
Also used : InstancesClient(com.google.cloud.compute.v1.InstancesClient) Operation(com.google.cloud.compute.v1.Operation) Test(org.junit.jupiter.api.Test)

Example 67 with Instance

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

the class CreateEncryptedInstance method createEncryptedInstance.

// Create a new encrypted instance with the provided "instanceName" value and encryption key
// in the specified project and zone.
public static void createEncryptedInstance(String project, String zone, String instanceName, String diskEncryptionKey) 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";
    /* Initialize client that will be used to send requests. This client only needs to be created
       once, and can be reused for multiple requests. After completing all of your requests, call
       the `instancesClient.close()` method on the client to safely
       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()).setInitializeParams(AttachedDiskInitializeParams.newBuilder().setSourceImage(sourceImage).setDiskSizeGb(diskSizeGb).build()).setDiskEncryptionKey(CustomerEncryptionKey.newBuilder().setRawKey(diskEncryptionKey).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 ", 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 68 with Instance

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

the class CreateInstanceFromTemplate method createInstanceFromTemplate.

// Create a new instance from template in the specified project and zone.
public static void createInstanceFromTemplate(String projectId, String zone, String instanceName, String instanceTemplateUrl) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (InstancesClient instancesClient = InstancesClient.create()) {
        InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(Instance.newBuilder().setName(instanceName).build()).setSourceInstanceTemplate(instanceTemplateUrl).build();
        Operation response = instancesClient.insertAsync(insertInstanceRequest).get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance creation from template failed ! ! " + response);
            return;
        }
        System.out.printf("Instance creation from template: Operation Status %s: %s ", instanceName, response.getStatus());
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) InstancesClient(com.google.cloud.compute.v1.InstancesClient) Operation(com.google.cloud.compute.v1.Operation)

Example 69 with Instance

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

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

the class ListInstance method listInstances.

// List all instances in the given zone in the specified project ID.
public static void listInstances(String project, String zone) throws IOException {
    // safely clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Set the project and zone to retrieve instances present in the zone.
        System.out.printf("Listing instances from %s in %s:", project, zone);
        for (Instance zoneInstance : instancesClient.list(project, zone).iterateAll()) {
            System.out.println(zoneInstance.getName());
        }
        System.out.println("####### Listing instances complete #######");
    }
}
Also used : Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient)

Aggregations

Test (org.junit.Test)41 Instance (com.google.cloud.redis.v1beta1.Instance)34 CloudRedisClient (com.google.cloud.redis.v1beta1.CloudRedisClient)30 InstancesClient (com.google.cloud.compute.v1.InstancesClient)29 Operation (com.google.cloud.compute.v1.Operation)29 Instance (com.google.cloud.compute.v1.Instance)25 ByteString (com.google.protobuf.ByteString)17 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)16 AbstractMessage (com.google.protobuf.AbstractMessage)13 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)11 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)9 Instance (com.google.cloud.notebooks.v1beta1.Instance)9 ArrayList (java.util.ArrayList)8 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)7 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)7 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)7 Any (com.google.protobuf.Any)7 FieldMask (com.google.protobuf.FieldMask)7