Search in sources :

Example 1 with PreserveResourceException

use of com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException in project cloudbreak by hortonworks.

the class AzureVolumeResourceBuilder method delete.

@Override
@Retryable(value = RuntimeException.class, backoff = @Backoff(delay = 1000, multiplier = 2, maxDelay = 10000), maxAttempts = 5)
public CloudResource delete(AzureContext context, AuthenticatedContext auth, CloudResource resource) throws PreserveResourceException {
    LOGGER.info("Delete the disks from the instances if they are not reattached. {}", resource);
    VolumeSetAttributes volumeSetAttributes = getVolumeSetAttributes(resource);
    List<CloudResourceStatus> cloudResourceStatuses = checkResources(ResourceType.AZURE_VOLUMESET, context, auth, List.of(resource));
    boolean anyDeleted = cloudResourceStatuses.stream().map(CloudResourceStatus::getStatus).anyMatch(ResourceStatus.DELETED::equals);
    preserveVolumeIfDoNotDeleteOnTermination(auth, resource, volumeSetAttributes, anyDeleted);
    LOGGER.info("Resource {} will be deleted.", resource.getName());
    AzureClient client = getAzureClient(auth);
    List<CloudResourceStatus> removableDisks = cloudResourceStatuses.stream().filter(cloudResourceStatus -> ResourceStatus.CREATED.equals(cloudResourceStatus.getStatus()) || (ResourceStatus.ATTACHED.equals(cloudResourceStatus.getStatus()) && volumeSetAttributes.getDeleteOnTermination())).collect(toList());
    deleteVolumes(client, removableDisks);
    return null;
}
Also used : AzureContext(com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext) VirtualMachine(com.microsoft.azure.management.compute.VirtualMachine) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) AvailabilityZoneId(com.microsoft.azure.management.resources.fluentcore.arm.AvailabilityZoneId) AsyncTaskExecutor(org.springframework.core.task.AsyncTaskExecutor) LoggerFactory(org.slf4j.LoggerFactory) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) PlatformParametersConsts(com.sequenceiq.cloudbreak.cloud.PlatformParametersConsts) DeviceNameGenerator(com.sequenceiq.cloudbreak.util.DeviceNameGenerator) Future(java.util.concurrent.Future) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) AzureInstanceView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureInstanceView) AzureUtils(com.sequenceiq.cloudbreak.cloud.azure.AzureUtils) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) AzureResourceGroupMetadataProvider(com.sequenceiq.cloudbreak.cloud.azure.AzureResourceGroupMetadataProvider) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) AzureDiskType(com.sequenceiq.cloudbreak.cloud.azure.AzureDiskType) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Collectors(java.util.stream.Collectors) Backoff(org.springframework.retry.annotation.Backoff) Objects(java.util.Objects) Disk(com.microsoft.azure.management.compute.Disk) 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) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) HashMap(java.util.HashMap) Image(com.sequenceiq.cloudbreak.cloud.model.Image) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) AzureResourceNameService(com.sequenceiq.cloudbreak.cloud.azure.service.AzureResourceNameService) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) PagedList(com.microsoft.azure.PagedList) Qualifier(org.springframework.beans.factory.annotation.Qualifier) StreamSupport(java.util.stream.StreamSupport) InstanceTemplate(com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate) Retryable(org.springframework.retry.annotation.Retryable) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Collectors.toList(java.util.stream.Collectors.toList) Component(org.springframework.stereotype.Component) PreserveResourceException(com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException) Collections(java.util.Collections) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) VolumeSetAttributes(com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes) Retryable(org.springframework.retry.annotation.Retryable)

Example 2 with PreserveResourceException

use of com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException 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)2 VolumeSetAttributes (com.sequenceiq.cloudbreak.cloud.model.VolumeSetAttributes)2 PreserveResourceException (com.sequenceiq.cloudbreak.cloud.template.compute.PreserveResourceException)2 PagedList (com.microsoft.azure.PagedList)1 Disk (com.microsoft.azure.management.compute.Disk)1 VirtualMachine (com.microsoft.azure.management.compute.VirtualMachine)1 AvailabilityZoneId (com.microsoft.azure.management.resources.fluentcore.arm.AvailabilityZoneId)1 PlatformParametersConsts (com.sequenceiq.cloudbreak.cloud.PlatformParametersConsts)1 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)1 AzureDiskType (com.sequenceiq.cloudbreak.cloud.azure.AzureDiskType)1 AzureResourceGroupMetadataProvider (com.sequenceiq.cloudbreak.cloud.azure.AzureResourceGroupMetadataProvider)1 AzureUtils (com.sequenceiq.cloudbreak.cloud.azure.AzureUtils)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 AzureContext (com.sequenceiq.cloudbreak.cloud.azure.context.AzureContext)1 AzureResourceNameService (com.sequenceiq.cloudbreak.cloud.azure.service.AzureResourceNameService)1 AzureInstanceView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureInstanceView)1 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1