Search in sources :

Example 26 with InstancesClient

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

the class StopInstance method stopInstance.

// Stops a started Google Compute Engine instance.
public static void stopInstance(String project, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    /* 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()) {
        StopInstanceRequest stopInstanceRequest = StopInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
        OperationFuture<Operation, Operation> operation = instancesClient.stopAsync(stopInstanceRequest);
        Operation response = operation.get(3, TimeUnit.MINUTES);
        if (response.getStatus() == Status.DONE) {
            System.out.println("Instance stopped successfully ! ");
        }
    }
}
Also used : InstancesClient(com.google.cloud.compute.v1.InstancesClient) Operation(com.google.cloud.compute.v1.Operation) StopInstanceRequest(com.google.cloud.compute.v1.StopInstanceRequest)

Example 27 with InstancesClient

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

the class CreateInstancesAdvanced method createWithDisks.

// [END compute_instances_create_from_image_plus_snapshot_disk]
// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_with_subnet]
// [START compute_instances_create_from_image_plus_snapshot_disk]
// [START compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_empty_disk]
// [START compute_instances_create_from_custom_image]
// [START compute_instances_create_from_image]
/**
 * Send an instance creation request to the Compute Engine API and wait for it to complete.
 *
 * @param project project ID or project number of the Cloud project you want to use.
 * @param zone name of the zone to create the instance in. For example: "us-west3-b"
 * @param instanceName name of the new virtual machine (VM) instance.
 * @param disks a list of compute_v1.AttachedDisk objects describing the disks you want to attach
 * to your new instance.
 * @param machineType machine type of the VM being created. This value uses the following format:
 * "zones/{zone}/machineTypes/{type_name}".
 * For example: "zones/europe-west3-c/machineTypes/f1-micro"
 * @param network name of the network you want the new instance to use. For example:
 * "global/networks/default" represents the network named "default", which is created
 * automatically for each project.
 * @param subnetwork name of the subnetwork you want the new instance to use. This value uses the
 * following format: "regions/{region}/subnetworks/{subnetwork_name}"
 * @return Instance object.
 */
private static Instance createWithDisks(String project, String zone, String instanceName, Vector<AttachedDisk> disks, String machineType, String network, String subnetwork) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface;
        if (subnetwork != null) {
            networkInterface = NetworkInterface.newBuilder().setName(network).setSubnetwork(subnetwork).build();
        } else {
            networkInterface = NetworkInterface.newBuilder().setName(network).build();
        }
        machineType = String.format("zones/%s/machineTypes/%s", zone, machineType);
        // Bind `instanceName`, `machineType`, `disk`, and `networkInterface` to an instance.
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setMachineType(machineType).addAllDisks(disks).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 null;
        }
        System.out.println("Operation Status: " + response.getStatus());
        return instancesClient.get(project, zone, instanceName);
    }
}
Also used : InsertInstanceRequest(com.google.cloud.compute.v1.InsertInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Example 28 with InstancesClient

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

the class GetDeleteProtection method getDeleteProtection.

// Returns the state of delete protection flag of given instance.
public static boolean getDeleteProtection(String projectId, String zone, String instanceName) throws IOException {
    try (InstancesClient instancesClient = InstancesClient.create()) {
        Instance instance = instancesClient.get(projectId, zone, instanceName);
        boolean deleteProtection = instance.getDeletionProtection();
        System.out.printf("Retrieved Delete Protection setting for instance: %s : %s", instanceName, deleteProtection);
        return deleteProtection;
    }
}
Also used : Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient)

Example 29 with InstancesClient

use of com.google.cloud.compute.v1.InstancesClient 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)

Aggregations

InstancesClient (com.google.cloud.compute.v1.InstancesClient)28 Operation (com.google.cloud.compute.v1.Operation)20 Instance (com.google.cloud.compute.v1.Instance)18 InsertInstanceRequest (com.google.cloud.compute.v1.InsertInstanceRequest)11 AttachedDisk (com.google.cloud.compute.v1.AttachedDisk)9 NetworkInterface (com.google.cloud.compute.v1.NetworkInterface)6 GetInstanceRequest (com.google.cloud.compute.v1.GetInstanceRequest)2 AggregatedListInstancesRequest (com.google.cloud.compute.v1.AggregatedListInstancesRequest)1 CustomerEncryptionKeyProtectedDisk (com.google.cloud.compute.v1.CustomerEncryptionKeyProtectedDisk)1 DeleteInstanceRequest (com.google.cloud.compute.v1.DeleteInstanceRequest)1 Disk (com.google.cloud.compute.v1.Disk)1 DisksClient (com.google.cloud.compute.v1.DisksClient)1 Image (com.google.cloud.compute.v1.Image)1 ImagesClient (com.google.cloud.compute.v1.ImagesClient)1 InsertImageRequest (com.google.cloud.compute.v1.InsertImageRequest)1 InstanceTemplate (com.google.cloud.compute.v1.InstanceTemplate)1 InstanceTemplatesClient (com.google.cloud.compute.v1.InstanceTemplatesClient)1 AggregatedListPagedResponse (com.google.cloud.compute.v1.InstancesClient.AggregatedListPagedResponse)1 InstancesScopedList (com.google.cloud.compute.v1.InstancesScopedList)1 InstancesSettings (com.google.cloud.compute.v1.InstancesSettings)1