use of com.google.cloud.compute.v1.InstancesStartWithEncryptionKeyRequest 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 ! ");
}
}
}
Aggregations