Search in sources :

Example 41 with VolumeSetAttributes

use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.

the class AzureVolumeResourceBuilderTest method buildTestWhenVolumeSetExistsAndRequestedAndNewDiskAndNoDiskEncryptionSetId.

@Test
void buildTestWhenVolumeSetExistsAndRequestedAndNewDiskAndNoDiskEncryptionSetId() throws Exception {
    initAsyncTaskExecutor();
    ArrayList<VolumeSetAttributes.Volume> volumes = new ArrayList<>();
    volumes.add(new VolumeSetAttributes.Volume(VOLUME_ID, DEVICE, VOLUME_SIZE, VOLUME_TYPE, CloudVolumeUsageType.GENERAL));
    CloudResource volumeSetResource = CloudResource.builder().type(ResourceType.AZURE_VOLUMESET).status(CommonStatus.REQUESTED).params(Map.of(CloudResource.ATTRIBUTES, new VolumeSetAttributes(AVAILABILITY_ZONE, true, FSTAB, volumes, VOLUME_SIZE, VOLUME_TYPE))).name(VOLUME_NAME).build();
    when(azureClient.createManagedDisk(VOLUME_ID, VOLUME_SIZE, AzureDiskType.STANDARD_SSD_LRS, REGION, RESOURCE_GROUP, Map.of(), null)).thenReturn(disk);
    List<CloudResource> result = underTest.build(context, cloudInstance, PRIVATE_ID, auth, group, List.of(volumeSetResource), cloudStack);
    assertThat(result).isNotNull();
    assertThat(result).hasSize(1);
    verifyVolumeSetResource(result.get(0), DISK_ID, DEVICE_DEV_SDC);
}
Also used : Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Test(org.junit.jupiter.api.Test)

Example 42 with VolumeSetAttributes

use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.

the class AzureVolumeResourceBuilderTest method buildTestWhenVolumeSetExistsAndCreated.

@Test
void buildTestWhenVolumeSetExistsAndCreated() throws Exception {
    ArrayList<VolumeSetAttributes.Volume> volumes = new ArrayList<>();
    volumes.add(new VolumeSetAttributes.Volume(VOLUME_ID, DEVICE, VOLUME_SIZE, VOLUME_TYPE, CloudVolumeUsageType.GENERAL));
    CloudResource volumeSetResource = CloudResource.builder().type(ResourceType.AZURE_VOLUMESET).status(CommonStatus.CREATED).params(Map.of(CloudResource.ATTRIBUTES, new VolumeSetAttributes(AVAILABILITY_ZONE, true, FSTAB, volumes, VOLUME_SIZE, VOLUME_TYPE))).name(VOLUME_NAME).build();
    List<CloudResource> result = underTest.build(context, cloudInstance, PRIVATE_ID, auth, group, List.of(volumeSetResource), cloudStack);
    verify(azureClient, never()).createManagedDisk(anyString(), anyInt(), any(AzureDiskType.class), anyString(), anyString(), anyMap(), anyString());
    assertThat(result).isNotNull();
    assertThat(result).hasSize(1);
    verifyVolumeSetResource(result.get(0), VOLUME_ID, DEVICE);
}
Also used : Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) ArrayList(java.util.ArrayList) AzureDiskType(com.sequenceiq.cloudbreak.cloud.azure.AzureDiskType) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Test(org.junit.jupiter.api.Test)

Example 43 with VolumeSetAttributes

use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.

the class GcpAttachedDiskResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
    if (!volumeSetAttributes.getDeleteOnTermination()) {
        resource.setStatus(CommonStatus.DETACHED);
        volumeSetAttributes.setDeleteOnTermination(Boolean.TRUE);
        resource.putParameter(CloudResource.ATTRIBUTES, volumeSetAttributes);
        resourceNotifier.notifyUpdate(resource, auth.getCloudContext());
        throw new InterruptedException("Resource will be preserved for later reattachment.");
    }
    List<String> operations = new ArrayList<>();
    List<String> syncedOperations = Collections.synchronizedList(operations);
    Collection<Future<Void>> futures = new ArrayList<>();
    for (VolumeSetAttributes.Volume volume : volumeSetAttributes.getVolumes()) {
        Future<Void> submit = intermediateBuilderExecutor.submit(() -> {
            try {
                LOGGER.info("Going to delete volume [id: {}] from project [id: {}] in the following availability zone: {}", volume.getId(), context.getProjectId(), volumeSetAttributes.getAvailabilityZone());
                Operation operation = context.getCompute().disks().delete(context.getProjectId(), volumeSetAttributes.getAvailabilityZone(), volume.getId()).execute();
                syncedOperations.add(operation.getName());
                if (operation.getHttpErrorStatusCode() != null) {
                    String message = String.format("%s [code: %d, message: %s, error: %s]", VOLUME_DELETION_FAILED_BASE_MSG, operation.getHttpErrorStatusCode(), operation.getHttpErrorMessage(), operation.getError());
                    LOGGER.warn(message);
                    throw new GcpResourceException(operation.getHttpErrorMessage(), resourceType(), volume.getId());
                }
            } catch (TokenResponseException e) {
                logVolumeDeletionProblem(e);
                gcpStackUtil.getMissingServiceAccountKeyError(e, context.getProjectId());
            } catch (GoogleJsonResponseException e) {
                logVolumeDeletionProblem(e);
                exceptionHandler(e, resource.getName(), resourceType());
            } catch (IOException e) {
                logVolumeDeletionProblem(e);
                throw new GcpResourceException(e.getMessage(), e);
            }
            return null;
        });
        futures.add(submit);
    }
    for (Future<Void> future : futures) {
        future.get();
    }
    resource.putParameter(OPERATION_ID, operations);
    return resource;
}
Also used : ArrayList(java.util.ArrayList) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Future(java.util.concurrent.Future) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Example 44 with VolumeSetAttributes

