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

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

There is a newer version: 3.0.0
Show newest version
package com.atlassian.maven.plugins.aws.it

import com.amazonaws.services.cloudformation.model.ListStackResourcesResult
import com.amazonaws.services.cloudformation.model.StackResourceSummary
import com.amazonaws.services.ec2.AmazonEC2ClientBuilder
import com.amazonaws.services.ec2.model.DescribeInstancesRequest
import com.amazonaws.services.ec2.model.Instance
import com.amazonaws.services.ec2.model.Reservation

data class StackInfo(
    val awsRegion: String,
    val stackName: String,
    val keyPairName: String,
    val resources: ListStackResourcesResult
) {
    fun listInstances(): List {
        return AmazonEC2ClientBuilder.standard()
            .withRegion(awsRegion)
            .build()
            .describeInstances(DescribeInstancesRequest().withInstanceIds(
                filterResources("AWS::EC2::Instance").map({ it.physicalResourceId })
            ))
            .reservations
            .flatMap(Reservation::getInstances)
    }

    private fun filterResources(
        resourceType: String
    ): List {
        return resources
            .stackResourceSummaries
            .filter({ it.resourceType == resourceType })
    }

    fun listBuckets(): List {
        return filterResources("AWS::S3::Bucket")
    }

    fun findBucket(
        logicalId: String
    ): StackResourceSummary {
        val bucket = listBuckets().find { it.logicalResourceId == logicalId }
        return bucket ?: throw RuntimeException("$this did not provision an S3 bucket with '$logicalId' logical id")
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy