com.google.api.services.compute.model.AttachedDisk Java Examples

The following examples show how to use com.google.api.services.compute.model.AttachedDisk. 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 6 votes vote down vote up
private Operation executeStartOperation(String projectId, String availabilityZone, Compute compute, String instanceId, InstanceTemplate template,
        List<AttachedDisk> disks) throws IOException {

    if (gcpDiskEncryptionService.hasCustomEncryptionRequested(template)) {
        CustomerEncryptionKey customerEncryptionKey = gcpDiskEncryptionService.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();
    }
}
 
Example #2
Source File: GcpInstanceResourceBuilderTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void doTestDefaultDiskEncryption(ImmutableMap<String, Object> params) throws Exception {
    Group group = newGroupWithParams(params);
    List<CloudResource> buildableResources = builder.create(context, privateId, authenticatedContext, group, image);
    context.addComputeResources(0L, buildableResources);

    when(compute.instances()).thenReturn(instances);
    ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class);
    when(instances.insert(anyString(), anyString(), instanceArgumentCaptor.capture())).thenReturn(insert);
    when(insert.execute()).thenReturn(operation);

    builder.build(context, privateId, authenticatedContext, group, buildableResources, cloudStack);

    verify(gcpDiskEncryptionService, times(0)).addEncryptionKeyToDisk(any(InstanceTemplate.class), any(AttachedDisk.class));

    instanceArgumentCaptor.getValue().getDisks().forEach(attachedDisk -> assertNull(attachedDisk.getDiskEncryptionKey()));
}
 
Example #3
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Collection<AttachedDisk> getBootDiskList(Iterable<CloudResource> resources, String projectId, AvailabilityZone zone) {
    Collection<AttachedDisk> listOfDisks = new ArrayList<>();
    for (CloudResource resource : filterResourcesByType(resources, ResourceType.GCP_DISK)) {
        listOfDisks.add(createDisk(projectId, true, resource.getName(), zone.value(), true));
    }
    return listOfDisks;
}
 
Example #4
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private Collection<AttachedDisk> getAttachedDisks(Iterable<CloudResource> resources, String projectId) {
    Collection<AttachedDisk> listOfDisks = new ArrayList<>();
    for (CloudResource resource : filterResourcesByType(resources, ResourceType.GCP_ATTACHED_DISKSET)) {
        VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
        for (Volume volume : volumeSetAttributes.getVolumes()) {
            listOfDisks.add(createDisk(projectId, false, volume.getId(), volumeSetAttributes.getAvailabilityZone(), Boolean.FALSE));
        }
    }
    return listOfDisks;
}
 
Example #5
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 #6
Source File: GcpInstanceResourceBuilderTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void doTestDiskEncryption(String encryptionKey, ImmutableMap<String, Object> templateParams) throws Exception {
    Group group = newGroupWithParams(templateParams);
    CloudResource requestedDisk = CloudResource.builder()
            .type(ResourceType.GCP_DISK)
            .status(CommonStatus.REQUESTED)
            .name("dasdisk")
            .build();
    List<CloudResource> buildableResources = List.of(requestedDisk);
    context.addComputeResources(0L, buildableResources);

    when(compute.instances()).thenReturn(instances);

    ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class);
    when(instances.insert(anyString(), anyString(), instanceArgumentCaptor.capture())).thenReturn(insert);
    when(insert.execute()).thenReturn(operation);

    CustomerEncryptionKey customerEncryptionKey = new CustomerEncryptionKey();
    customerEncryptionKey.setRawKey("encodedKey==");
    doAnswer(invocation -> {
        AttachedDisk argument = invocation.getArgument(1);
        argument.setDiskEncryptionKey(customerEncryptionKey);
        return invocation;
    }).when(gcpDiskEncryptionService).addEncryptionKeyToDisk(any(InstanceTemplate.class), any(AttachedDisk.class));

    builder.build(context, privateId, authenticatedContext, group, buildableResources, cloudStack);

    verify(gcpDiskEncryptionService, times(1)).addEncryptionKeyToDisk(any(InstanceTemplate.class), any(AttachedDisk.class));

    instanceArgumentCaptor.getValue().getDisks().forEach(attachedDisk -> {
        assertNotNull(attachedDisk.getDiskEncryptionKey());
        assertEquals(customerEncryptionKey, attachedDisk.getDiskEncryptionKey());
    });
}
 