use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.

the class GcpDiskResourceBuilder method delete.

@Override
public CloudResource delete(GcpContext context, AuthenticatedContext auth, CloudResource resource) throws Exception {
    String resourceName = resource.getName();
    VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
    String zone;
    if (volumeSetAttributes != null && volumeSetAttributes.getAvailabilityZone() != null) {
        zone = volumeSetAttributes.getAvailabilityZone();
    } else {
        zone = context.getLocation().getAvailabilityZone().value();
    }
    try {
        LOGGER.info("Creating operation to delete disk [name: {}] in project [id: {}] in the following availability zone: {}", resourceName, context.getProjectId(), zone);
        Operation operation = context.getCompute().disks().delete(context.getProjectId(), zone, resourceName).execute();
        return createOperationAwareCloudResource(resource, operation);
    } catch (GoogleJsonResponseException e) {
        exceptionHandler(e, resourceName, resourceType());
    }
    return null;
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Operation(com.google.api.services.compute.model.Operation) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)

Example 45 with VolumeSetAttributes

use of com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes in project cloudbreak by hortonworks.

the class AwsAttachmentResourceBuilder method build.

@Override
public List<CloudResource> build(AwsContext context, CloudInstance cloudInstance, long privateId, AuthenticatedContext auth, Group group, List<CloudResource> buildableResource, CloudStack cloudStack) throws Exception {
    LOGGER.debug("Attach volumes to instance");
    CloudResource instance = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AWS_INSTANCE)).findFirst().orElseThrow(() -> new AwsResourceException("Instance resource not found"));
    Optional<CloudResource> volumeSetOpt = buildableResource.stream().filter(cloudResource -> cloudResource.getType().equals(ResourceType.AWS_VOLUMESET)).findFirst();
    if (volumeSetOpt.isEmpty()) {
        LOGGER.debug("No volumes to attach");
        return List.of();
    }
    CloudResource volumeSet = volumeSetOpt.get();
    AmazonEc2Client client = getAmazonEc2Client(auth);
    VolumeSetAttributes volumeSetAttributes = volumeSet.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
    LOGGER.debug("Creating attach volume requests and submitting to executor for stack '{}',   group '{}'", auth.getCloudContext().getName(), group.getName());
    List<Future<?>> futures = volumeSetAttributes.getVolumes().stream().filter(volume -> !StringUtils.equals(AwsDiskType.Ephemeral.value(), volume.getType())).map(volume -> new AttachVolumeRequest().withInstanceId(instance.getInstanceId()).withVolumeId(volume.getId()).withDevice(volume.getDevice())).map(request -> intermediateBuilderExecutor.submit(() -> client.attachVolume(request))).collect(Collectors.toList());
    LOGGER.debug("Waiting for attach volumes request");
    for (Future<?> future : futures) {
        try {
            future.get();
        } catch (AmazonClientException e) {
            LOGGER.error("Attach was unsuccesful. Group: {}, Volume: {} Instance: {}, AWS error: {}", group.getName(), instance.getName(), volumeSet.getName(), e.getMessage());
            throw e;
        }
    }
    LOGGER.debug("Attach volume requests sent");
    volumeSet.setInstanceId(instance.getInstanceId());
    volumeSet.setStatus(CommonStatus.CREATED);
    return List.of(volumeSet);
}
Also used : AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) LoggerFactory(org.slf4j.LoggerFactory) AwsDiskType(com.sequenceiq.common.model.AwsDiskType) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) Inject(javax.inject.Inject) Future(java.util.concurrent.Future) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsContext(com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Pair(org.springframework.data.util.Pair) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Collectors(java.util.stream.Collectors) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Component(org.springframework.stereotype.Component) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) AmazonClientException(com.amazonaws.AmazonClientException) AttachVolumeRequest(com.amazonaws.services.ec2.model.AttachVolumeRequest) AmazonClientException(com.amazonaws.AmazonClientException) Future(java.util.concurrent.Future) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)

Aggregations

VolumeSetAttributes (com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)49 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)27 ArrayList (java.util.ArrayList)26 Test (org.junit.jupiter.api.Test)18 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)16 List (java.util.List)16 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)14 ResourceType (com.sequenceiq.common.api.type.ResourceType)14 Group (com.sequenceiq.cloudbreak.cloud.model.Group)12 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)12 Volume (com.sequenceiq.cloudbreak.cloud.model.Volume)12 CommonStatus (com.sequenceiq.common.api.type.CommonStatus)12 Collectors (java.util.stream.Collectors)12 Optional (java.util.Optional)11 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)10 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)10 Resource (com.sequenceiq.cloudbreak.domain.Resource)10 InstanceMetaData (com.sequenceiq.cloudbreak.domain.stack.instance.InstanceMetaData)10 Inject (javax.inject.Inject)10 Logger (org.slf4j.Logger)10