Example usage for com.amazonaws.services.cloudformation AmazonCloudFormation executeChangeSet

List of usage examples for com.amazonaws.services.cloudformation AmazonCloudFormation executeChangeSet

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation AmazonCloudFormation executeChangeSet.

Prototype

ExecuteChangeSetResult executeChangeSet(ExecuteChangeSetRequest executeChangeSetRequest);

Source Link

Document

Updates a stack using the input information that was provided when the specified change set was created.

Usage

From source file:jp.classmethod.aws.gradle.cloudformation.AmazonCloudFormationExecuteChangeSetTask.java

License:Apache License

@TaskAction
public void executeChangeSet() throws InterruptedException, IOException {
    // to enable conventionMappings feature
    String stackName = getStackName();

    if (stackName == null) {
        throw new GradleException("stackName is not specified");
    }//from w  w w. j  a v  a 2s.  c  om

    AmazonCloudFormationPluginExtension ext = getProject().getExtensions()
            .getByType(AmazonCloudFormationPluginExtension.class);
    AmazonCloudFormation cfn = ext.getClient();

    try {
        DescribeStacksResult describeStackResult = cfn
                .describeStacks(new DescribeStacksRequest().withStackName(stackName));
        Stack stack = describeStackResult.getStacks().get(0);

        if (stableStatuses.contains(stack.getStackStatus())) {
            Optional<ChangeSetSummary> summary = getLatestChangeSetSummary(cfn);
            String changeSetName = summary
                    .orElseThrow(
                            () -> new GradleException("ChangeSet for stack " + stackName + " was not found."))
                    .getChangeSetName();
            ExecuteChangeSetRequest req = new ExecuteChangeSetRequest().withStackName(stackName)
                    .withChangeSetName(changeSetName);
            cfn.executeChangeSet(req);
            getLogger().info("ChangeSet is executed : {}, {}", stackName, changeSetName);
        } else {
            throw new GradleException("invalid status for update: " + stack.getStackStatus());
        }
    } catch (AmazonServiceException e) {
        if (e.getMessage().contains("does not exist")) {
            getLogger().warn("stack {} not found", stackName);
        } else if (e.getMessage().contains("No updates are to be performed.")) {
            getLogger().trace(e.getMessage());
        } else {
            throw e;
        }
    }

}