Example #7
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 #8
Source File: ComputeEngineSample.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static Operation startInstance(Compute compute, String instanceName) throws IOException {
  System.out.println("================== Starting New Instance ==================");

  // Create VM Instance object with the required properties.
  Instance instance = new Instance();
  instance.setName(instanceName);
  instance.setMachineType(
      String.format(
          "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/e2-standard-1",
          PROJECT_ID, ZONE_NAME));
  // Add Network Interface to be used by VM Instance.
  NetworkInterface ifc = new NetworkInterface();
  ifc.setNetwork(
      String.format(
          "https://www.googleapis.com/compute/v1/projects/%s/global/networks/default",
          PROJECT_ID));
  List<AccessConfig> configs = new ArrayList<>();
  AccessConfig config = new AccessConfig();
  config.setType(NETWORK_INTERFACE_CONFIG);
  config.setName(NETWORK_ACCESS_CONFIG);
  configs.add(config);
  ifc.setAccessConfigs(configs);
  instance.setNetworkInterfaces(Collections.singletonList(ifc));

  // Add attached Persistent Disk to be used by VM Instance.
  AttachedDisk disk = new AttachedDisk();
  disk.setBoot(true);
  disk.setAutoDelete(true);
  disk.setType("PERSISTENT");
  AttachedDiskInitializeParams params = new AttachedDiskInitializeParams();
  // Assign the Persistent Disk the same name as the VM Instance.
  params.setDiskName(instanceName);
  // Specify the source operating system machine image to be used by the VM Instance.
  params.setSourceImage(SOURCE_IMAGE_PREFIX + SOURCE_IMAGE_PATH);
  // Specify the disk type as Standard Persistent Disk
  params.setDiskType(
      String.format(
          "https://www.googleapis.com/compute/v1/projects/%s/zones/%s/diskTypes/pd-standard",
          PROJECT_ID, ZONE_NAME));
  disk.setInitializeParams(params);
  instance.setDisks(Collections.singletonList(disk));

  // Initialize the service account to be used by the VM Instance and set the API access scopes.
  ServiceAccount account = new ServiceAccount();
  account.setEmail("default");
  List<String> scopes = new ArrayList<>();
  scopes.add("https://www.googleapis.com/auth/devstorage.full_control");
  scopes.add("https://www.googleapis.com/auth/compute");
  account.setScopes(scopes);
  instance.setServiceAccounts(Collections.singletonList(account));

  // Optional - Add a startup script to be used by the VM Instance.
  Metadata meta = new Metadata();
  Metadata.Items item = new Metadata.Items();
  item.setKey("startup-script-url");
  // If you put a script called "vm-startup.sh" in this Google Cloud Storage
  // bucket, it will execute on VM startup.  This assumes you've created a
  // bucket named the same as your PROJECT_ID.
  // For info on creating buckets see:
  // https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
  item.setValue(String.format("gs://%s/vm-startup.sh", PROJECT_ID));
  meta.setItems(Collections.singletonList(item));
  instance.setMetadata(meta);

  System.out.println(instance.toPrettyString());
  Compute.Instances.Insert insert = compute.instances().insert(PROJECT_ID, ZONE_NAME, instance);
  return insert.execute();
}
 
