Search in sources :

Example 31 with Instance

use of com.google.cloud.compute.v1.Instance 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 32 with Instance

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

the class WindowsOsImageIT method cleanUp.

@AfterAll
public static void cleanUp() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    final PrintStream out = System.out;
    ByteArrayOutputStream stdOut = new ByteArrayOutputStream();
    System.setOut(new PrintStream(stdOut));
    // Delete image.
    ImagesClient imagesClient = ImagesClient.create();
    Operation operation = imagesClient.deleteAsync(PROJECT_ID, IMAGE_NAME).get();
    if (operation.hasError()) {
        System.out.println("Image not deleted.");
    }
    // Delete instance.
    DeleteInstance.deleteInstance(PROJECT_ID, ZONE, INSTANCE_NAME);
    stdOut.close();
    System.setOut(out);
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient) AfterAll(org.junit.jupiter.api.AfterAll)

Example 33 with Instance

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

the class CreateInstancesAdvanced method createWithSubnetwork.

// [END compute_instances_create_from_image_plus_snapshot_disk]
// [START compute_instances_create_from_image]
/**
 * Create a new VM instance with Debian 10 operating system in specified network and subnetwork.
 *
 * @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 networkLink 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 subnetworkLink 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.
 */
public static Instance createWithSubnetwork(String project, String zone, String instanceName, String networkLink, String subnetworkLink) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create()) {
        // List of public operating system (OS) images: https://cloud.google.com/compute/docs/images/os-details
        Image image = imagesClient.getFromFamily("debian-cloud", "debian-10");
        String diskType = String.format("zones/%s/diskTypes/pd-standard", zone);
        Vector<AttachedDisk> disks = new Vector<>();
        disks.add(diskFromImage(diskType, 10, true, image.getSelfLink()));
        return createWithDisks(project, zone, instanceName, disks, "n1-standard-1", networkLink, subnetworkLink);
    }
}
Also used : AttachedDisk(com.google.cloud.compute.v1.AttachedDisk) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Vector(java.util.Vector)

Example 34 with Instance

use of com.google.cloud.compute.v1.Instance 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 35 with Instance

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

the class DeleteInstance method deleteInstance.

// Delete the instance specified by `instanceName`
// if it's present in the given project and zone.
public static void deleteInstance(String project, String zone, String instanceName) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        System.out.printf("Deleting instance: %s ", instanceName);
        // Describe which instance is to be deleted.
        DeleteInstanceRequest deleteInstanceRequest = DeleteInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
        OperationFuture<Operation, Operation> operation = instancesClient.deleteAsync(deleteInstanceRequest);
        // Wait for the operation to complete.
        Operation response = operation.get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Instance deletion failed ! ! " + response);
            return;
        }
        System.out.println("Operation Status: " + response.getStatus());
    }
}
Also used : InstancesClient(com.google.cloud.compute.v1.InstancesClient) Operation(com.google.cloud.compute.v1.Operation) DeleteInstanceRequest(com.google.cloud.compute.v1.DeleteInstanceRequest)

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