Search in sources :

Example 36 with Instance

use of com.google.bigtable.admin.v2.Instance in project java-docs-samples by GoogleCloudPlatform.

the class CreateWindowsServerInstanceExternalIp method createWindowsServerInstanceExternalIp.

// Creates a new Windows Server instance that has an external IP address.
public static void createWindowsServerInstanceExternalIp(String projectId, String zone, String instanceName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // machineType - Machine type you want to create in following format:
    // *    "zones/{zone}/machineTypes/{type_name}". For example:
    // *    "zones/europe-west3-c/machineTypes/f1-micro"
    // *    You can find the list of available machine types using:
    // *    https://cloud.google.com/sdk/gcloud/reference/compute/machine-types/list
    String machineType = "n1-standard-1";
    // sourceImageFamily - Name of the public image family for Windows Server or SQL Server images.
    // *    https://cloud.google.com/compute/docs/images#os-compute-support
    String sourceImageFamily = "windows-2012-r2";
    // Instantiates a client.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        AttachedDisk attachedDisk = AttachedDisk.newBuilder().setInitializeParams(AttachedDiskInitializeParams.newBuilder().setDiskSizeGb(64).setSourceImage(String.format("projects/windows-cloud/global/images/family/%s", sourceImageFamily)).build()).setAutoDelete(true).setBoot(true).setType(AttachedDisk.Type.PERSISTENT.toString()).build();
        Instance instance = Instance.newBuilder().setName(instanceName).setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType)).addDisks(attachedDisk).addNetworkInterfaces(NetworkInterface.newBuilder().addAccessConfigs(AccessConfig.newBuilder().setType("ONE_TO_ONE_NAT").setName("External NAT").build()).setName("global/networks/default").build()).build();
        InsertInstanceRequest request = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instance).build();
        // Wait for the operation to complete.
        Operation operation = instancesClient.insertAsync(request).get(3, TimeUnit.MINUTES);
        if (operation.hasError()) {
            System.out.printf("Error in creating instance %s", operation.getError());
            return;
        }
        System.out.printf("Instance created %s", instanceName);
    }
}
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)

Example 37 with Instance

use of com.google.bigtable.admin.v2.Instance in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceWithCustomHostname method createInstanceWithCustomHostname.

// Creates an instance with custom hostname.
public static void createInstanceWithCustomHostname(String projectId, String zone, String instanceName, String hostName) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // machineType - Machine type for the VM instance specified in the following format:
    // *    "zones/{zone}/machineTypes/{type_name}". For example:
    // *    "zones/europe-west3-c/machineTypes/f1-micro"
    // *    You can find the list of available machine types by using this gcloud command:
    // *    $ gcloud compute machine-types list
    // sourceImage - Path of the disk image you want to use for your boot
    // *    disk. This image can be one of the public images
    // *    eg: "projects/...
    // *    or a private image you have access to.
    // *    You can check the list of available public images using:
    // *    $ gcloud compute images list
    // networkName - Name of the network you want the new instance to use.
    // *    For example: global/networks/default - if you want to use the default network.
    String machineType = "n1-standard-1";
    String sourceImage = String.format("projects/%s/global/images/family/%s", "debian-cloud", "debian-11");
    String networkName = "global/networks/default";
    try (InstancesClient instancesClient = InstancesClient.create()) {
        System.out.printf("Creating the %s instance in %s with hostname %s...", instanceName, zone, hostName);
        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(10).build()).build();
        // Use the network interface provided in the networkName argument.
        NetworkInterface networkInterface = NetworkInterface.newBuilder().setName(networkName).build();
        Instance instanceResource = Instance.newBuilder().setName(instanceName).setHostname(hostName).addDisks(disk).setMachineType(String.format("zones/%s/machineTypes/%s", zone, machineType)).addNetworkInterfaces(networkInterface).build();
        InsertInstanceRequest request = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(instanceResource).build();
        // Wait for the create operation to complete.
        Operation response = instancesClient.insertAsync(request).get(3, TimeUnit.MINUTES);
        ;
        if (response.hasError()) {
            System.out.printf("Instance creation failed for instance: %s ; Response: %s ! ! ", instanceName, response);
            return;
        }
        System.out.printf("Instance created : %s", instanceName);
        System.out.printf("Operation Status for instance %s is %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) NetworkInterface(com.google.cloud.compute.v1.NetworkInterface) Operation(com.google.cloud.compute.v1.Operation)

Example 38 with Instance

use of com.google.bigtable.admin.v2.Instance in project java-docs-samples by GoogleCloudPlatform.

the class CreateInstanceDeleteProtection method createInstanceDeleteProtection.

// Send an instance creation request to the Compute Engine API and wait for it to complete.
public static void createInstanceDeleteProtection(String projectId, String zone, String instanceName, boolean deleteProtection) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    String machineType = String.format("zones/%s/machineTypes/e2-small", zone);
    String sourceImage = String.format("projects/debian-cloud/global/images/family/%s", "debian-11");
    long diskSizeGb = 10L;
    String networkName = "default";
    // Instance creation requires at least one persistent disk and one network interface.
    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).setDeletionProtection(deleteProtection).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", 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 39 with Instance

