Search in sources :

Example 1 with DELETED

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

the class AwsVolumeResourceBuilder method checkResources.

@Override
protected List<CloudResourceStatus> checkResources(ResourceType type, AwsContext context, AuthenticatedContext auth, Iterable<CloudResource> resources) {
    AmazonEc2Client client = getAmazonEC2Client(auth);
    Pair<List<String>, List<CloudResource>> volumes = volumeResourceCollector.getVolumeIdsByVolumeResources(resources, resourceType(), volumeSetAttributes());
    DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest(volumes.getFirst());
    LOGGER.debug("Going to describe volume(s) with id(s): [{}]", String.join(",", describeVolumesRequest.getVolumeIds()));
    AtomicReference<ResourceStatus> volumeSetStatus = new AtomicReference<>();
    try {
        DescribeVolumesResult result = client.describeVolumes(describeVolumesRequest);
        volumeSetStatus.set(getResourceStatus(result));
    } catch (AmazonEC2Exception e) {
        if (!"InvalidVolume.NotFound".equals(e.getErrorCode())) {
            throw e;
        }
        LOGGER.info("The volume doesn't need to be deleted as it does not exist on the provider side. Reason: {}", e.getMessage());
        volumeSetStatus.set(DELETED);
    }
    LOGGER.debug("[{}] volume set status is {}", String.join(",", volumes.getFirst()), volumeSetStatus);
    return volumes.getSecond().stream().map(resource -> new CloudResourceStatus(resource, volumeSetStatus.get())).collect(Collectors.toList());
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) CREATED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.CREATED) AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) LoggerFactory(org.slf4j.LoggerFactory) AwsDiskType(com.sequenceiq.common.model.AwsDiskType) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TagSpecification(com.amazonaws.services.ec2.model.TagSpecification) StringUtils(org.apache.commons.lang3.StringUtils) DeviceNameGenerator(com.sequenceiq.cloudbreak.util.DeviceNameGenerator) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) Future(java.util.concurrent.Future) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) FAILED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.FAILED) CreateVolumeRequest(com.amazonaws.services.ec2.model.CreateVolumeRequest) Map(java.util.Map) EbsInstanceBlockDeviceSpecification(com.amazonaws.services.ec2.model.EbsInstanceBlockDeviceSpecification) Pair(org.springframework.data.util.Pair) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CloudVolumeUsageType(com.sequenceiq.cloudbreak.cloud.model.CloudVolumeUsageType) IN_PROGRESS(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.IN_PROGRESS) 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) BinaryOperator(java.util.function.BinaryOperator) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Optional(java.util.Optional) AwsMethodExecutor(com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsMethodExecutor) NotNull(org.jetbrains.annotations.NotNull) AwsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsInstanceView) HashMap(java.util.HashMap) Image(com.sequenceiq.cloudbreak.cloud.model.Image) ResourceRetriever(com.sequenceiq.cloudbreak.cloud.service.ResourceRetriever) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InstanceBlockDeviceMappingSpecification(com.amazonaws.services.ec2.model.InstanceBlockDeviceMappingSpecification) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) CreateVolumeResult(com.amazonaws.services.ec2.model.CreateVolumeResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsContext(com.sequenceiq.cloudbreak.cloud.aws.common.context.AwsContext) Qualifier(org.springframework.beans.factory.annotation.Qualifier) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Subnet(com.amazonaws.services.ec2.model.Subnet) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) ATTACHED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.ATTACHED) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DELETED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.DELETED) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) ModifyInstanceAttributeResult(com.amazonaws.services.ec2.model.ModifyInstanceAttributeResult) Component(org.springframework.stereotype.Component) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException) ModifyInstanceAttributeRequest(com.amazonaws.services.ec2.model.ModifyInstanceAttributeRequest) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) CommonAwsClient(com.sequenceiq.cloudbreak.cloud.aws.common.CommonAwsClient) Collections(java.util.Collections) Volume(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes.Volume) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) List(java.util.List) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Example 2 with DELETED

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

the class MockResourceConnector method downscale.

