Search in sources :

Example 1 with LifeCycleState

use of com.sequenceiq.cloudbreak.cloud.model.filesystem.efs.LifeCycleState in project cloudbreak by hortonworks.

the class AwsEfsResourceBuilder method delete.

@Override
public CloudResource delete(AwsContext context, AuthenticatedContext auth, CloudResource resource) throws InterruptedException {
    // 
    // https://docs.aws.amazon.com/efs/latest/ug/wt1-clean-up.html
    // To delete an EFS instance, need the following steps
    // 1. Terminate the EC2 instances that mount on this EFS. The caller of this function has to make sure it is done before calling this function
    // 2. Delete the mount targets of this EFS.
    // 3. (Optional) Delete the security group of each mount target. AWS does not charge for security groups
    // 4. (Optional) Delete the security group of the EC2 instances at step 1. The mount target's security group has a rule that references
    // the EC2 security group. Therefore, we cannot first delete the EC2 instance's security group.
    // 5. Actually delete the EFS
    // 
    AmazonEfsClient client = getAmazonEfsClient(auth);
    CloudEfsAttributes efsAttributes = resource.getParameter(CloudResource.ATTRIBUTES, CloudEfsAttributes.class);
    String efsId = efsAttributes.getFileSystemId();
    DescribeFileSystemsRequest request = new DescribeFileSystemsRequest().withFileSystemId(efsId);
    DescribeFileSystemsResult result = client.describeFileSystems(request);
    List<FileSystemDescription> efsDescriptions = result.getFileSystems();
    for (FileSystemDescription efsDescription : efsDescriptions) {
        LifeCycleState efsLifeCycleState = LifeCycleState.fromValue(efsDescription.getLifeCycleState());
        if (LifeCycleState.DELETED.equals(efsLifeCycleState) || LifeCycleState.DELETING.equals(efsLifeCycleState)) {
            LOGGER.debug("The given AWS EFS's [name: {}] lifecycle state was [{}] hence we are going to skip any delete operation over this resource", efsDescription.getName(), efsLifeCycleState);
            continue;
        }
        if (efsDescription.getNumberOfMountTargets() > 0) {
            DescribeMountTargetsRequest mtRequest = new DescribeMountTargetsRequest().withFileSystemId(efsId);
            DescribeMountTargetsResult mtResult = client.describeMountTargets(mtRequest);
            List<MountTargetDescription> mountTargetDescriptionList = mtResult.getMountTargets();
            // Only delete the mount targets.
            for (MountTargetDescription mtDescription : mountTargetDescriptionList) {
                DeleteMountTargetRequest mtDelRequest = new DeleteMountTargetRequest().withMountTargetId(mtDescription.getMountTargetId());
                LOGGER.debug("About to delete AWS EFS mount target that has the following id: {}", mtDescription.getMountTargetId());
                client.deleteMountTarget(mtDelRequest);
            }
        // TODO: delete the security groups in the future
        }
        DeleteFileSystemRequest efsDelRequest = new DeleteFileSystemRequest().withFileSystemId(efsId);
        client.deleteFileSystem(efsDelRequest);
    }
    return null;
}
Also used : AmazonEfsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient) CloudEfsAttributes(com.sequenceiq.cloudbreak.cloud.model.CloudEfsAttributes) DescribeMountTargetsResult(com.amazonaws.services.elasticfilesystem.model.DescribeMountTargetsResult) MountTargetDescription(com.amazonaws.services.elasticfilesystem.model.MountTargetDescription) DescribeMountTargetsRequest(com.amazonaws.services.elasticfilesystem.model.DescribeMountTargetsRequest) DescribeFileSystemsRequest(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsRequest) DeleteFileSystemRequest(com.amazonaws.services.elasticfilesystem.model.DeleteFileSystemRequest) DeleteMountTargetRequest(com.amazonaws.services.elasticfilesystem.model.DeleteMountTargetRequest) LifeCycleState(com.sequenceiq.cloudbreak.cloud.model.filesystem.efs.LifeCycleState) DescribeFileSystemsResult(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult) FileSystemDescription(com.amazonaws.services.elasticfilesystem.model.FileSystemDescription)

Aggregations

DeleteFileSystemRequest (com.amazonaws.services.elasticfilesystem.model.DeleteFileSystemRequest)1 DeleteMountTargetRequest (com.amazonaws.services.elasticfilesystem.model.DeleteMountTargetRequest)1 DescribeFileSystemsRequest (com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsRequest)1 DescribeFileSystemsResult (com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult)1 DescribeMountTargetsRequest (com.amazonaws.services.elasticfilesystem.model.DescribeMountTargetsRequest)1 DescribeMountTargetsResult (com.amazonaws.services.elasticfilesystem.model.DescribeMountTargetsResult)1 FileSystemDescription (com.amazonaws.services.elasticfilesystem.model.FileSystemDescription)1 MountTargetDescription (com.amazonaws.services.elasticfilesystem.model.MountTargetDescription)1 AmazonEfsClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient)1 CloudEfsAttributes (com.sequenceiq.cloudbreak.cloud.model.CloudEfsAttributes)1 LifeCycleState (com.sequenceiq.cloudbreak.cloud.model.filesystem.efs.LifeCycleState)1