Skip to content

Commit

Permalink
Add flag for logging stack outputs (#10)
Browse files Browse the repository at this point in the history
* Add flag for logging stack outputs

* Change output logical name to match output
  • Loading branch information
fieldju authored Apr 24, 2017
1 parent ed3d757 commit c38dc02
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.fieldju
artifactId=gradle-aws-sam-deployer-plugin
version=1.5.2
version=1.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class DeploySamTaskIntegrationTest {
Foo: 'bar'
]
forceUploads = true
logStackOutputs = true
}

project.evaluate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class MultiRegionPackageAndDeploySamTaskIntegrationTest {
task.parameterOverrides = [
Foo: 'bar'
]
task.logStackOutputs = true

MultiRegionPackageAndDeploySamTask testTask = project.tasks.getByName('testTask') as MultiRegionPackageAndDeploySamTask
testTask.taskAction()
Expand Down
6 changes: 5 additions & 1 deletion src/integration-test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ Resources:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"
Resource: "*"
Outputs:
LambdaRoleArn:
Description: Role Arn
Value: !GetAtt LambdaRole.Arn
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fieldju.gradle.plugins.lambdasam

import org.gradle.api.GradleException

class AwsSamDeployerExtension {

String region
Expand All @@ -13,6 +11,7 @@ class AwsSamDeployerExtension {
Map<String, String> tokenArtifactMap = [:]
Map<String, String> parameterOverrides = [:]
boolean forceUploads = false
boolean logStackOutputs = false


@Override
Expand All @@ -27,6 +26,7 @@ class AwsSamDeployerExtension {
", tokenArtifactMap=" + tokenArtifactMap +
", parameterOverrides=" + parameterOverrides +
", forceUploads=" + forceUploads +
", logStackOutputs=" + logStackOutputs +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AwsSamDeployerPlugin implements Plugin<Project> {
deploySamTask.region = extension.getRegion()
deploySamTask.stackName = extension.getStackName()
deploySamTask.parameterOverrides = extension.parameterOverrides
deploySamTask.logStackOutputs = extension.logStackOutputs

PackageSamTask packageSamTask = project.tasks.getByName(TaskDefinitions.PACKAGE_SAM_TASK.name) as PackageSamTask
packageSamTask.region = extension.getRegion()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class PackageAndDeployTaskHelper {
List<String> regions,
String stackName,
Map<String, Map<String, String>> regionToParameterOverridesMap,
boolean executeChangeSet) {
boolean executeChangeSet,
boolean logStackOutputs) {

regions.each { String region ->
Map<String, String> parameterOverrides
Expand Down Expand Up @@ -52,6 +53,9 @@ class PackageAndDeployTaskHelper {

logger.lifecycle("Creating and executing changeset for ${templatePath} with stackname ${stackName} in region ${region} with overrides ${parameterOverrides}")
deployer.deployStack(stackName, calculatedTemplatePath, parameterOverrides, executeChangeSet)
if (logStackOutputs) {
deployer.logOutputs(stackName)
}
}
}

Expand Down Expand Up @@ -121,7 +125,8 @@ class PackageAndDeployTaskHelper {
String stackName,
String templatePath,
Map<String, String> parameterOverrides,
boolean executeChangeSet) {
boolean executeChangeSet,
boolean logStackOutputs) {

if (StringUtils.isBlank(stackName)) {
throw new GradleException("stackName cannot be blank")
Expand All @@ -135,5 +140,8 @@ class PackageAndDeployTaskHelper {
)

deployer.deployStack(stackName, templatePath, parameterOverrides, executeChangeSet)
if (logStackOutputs) {
deployer.logOutputs(stackName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.amazonaws.services.cloudformation.model.DescribeChangeSetResult
import com.amazonaws.services.cloudformation.model.DescribeStacksRequest
import com.amazonaws.services.cloudformation.model.ExecuteChangeSetRequest
import com.amazonaws.services.cloudformation.model.ExecuteChangeSetResult
import com.amazonaws.services.cloudformation.model.Output
import com.amazonaws.services.cloudformation.model.Parameter
import com.amazonaws.services.cloudformation.model.TemplateParameter
import com.amazonaws.services.cloudformation.model.ValidateTemplateRequest
Expand Down Expand Up @@ -295,4 +296,25 @@ class CloudFormationDeployer {
class ChangeSetMetadata {
String name, type
}

private List<Output> getStackOutputs(String stackName) {
def res = amazonCloudFormation.describeStacks(new DescribeStacksRequest().withStackName(stackName))
if (res.stacks.size() == 1) {
def stack = res.stacks.get(0)
return stack.outputs
} else {
logger.warn("Failed to describe stack: ${stackName}, AWS returned ${res.stacks.size()} stacks with that name.")
return []
}
}

public void logOutputs(String stackName) {
def outputs = getStackOutputs(stackName)
logger.lifecycle("The stack: ${stackName} has the following stack outputs")
outputs.each { output ->
logger.lifecycle("key: ${output.outputKey}")
logger.lifecycle("value: ${output.outputValue}")
logger.lifecycle("description: ${output.description}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class DeploySamTask extends SamTask {
@Input
boolean executeChangeSet = true

@Input
boolean logStackOutputs = false

DeploySamTask() {
group = TASK_GROUP
}
Expand All @@ -35,6 +38,6 @@ class DeploySamTask extends SamTask {
def calculatedTemplatePath = templatePath ? templatePath : "${project.buildDir.absolutePath}${File.separator}sam${File.separator}sam-deploy-${region}.yaml"

PackageAndDeployTaskHelper helper = new PackageAndDeployTaskHelper(logger)
helper.deployProcessedTemplate(region, stackName, calculatedTemplatePath, parameterOverrides, executeChangeSet)
helper.deployProcessedTemplate(region, stackName, calculatedTemplatePath, parameterOverrides, executeChangeSet, logStackOutputs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class MultiRegionDeploySamTask extends DefaultTask {
@Input
boolean executeChangeSet = true

@Input
boolean logStackOutputs = false

@TaskAction
void taskAction() {
// Groovy Strings and GStrings don't work together in collections because hashCode() produces different hashes
Expand All @@ -39,6 +42,6 @@ class MultiRegionDeploySamTask extends DefaultTask {
PackageAndDeployTaskHelper helper = new PackageAndDeployTaskHelper(logger)
//noinspection GroovyAssignabilityCheck
helper.multiRegionDeploy(templatePath, regionTemplatePathMap, regions,
stackName, regionToParameterOverridesMap, executeChangeSet)
stackName, regionToParameterOverridesMap, executeChangeSet, logStackOutputs)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class MultiRegionPackageAndDeploySamTask extends DefaultTask {
@Input
boolean executeChangeSet = true

@Input
boolean logStackOutputs = false

@TaskAction
void taskAction() {
// Groovy Strings and GStrings don't work together in collections because hashCode() produces different hashes
Expand Down Expand Up @@ -77,7 +80,7 @@ class MultiRegionPackageAndDeploySamTask extends DefaultTask {
calculatedParameterOverrides = regionToParameterOverridesMap."${region}"
}

helper.deployProcessedTemplate(region, stackName, processedTemplatePath, calculatedParameterOverrides, executeChangeSet)
helper.deployProcessedTemplate(region, stackName, processedTemplatePath, calculatedParameterOverrides, executeChangeSet, logStackOutputs)
}
}

Expand Down

0 comments on commit c38dc02

Please sign in to comment.