@Override
public List<CloudResourceStatus> downscale(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> resources, List<CloudInstance> vms, Object resourcesToRemove) {
    mockUrlFactory.get(authenticatedContext, "/spi/terminate_instances").post(Entity.entity(vms, MediaType.APPLICATION_JSON_TYPE), String.class);
    List<String> instanceIdsToDownscale = new ArrayList<>();
    for (CloudInstance vm : vms) {
        instanceIdsToDownscale.add(vm.getInstanceId());
    }
    List<CloudResource> resourcesToDownscale = resources.stream().filter(resource -> instanceIdsToDownscale.contains(resource.getInstanceId())).collect(Collectors.toList());
    resourcesToDownscale.forEach(r -> resourceNotifier.notifyDeletion(r, authenticatedContext.getCloudContext()));
    return resourcesToDownscale.stream().map(r -> new CloudResourceStatus(r, DELETED)).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CREATED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.CREATED) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TlsInfo(com.sequenceiq.cloudbreak.cloud.model.TlsInfo) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) MediaType(javax.ws.rs.core.MediaType) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) Service(org.springframework.stereotype.Service) CloudVmInstanceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) CloudResourceHelper(com.sequenceiq.cloudbreak.cloud.transform.CloudResourceHelper) ResourceType(com.sequenceiq.common.api.type.ResourceType) ResourceConnector(com.sequenceiq.cloudbreak.cloud.ResourceConnector) Logger(org.slf4j.Logger) Collections.emptyList(java.util.Collections.emptyList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) AdjustmentTypeWithThreshold(com.sequenceiq.common.api.adjustment.AdjustmentTypeWithThreshold) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) DELETED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.DELETED) TemplatingNotSupportedException(com.sequenceiq.cloudbreak.cloud.exception.TemplatingNotSupportedException) UUID(java.util.UUID) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors(java.util.stream.Collectors) Entity(javax.ws.rs.client.Entity) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) InstanceStatus(com.sequenceiq.cloudbreak.cloud.model.InstanceStatus) Response(javax.ws.rs.core.Response) Group(com.sequenceiq.cloudbreak.cloud.model.Group) ResourcePersisted(com.sequenceiq.cloudbreak.cloud.notification.model.ResourcePersisted) Volume(com.sequenceiq.cloudbreak.cloud.model.Volume) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) ArrayList(java.util.ArrayList) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 3 with DELETED

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

the class AwsVolumeResourceBuilder method delete.

@Override
public CloudResource delete(AwsContext context, AuthenticatedContext auth, CloudResource resource) throws PreserveResourceException {
    LOGGER.debug("Set delete on termination to true, on instances");
    VolumeSetAttributes volumeSetAttributes = resource.getParameter(CloudResource.ATTRIBUTES, VolumeSetAttributes.class);
    List<CloudResourceStatus> cloudResourceStatuses = checkResources(ResourceType.AWS_VOLUMESET, context, auth, List.of(resource));
    boolean anyDeleted = cloudResourceStatuses.stream().map(CloudResourceStatus::getStatus).anyMatch(DELETED::equals);
    if (!volumeSetAttributes.getDeleteOnTermination() && !anyDeleted) {
        LOGGER.debug("Volumes will be preserved.");
        resource.setStatus(CommonStatus.DETACHED);
        volumeSetAttributes.setDeleteOnTermination(Boolean.TRUE);
        resource.putParameter(CloudResource.ATTRIBUTES, volumeSetAttributes);
        resourceNotifier.notifyUpdate(resource, auth.getCloudContext());
        throw new PreserveResourceException("Resource will be preserved for later reattachment.");
    }
    AmazonEc2Client client = getAmazonEC2Client(auth);
    deleteOrphanedVolumes(cloudResourceStatuses, client);
    turnOnDeleteOnterminationOnAttachedVolumes(resource, cloudResourceStatuses, client);
    return null;
}
Also used : CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) DELETED(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.DELETED) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException)

Aggregations

CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)3 DELETED (com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.DELETED)3 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)2 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)2 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)2 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 Builder (com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder)2 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)2 Group (com.sequenceiq.cloudbreak.cloud.model.Group)2 CREATED (com.sequenceiq.cloudbreak.cloud.model.ResourceStatus.CREATED)2 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)2 CommonStatus (com.sequenceiq.common.api.type.CommonStatus)2 ResourceType (com.sequenceiq.common.api.type.ResourceType)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2