use of com.google.bigtable.admin.v2.Instance in project java-docs-samples by GoogleCloudPlatform.

the class IsPreemptible method isPreemptible.

// Check if a given instance is preemptible or not.
public static void isPreemptible(String projectId, String zone, String instanceName) throws IOException {
    try (InstancesClient instancesClient = InstancesClient.create()) {
        Instance instance = instancesClient.get(projectId, zone, instanceName);
        boolean isPreemptible = instance.getScheduling().getPreemptible();
        System.out.printf("Preemptible status: %s", isPreemptible);
    }
}
Also used : Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient)

Example 40 with Instance

use of com.google.bigtable.admin.v2.Instance in project java-docs-samples by GoogleCloudPlatform.

the class CreateWindowsOsImage method createWindowsOsImage.

// Creates a new Windows image from the specified source disk.
public static void createWindowsOsImage(String project, String zone, String sourceDiskName, String imageName, String storageLocation, boolean forceCreate) throws IOException, ExecutionException, InterruptedException, TimeoutException {
    try (ImagesClient imagesClient = ImagesClient.create();
        InstancesClient instancesClient = InstancesClient.create();
        DisksClient disksClient = DisksClient.create()) {
        Disk disk = disksClient.get(project, zone, sourceDiskName);
        // Getting instances where source disk is attached.
        for (String fullInstanceName : disk.getUsersList()) {
            Map<String, String> instanceInfo = parseInstanceName(fullInstanceName);
            Instance instance = instancesClient.get(instanceInfo.get("instanceProjectId"), instanceInfo.get("instanceZone"), instanceInfo.get("instanceName"));
            // Сhecking whether the instances is stopped.
            if (!Arrays.asList("TERMINATED", "STOPPED").contains(instance.getStatus()) && !forceCreate) {
                throw new IllegalStateException(String.format("Instance %s should be stopped. Please stop the instance using GCESysprep command or set forceCreate parameter to true (not recommended). More information here: https://cloud.google.com/compute/docs/instances/windows/creating-windows-os-image#api.", instanceInfo.get("instanceName")));
            }
        }
        if (forceCreate) {
            System.out.println("Warning: forceCreate option compromise the integrity of your image. " + "Stop the instance before you create the image if possible.");
        }
        // Create Image.
        Image image = Image.newBuilder().setName(imageName).setSourceDisk(String.format("/zones/%s/disks/%s", zone, sourceDiskName)).addStorageLocations(storageLocation.isEmpty() ? "" : storageLocation).build();
        InsertImageRequest insertImageRequest = InsertImageRequest.newBuilder().setProject(project).setForceCreate(forceCreate).setImageResource(image).build();
        Operation response = imagesClient.insertAsync(insertImageRequest).get(3, TimeUnit.MINUTES);
        if (response.hasError()) {
            System.out.println("Windows OS Image creation failed ! ! " + response);
            return;
        }
        System.out.println("Image created.");
    }
}
Also used : Instance(com.google.cloud.compute.v1.Instance) InstancesClient(com.google.cloud.compute.v1.InstancesClient) InsertImageRequest(com.google.cloud.compute.v1.InsertImageRequest) Operation(com.google.cloud.compute.v1.Operation) ImagesClient(com.google.cloud.compute.v1.ImagesClient) Image(com.google.cloud.compute.v1.Image) Disk(com.google.cloud.compute.v1.Disk) DisksClient(com.google.cloud.compute.v1.DisksClient)

Aggregations

Test (org.junit.Test)137 AbstractMessage (com.google.protobuf.AbstractMessage)63 ByteString (com.google.protobuf.ByteString)57 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)41 StatusRuntimeException (io.grpc.StatusRuntimeException)41 Instance (com.google.cloud.redis.v1beta1.Instance)34 CloudRedisClient (com.google.cloud.redis.v1beta1.CloudRedisClient)30 Instance (com.google.cloud.compute.v1.Instance)25 Operation (com.google.longrunning.Operation)22 InstanceName (com.google.bigtable.admin.v2.InstanceName)20 InstancesClient (com.google.cloud.compute.v1.InstancesClient)19 ExecutionException (java.util.concurrent.ExecutionException)17 ClusterName (com.google.bigtable.admin.v2.ClusterName)16 Table (com.google.bigtable.admin.v2.Table)16 HashMap (java.util.HashMap)16 TableName (com.google.bigtable.admin.v2.TableName)15 Cluster (com.google.bigtable.admin.v2.Cluster)13 ColumnFamily (com.google.bigtable.admin.v2.ColumnFamily)13 Operation (com.google.cloud.compute.v1.Operation)13 ArrayList (java.util.ArrayList)13