Java Code Examples for com.google.api.services.compute.model.AttachedDisk#setSource()

The following examples show how to use com.google.api.services.compute.model.AttachedDisk#setSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private AttachedDisk createDisk(String projectId, boolean boot, String resourceName, String zone, boolean autoDelete) {
    AttachedDisk attachedDisk = new AttachedDisk();
    attachedDisk.setBoot(boot);
    attachedDisk.setAutoDelete(autoDelete);
    attachedDisk.setType(GCP_DISK_TYPE);
    attachedDisk.setMode(GCP_DISK_MODE);
    attachedDisk.setDeviceName(resourceName);
    attachedDisk.setSource(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/disks/%s",
            projectId, zone, resourceName));
    return attachedDisk;
}
 
Example 2
Source File: GcpInstanceResourceBuilderTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void doTestDefaultEncryption(CloudInstance cloudInstance) throws IOException {
    when(compute.instances()).thenReturn(instances);

    Get get = Mockito.mock(Get.class);
    when(instances.get(anyString(), anyString(), anyString())).thenReturn(get);
    Start start = Mockito.mock(Start.class);
    when(instances.start(anyString(), anyString(), anyString())).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);

    CloudVmInstanceStatus vmInstanceStatus = builder.start(context, authenticatedContext, cloudInstance);

    assertEquals(InstanceStatus.IN_PROGRESS, vmInstanceStatus.getStatus());

    verify(gcpDiskEncryptionService, times(0)).addEncryptionKeyToDisk(any(InstanceTemplate.class), any(Disk.class));
    verify(instances, times(0))
            .startWithEncryptionKey(anyString(), anyString(), anyString(), any(InstancesStartWithEncryptionKeyRequest.class));
}
 
Example 3
Source File: GcpInstanceResourceBuilderTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
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(gcpDiskEncryptionService.hasCustomEncryptionRequested(any(InstanceTemplate.class))).thenReturn(true);
    when(gcpDiskEncryptionService.createCustomerEncryptionKey(any(InstanceTemplate.class))).thenReturn(encryptionKey);

    CloudVmInstanceStatus vmInstanceStatus = builder.start(context, authenticatedContext, cloudInstance);

    assertEquals(InstanceStatus.IN_PROGRESS, vmInstanceStatus.getStatus());

    verify(gcpDiskEncryptionService, 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());
}