Example #9
Source File: GcpInstanceResourceBuilder.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@Override
public List<CloudResource> build(GcpContext context, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource,
        CloudStack cloudStack) throws Exception {
    InstanceTemplate template = group.getReferenceInstanceConfiguration().getTemplate();
    String projectId = context.getProjectId();
    Location location = context.getLocation();
    Compute compute = context.getCompute();

    List<CloudResource> computeResources = context.getComputeResources(privateId);
    List<AttachedDisk> listOfDisks = new ArrayList<>();

    listOfDisks.addAll(getBootDiskList(computeResources, projectId, location.getAvailabilityZone()));
    listOfDisks.addAll(getAttachedDisks(computeResources, projectId));

    listOfDisks.forEach(disk -> gcpDiskEncryptionService.addEncryptionKeyToDisk(template, disk));

    Instance instance = new Instance();
    instance.setMachineType(String.format("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/machineTypes/%s",
            projectId, location.getAvailabilityZone().value(), template.getFlavor()));
    instance.setName(buildableResource.get(0).getName());
    instance.setCanIpForward(Boolean.TRUE);
    instance.setNetworkInterfaces(getNetworkInterface(context, computeResources, group, cloudStack));
    instance.setDisks(listOfDisks);
    instance.setServiceAccounts(extractServiceAccounts(cloudStack));
    Scheduling scheduling = new Scheduling();
    boolean preemptible = false;
    if (template.getParameter(PREEMPTIBLE, Boolean.class) != null) {
        preemptible = template.getParameter(PREEMPTIBLE, Boolean.class);
    }
    scheduling.setPreemptible(preemptible);
    instance.setScheduling(scheduling);

    Tags tags = new Tags();
    List<String> tagList = new ArrayList<>();
    Map<String, String> labels = new HashMap<>();
    String groupname = group.getName().toLowerCase().replaceAll("[^A-Za-z0-9 ]", "");
    tagList.add(groupname);

    tagList.add(GcpStackUtil.getClusterTag(auth.getCloudContext()));
    tagList.add(GcpStackUtil.getGroupClusterTag(auth.getCloudContext(), group));
    cloudStack.getTags().forEach((key, value) -> tagList.add(key + '-' + value));

    labels.putAll(cloudStack.getTags());
    tags.setItems(tagList);

    instance.setTags(tags);
    instance.setLabels(labels);

    Metadata metadata = new Metadata();
    metadata.setItems(new ArrayList<>());

    Items sshMetaData = new Items();
    sshMetaData.setKey("ssh-keys");
    sshMetaData.setValue(group.getInstanceAuthentication().getLoginUserName() + ':' + group.getInstanceAuthentication().getPublicKey());

    Items blockProjectWideSsh = new Items();
    blockProjectWideSsh.setKey("block-project-ssh-keys");
    blockProjectWideSsh.setValue("TRUE");

    Items startupScript = new Items();
    startupScript.setKey("startup-script");
    startupScript.setValue(cloudStack.getImage().getUserDataByType(group.getType()));

    metadata.getItems().add(sshMetaData);
    metadata.getItems().add(startupScript);
    metadata.getItems().add(blockProjectWideSsh);
    instance.setMetadata(metadata);

    Insert insert = compute.instances().insert(projectId, location.getAvailabilityZone().value(), instance);
    insert.setPrettyPrint(Boolean.TRUE);
    try {
        Operation operation = insert.execute();
        verifyOperation(operation, buildableResource);
        updateDiskSetWithInstanceName(auth, computeResources, instance);
        return singletonList(createOperationAwareCloudResource(buildableResource.get(0), operation));
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
    }
}
 
Example #10
Source File: GcpDiskEncryptionService.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public void addEncryptionKeyToDisk(InstanceTemplate template, AttachedDisk disk) {
    if (hasCustomEncryptionRequested(template)) {
        CustomerEncryptionKey customerEncryptionKey = createCustomerEncryptionKey(template);
        disk.setDiskEncryptionKey(customerEncryptionKey);
    }
}
 
Example #11
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());
}