use of com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilderTest method doTestCustomEncryption.
public void doTestCustomEncryption(Map<String, Object> params, CustomerEncryptionKey encryptionKey) throws IOException {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
CloudInstance cloudInstance = newCloudInstance(params, instanceAuthentication);
when(compute.instances()).thenReturn(instances);
ArgumentCaptor<InstancesStartWithEncryptionKeyRequest> requestCaptor = ArgumentCaptor.forClass(InstancesStartWithEncryptionKeyRequest.class);
Get get = Mockito.mock(Get.class);
when(instances.get(anyString(), anyString(), anyString())).thenReturn(get);
StartWithEncryptionKey start = Mockito.mock(StartWithEncryptionKey.class);
when(instances.startWithEncryptionKey(anyString(), anyString(), anyString(), requestCaptor.capture())).thenReturn(start);
String expectedSource = "google.disk";
AttachedDisk disk = new AttachedDisk();
disk.setSource(expectedSource);
Instance instance = new Instance();
instance.setDisks(List.of(disk));
instance.setStatus("TERMINATED");
when(get.execute()).thenReturn(instance);
when(start.setPrettyPrint(true)).thenReturn(start);
when(start.execute()).thenReturn(operation);
when(customGcpDiskEncryptionService.hasCustomEncryptionRequested(any(InstanceTemplate.class))).thenReturn(true);
when(customGcpDiskEncryptionCreatorService.createCustomerEncryptionKey(any(InstanceTemplate.class))).thenReturn(encryptionKey);
CloudVmInstanceStatus vmInstanceStatus = builder.start(context, authenticatedContext, cloudInstance);
assertEquals(InstanceStatus.IN_PROGRESS, vmInstanceStatus.getStatus());
verify(customGcpDiskEncryptionCreatorService, times(1)).createCustomerEncryptionKey(any(InstanceTemplate.class));
verify(instances, times(0)).start(anyString(), anyString(), anyString());
InstancesStartWithEncryptionKeyRequest keyRequest = requestCaptor.getValue();
assertNotNull(keyRequest.getDisks());
assertEquals(1, keyRequest.getDisks().size());
CustomerEncryptionKeyProtectedDisk protectedDisk = keyRequest.getDisks().iterator().next();
assertEquals(encryptionKey, protectedDisk.getDiskEncryptionKey());
assertEquals(expectedSource, protectedDisk.getSource());
}
use of com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk in project cloudbreak by hortonworks.
the class GcpInstanceResourceBuilder method executeStartOperation.
private Operation executeStartOperation(String projectId, String availabilityZone, Compute compute, String instanceId, InstanceTemplate template, List<AttachedDisk> disks) throws IOException {
if (customGcpDiskEncryptionService.hasCustomEncryptionRequested(template)) {
CustomerEncryptionKey customerEncryptionKey = customGcpDiskEncryptionCreatorService.createCustomerEncryptionKey(template);
List<CustomerEncryptionKeyProtectedDisk> protectedDisks = disks.stream().map(AttachedDisk::getSource).map(toCustomerEncryptionKeyProtectedDisk(customerEncryptionKey)).collect(Collectors.toList());
InstancesStartWithEncryptionKeyRequest request = new InstancesStartWithEncryptionKeyRequest();
request.setDisks(protectedDisks);
return compute.instances().startWithEncryptionKey(projectId, availabilityZone, instanceId, request).setPrettyPrint(true).execute();
} else {
return compute.instances().start(projectId, availabilityZone, instanceId).setPrettyPrint(true).execute();
}
}
Aggregations