com.atlassian.maven.plugins.aws.it.StackInfo.kt Maven / Gradle / Ivy
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")
}
}