Search in sources :

Example 31 with Instance

use of com.google.bigtable.admin.v2.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.bigtable.admin.v2.Instance in project java-docs-samples by GoogleCloudPlatform.

the class BulkWrite method getClusterNodeCount.

// Get the number of nodes for the Bigtable instance. This only works for single cluster instances
// so it will treat multi-cluster instances as single node clusters.
private static int getClusterNodeCount(String projectId, String instanceId) throws IOException, GeneralSecurityException {
    try {
        BigtableClusterUtilities clusterUtility = BigtableClusterUtilities.forInstance(projectId, instanceId);
        Cluster cluster = clusterUtility.getSingleCluster();
        String clusterId = new BigtableClusterName(cluster.getName()).getClusterId();
        String zoneId = BigtableClusterUtilities.getZoneId(cluster);
        int clusterNodeCount = clusterUtility.getClusterNodeCount(clusterId, zoneId);
        System.out.println("Cluster size " + clusterNodeCount);
        return clusterNodeCount;
    } catch (IllegalStateException e) {
        System.out.println("Unable to get cluster size. Treating as single-node cluster.");
        return 1;
    }
}
Also used : BigtableClusterName(com.google.cloud.bigtable.grpc.BigtableClusterName) Cluster(com.google.bigtable.admin.v2.Cluster) BigtableClusterUtilities(com.google.cloud.bigtable.grpc.BigtableClusterUtilities)

Example 33 with Instance

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

the class ListAllInstances method listAllInstances.

// List all instances in the specified project ID.
public static AggregatedListPagedResponse listAllInstances(String project) throws IOException {
    // safely clean up any remaining background resources.
    try (InstancesClient instancesClient = InstancesClient.create()) {
        // Use the `setMaxResults` parameter to limit the number of results
        // that the API returns per response page.
        AggregatedListInstancesRequest aggregatedListInstancesRequest = AggregatedListInstancesRequest.newBuilder().setProject(project).setMaxResults(5).build();
        InstancesClient.AggregatedListPagedResponse response = instancesClient.aggregatedList(aggregatedListInstancesRequest);
        // automatically, requesting next pages as you iterate over the results.
        for (Map.Entry<String, InstancesScopedList> zoneInstances : response.iterateAll()) {
            // Instances scoped by each zone
            String zone = zoneInstances.getKey();
            if (!zoneInstances.getValue().getInstancesList().isEmpty()) {
                // zoneInstances.getKey() returns the fully qualified address.
                // Hence, strip it to get the zone name only
                System.out.printf("Instances at %s: ", zone.substring(zone.lastIndexOf('/') + 1));
                for (Instance instance : zoneInstances.getValue().getInstancesList()) {
                    System.out.println(instance.getName());
                }
            }
        }
        System.out.println("####### Listing all instances complete #######");
        return response;
    }
}
Also used : InstancesScopedList(com.google.cloud.compute.v1.InstancesScopedList) Instance(com.google.cloud.compute.v1.Instance) AggregatedListPagedResponse(com.google.cloud.compute.v1.InstancesClient.AggregatedListPagedResponse) InstancesClient(com.google.cloud.compute.v1.InstancesClient) AggregatedListInstancesRequest(com.google.cloud.compute.v1.AggregatedListInstancesRequest) Map(java.util.Map)

Example 34 with Instance

use of com.google.bigtable.admin.v2.Instance 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 35 with Instance

use of com.google.bigtable.admin.v2.Instance 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

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