use of com.google.cloud.compute.v1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class ResetInstance method resetInstance.
// Resets a running Google Compute Engine instance (with unencrypted disks).
public static void resetInstance(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()) {
ResetInstanceRequest resetInstanceRequest = ResetInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
OperationFuture<Operation, Operation> operation = instancesClient.resetAsync(resetInstanceRequest);
Operation response = operation.get(3, TimeUnit.MINUTES);
if (response.getStatus() == Status.DONE) {
System.out.println("Instance reset successfully ! ");
}
}
}
use of com.google.cloud.compute.v1.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, 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()) {
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(3, TimeUnit.MINUTES);
if (response.getStatus() == Status.DONE) {
System.out.println("Encrypted instance started successfully ! ");
}
}
}
use of com.google.cloud.compute.v1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class StartInstance method startInstance.
// Starts a stopped Google Compute Engine instance (with unencrypted disks).
public static void startInstance(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()) {
// Create the request.
StartInstanceRequest startInstanceRequest = StartInstanceRequest.newBuilder().setProject(project).setZone(zone).setInstance(instanceName).build();
OperationFuture<Operation, Operation> operation = instancesClient.startAsync(startInstanceRequest);
// Wait for the operation to complete.
Operation response = operation.get(3, TimeUnit.MINUTES);
if (response.getStatus() == Status.DONE) {
System.out.println("Instance started successfully ! ");
}
}
}
use of com.google.cloud.compute.v1.Instance 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 ! ");
}
}
}
use of com.google.cloud.compute.v1.Instance in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstancesAdvanced method createWithSnapshottedDataDisk.
// [END compute_instances_create_from_snapshot]
// [START compute_instances_create_from_image_plus_snapshot_disk]
/**
* Create a new VM instance with Debian 10 operating system and data disk created from snapshot.
*
* @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 snapshotName link to the snapshot you want to use as the source of your data disk in the
* form of: "projects/{project_name}/global/snapshots/{snapshot_name}"
* @return Instance object.
*/
public static Instance createWithSnapshottedDataDisk(String project, String zone, String instanceName, String snapshotName) 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()));
disks.add(diskFromSnapshot(diskType, 11, false, snapshotName));
return createWithDisks(project, zone, instanceName, disks, "n1-standard-1", "global/networks/default", null);
}
}
Aggregations