Search in sources :

Example 1 with InstancesStartWithEncryptionKeyRequest

use of com.google.api.services.compute.model.InstancesStartWithEncryptionKeyRequest 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());
}
Also used : InstanceAuthentication(com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication) StartWithEncryptionKey(com.google.api.services.compute.Compute.Instances.StartWithEncryptionKey) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Instance(com.google.api.services.compute.model.Instance) Get(com.google.api.services.compute.Compute.Instances.Get) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) InstancesStartWithEncryptionKeyRequest(com.google.api.services.compute.model.InstancesStartWithEncryptionKeyRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CustomerEncryptionKeyProtectedDisk(com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)

Example 2 with InstancesStartWithEncryptionKeyRequest

use of com.google.api.services.compute.model.InstancesStartWithEncryptionKeyRequest 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();
    }
}
Also used : AttachedDisk(com.google.api.services.compute.model.AttachedDisk) InstancesStartWithEncryptionKeyRequest(com.google.api.services.compute.model.InstancesStartWithEncryptionKeyRequest) CustomerEncryptionKeyProtectedDisk(com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk) CustomerEncryptionKey(com.google.api.services.compute.model.CustomerEncryptionKey)

Aggregations

AttachedDisk (com.google.api.services.compute.model.AttachedDisk)2 CustomerEncryptionKeyProtectedDisk (com.google.api.services.compute.model.CustomerEncryptionKeyProtectedDisk)2 InstancesStartWithEncryptionKeyRequest (com.google.api.services.compute.model.InstancesStartWithEncryptionKeyRequest)2 Get (com.google.api.services.compute.Compute.Instances.Get)1 StartWithEncryptionKey (com.google.api.services.compute.Compute.Instances.StartWithEncryptionKey)1 CustomerEncryptionKey (com.google.api.services.compute.model.CustomerEncryptionKey)1 Instance (com.google.api.services.compute.model.Instance)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)1 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)1 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1