Search in sources :

Example 91 with Instance

use of com.google.bigtable.admin.v2.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 {
    /* 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();
        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 92 with Instance

use of com.google.bigtable.admin.v2.Instance 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 {
    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();
        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 93 with Instance

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

the class StartEncryptedInstance method startEncryptedInstance.

// Starts a stopped Google Compute Engine instance (with encrypted disks).
public static void startEncryptedInstance(String project, String zone, String instanceName, String key) throws IOException, ExecutionException, InterruptedException {
    /* 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()) {
        GetInstanceRequest getInstanceRequest = GetInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
        Instance instance = instancesClient.get(getInstanceRequest);
        // Prepare the information about disk encryption.
        CustomerEncryptionKeyProtectedDisk protectedDisk = CustomerEncryptionKeyProtectedDisk.newBuilder().setDiskEncryptionKey(CustomerEncryptionKey.newBuilder().setRawKey(key).build()).setSource(instance.getDisks(0).getSource()).build();
        InstancesStartWithEncryptionKeyRequest startWithEncryptionKeyRequest = InstancesStartWithEncryptionKeyRequest.newBuilder().addDisks(protectedDisk).build();
        StartWithEncryptionKeyInstanceRequest encryptionKeyInstanceRequest = StartWithEncryptionKeyInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).setInstancesStartWithEncryptionKeyRequestResource(startWithEncryptionKeyRequest).build();
        OperationFuture<Operation, Operation> operation = instancesClient.startWithEncryptionKeyAsync(encryptionKeyInstanceRequest);
        Operation response = operation.get();
        if (response.getStatus() == Status.DONE) {
            System.out.println("Encrypted instance started successfully ! ");
        }
    }
}
Also used : GetInstanceRequest(com.google.cloud.compute.v1.GetInstanceRequest) Instance(com.google.cloud.compute.v1.Instance) StartWithEncryptionKeyInstanceRequest(com.google.cloud.compute.v1.StartWithEncryptionKeyInstanceRequest) InstancesClient(com.google.cloud.compute.v1.InstancesClient) InstancesStartWithEncryptionKeyRequest(com.google.cloud.compute.v1.InstancesStartWithEncryptionKeyRequest) Operation(com.google.cloud.compute.v1.Operation) CustomerEncryptionKeyProtectedDisk(com.google.cloud.compute.v1.CustomerEncryptionKeyProtectedDisk)

Example 94 with Instance

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

use of com.google.bigtable.admin.v2.Instance in project java-bigtable by googleapis.

the class UpdateInstanceRequestTest method testLabels.

@Test
public void testLabels() {
    UpdateInstanceRequest input = UpdateInstanceRequest.of("my-instance").setAllLabels(ImmutableMap.of("label1", "value1", "label2", "value2"));
    PartialUpdateInstanceRequest actual = input.toProto("my-project");
    PartialUpdateInstanceRequest expected = PartialUpdateInstanceRequest.newBuilder().setUpdateMask(FieldMask.newBuilder().addPaths("labels")).setInstance(Instance.newBuilder().setName("projects/my-project/instances/my-instance").putLabels("label1", "value1").putLabels("label2", "value2")).build();
    assertThat(actual).isEqualTo(expected);
}
Also used : PartialUpdateInstanceRequest(com.google.bigtable.admin.v2.PartialUpdateInstanceRequest) PartialUpdateInstanceRequest(com.google.bigtable.admin.v2.PartialUpdateInstanceRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)124 AbstractMessage (com.google.protobuf.AbstractMessage)62 ByteString (com.google.protobuf.ByteString)57 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)40 StatusRuntimeException (io.grpc.StatusRuntimeException)40 Operation (com.google.longrunning.Operation)22 InstanceName (com.google.bigtable.admin.v2.InstanceName)20 HashMap (java.util.HashMap)16 ExecutionException (java.util.concurrent.ExecutionException)16 Table (com.google.bigtable.admin.v2.Table)15 TableName (com.google.bigtable.admin.v2.TableName)15 ClusterName (com.google.bigtable.admin.v2.ClusterName)14 Cluster (com.google.bigtable.admin.v2.Cluster)13 ColumnFamily (com.google.bigtable.admin.v2.ColumnFamily)13 ArrayList (java.util.ArrayList)12 Instance (com.google.cloud.compute.v1.Instance)11 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 Instance (com.google.cloud.notebooks.v1beta1.Instance)9