All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.atlassian.maven.plugins.aws.it.Cleaner.kt Maven / Gradle / Ivy

The newest version!
package com.atlassian.maven.plugins.aws.it

import com.amazonaws.services.cloudformation.AmazonCloudFormationClientBuilder
import com.amazonaws.services.cloudformation.model.DeleteStackRequest
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder
import com.amazonaws.services.ec2.model.DeleteKeyPairRequest
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import org.apache.maven.plugin.logging.Log

class Cleaner(
    private val log: Log
) {
    fun clean(
        stackInfo: StackInfo
    ) {
        cleanBuckets(stackInfo)
        cleanKeys(stackInfo)
        cleanStack(stackInfo)
    }

    private fun cleanBuckets(
        stackInfo: StackInfo
    ) {
        val s3 = AmazonS3ClientBuilder.standard()
            .withRegion(stackInfo.awsRegion)
            .build()
        stackInfo.listBuckets().forEach {
            cleanBucket(it.physicalResourceId, s3)
        }
    }

    private fun cleanBucket(
        bucketName: String,
        s3: AmazonS3
    ) {
        s3
            .listObjects(bucketName)
            .objectSummaries
            .map { it.key }
            .forEach {
                log.info("Deleting $it from $bucketName")
                s3.deleteObject(bucketName, it)
            }
    }

    private fun cleanKeys(
        stackInfo: StackInfo
    ) {
        log.info("Deleting key pair ${stackInfo.keyPairName}")
        AmazonEC2ClientBuilder.standard()
            .withRegion(stackInfo.awsRegion)
            .build()
            .deleteKeyPair(DeleteKeyPairRequest().withKeyName(stackInfo.keyPairName))
    }

    private fun cleanStack(
        stackInfo: StackInfo
    ) {
        log.info("Deleting stack ${stackInfo.stackName}")
        AmazonCloudFormationClientBuilder.standard()
            .withRegion(stackInfo.awsRegion)
            .build()
            .deleteStack(DeleteStackRequest().withStackName(stackInfo.stackName))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy