use of com.google.cloud.compute.v1.InstancesClient in project java-docs-samples by GoogleCloudPlatform.
the class CreateInstanceFromTemplate method createInstanceFromTemplate.
// Create a new instance from template in the specified project and zone.
public static void createInstanceFromTemplate(String projectId, String zone, String instanceName, String instanceTemplateUrl) throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (InstancesClient instancesClient = InstancesClient.create()) {
InsertInstanceRequest insertInstanceRequest = InsertInstanceRequest.newBuilder().setProject(projectId).setZone(zone).setInstanceResource(Instance.newBuilder().setName(instanceName).build()).setSourceInstanceTemplate(instanceTemplateUrl).build();
Operation response = instancesClient.insertAsync(insertInstanceRequest).get(3, TimeUnit.MINUTES);
if (response.hasError()) {
System.out.println("Instance creation from template failed ! ! " + response);
return;
}
System.out.printf("Instance creation from template: Operation Status %s: %s ", instanceName, response.getStatus());
}
}
use of com.google.cloud.compute.v1.InstancesClient in project java-docs-samples by GoogleCloudPlatform.
the class ListInstance method listInstances.
// List all instances in the given zone in the specified project ID.
public static void listInstances(String project, String zone) throws IOException {
// safely clean up any remaining background resources.
try (InstancesClient instancesClient = InstancesClient.create()) {
// Set the project and zone to retrieve instances present in the zone.
System.out.printf("Listing instances from %s in %s:", project, zone);
for (Instance zoneInstance : instancesClient.list(project, zone).iterateAll()) {
System.out.println(zoneInstance.getName());
}
System.out.println("####### Listing instances complete #######");
}
}
use of com.google.cloud.compute.v1.InstancesClient 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.InstancesClient 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.InstancesClient 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 ! ");
}
}
}
Aggregations