Search in sources :

Example 1 with CreateChangeSetRequest

use of com.amazonaws.services.cloudformation.model.CreateChangeSetRequest in project pipeline-aws-plugin by jenkinsci.

the class CloudformationStackTests method createStackWithStackChangeSetReviewInProgress.

@Test
public void createStackWithStackChangeSetReviewInProgress() throws ExecutionException {
    TaskListener taskListener = Mockito.mock(TaskListener.class);
    Mockito.when(taskListener.getLogger()).thenReturn(System.out);
    AmazonCloudFormation client = Mockito.mock(AmazonCloudFormation.class);
    Mockito.when(client.waiters()).thenReturn(new AmazonCloudFormationWaiters(client));
    Mockito.when(client.describeStacks(new DescribeStacksRequest().withStackName("foo"))).thenReturn(new DescribeStacksResult().withStacks(new Stack().withStackStatus("REVIEW_IN_PROGRESS")));
    CloudFormationStack stack = new CloudFormationStack(client, "foo", taskListener);
    stack.createChangeSet("c1", "templateBody", null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, ChangeSetType.UPDATE, "myarn", null);
    ArgumentCaptor<CreateChangeSetRequest> captor = ArgumentCaptor.forClass(CreateChangeSetRequest.class);
    Mockito.verify(client).createChangeSet(captor.capture());
    Assertions.assertThat(captor.getValue()).isEqualTo(new CreateChangeSetRequest().withChangeSetType(ChangeSetType.CREATE).withStackName("foo").withTemplateBody("templateBody").withCapabilities(Capability.values()).withParameters(Collections.emptyList()).withChangeSetName("c1").withRoleARN("myarn"));
    Mockito.verify(this.eventPrinter).waitAndPrintChangeSetEvents(Mockito.eq("foo"), Mockito.eq("c1"), Mockito.any(Waiter.class), Mockito.eq(PollConfiguration.DEFAULT));
}
Also used : AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CreateChangeSetRequest(com.amazonaws.services.cloudformation.model.CreateChangeSetRequest) TaskListener(hudson.model.TaskListener) AmazonCloudFormationWaiters(com.amazonaws.services.cloudformation.waiters.AmazonCloudFormationWaiters) Waiter(com.amazonaws.waiters.Waiter) DescribeStacksResult(com.amazonaws.services.cloudformation.model.DescribeStacksResult) Stack(com.amazonaws.services.cloudformation.model.Stack) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with CreateChangeSetRequest

use of com.amazonaws.services.cloudformation.model.CreateChangeSetRequest in project pipeline-aws-plugin by jenkinsci.

the class CloudformationStackTests method createNewStackChangeSet.

@Test
public void createNewStackChangeSet() throws ExecutionException {
    TaskListener taskListener = Mockito.mock(TaskListener.class);
    Mockito.when(taskListener.getLogger()).thenReturn(System.out);
    AmazonCloudFormation client = Mockito.mock(AmazonCloudFormation.class);
    Mockito.when(client.waiters()).thenReturn(new AmazonCloudFormationWaiters(client));
    Mockito.when(client.describeStacks(new DescribeStacksRequest().withStackName("foo"))).thenReturn(new DescribeStacksResult());
    CloudFormationStack stack = new CloudFormationStack(client, "foo", taskListener);
    stack.createChangeSet("c1", "templateBody", null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, ChangeSetType.CREATE, "myarn", null);
    ArgumentCaptor<CreateChangeSetRequest> captor = ArgumentCaptor.forClass(CreateChangeSetRequest.class);
    Mockito.verify(client).createChangeSet(captor.capture());
    Assertions.assertThat(captor.getValue()).isEqualTo(new CreateChangeSetRequest().withChangeSetType(ChangeSetType.CREATE).withStackName("foo").withTemplateBody("templateBody").withCapabilities(Capability.values()).withParameters(Collections.emptyList()).withChangeSetName("c1").withRoleARN("myarn"));
    Mockito.verify(this.eventPrinter).waitAndPrintChangeSetEvents(Mockito.eq("foo"), Mockito.eq("c1"), Mockito.any(Waiter.class), Mockito.eq(PollConfiguration.DEFAULT));
}
Also used : AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CreateChangeSetRequest(com.amazonaws.services.cloudformation.model.CreateChangeSetRequest) TaskListener(hudson.model.TaskListener) AmazonCloudFormationWaiters(com.amazonaws.services.cloudformation.waiters.AmazonCloudFormationWaiters) Waiter(com.amazonaws.waiters.Waiter) DescribeStacksResult(com.amazonaws.services.cloudformation.model.DescribeStacksResult) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with CreateChangeSetRequest

use of com.amazonaws.services.cloudformation.model.CreateChangeSetRequest in project pipeline-aws-plugin by jenkinsci.

the class CloudFormationStack method createChangeSet.

public void createChangeSet(String changeSetName, String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags, long pollIntervallMillis, ChangeSetType changeSetType, String roleArn) throws ExecutionException {
    try {
        CreateChangeSetRequest req = new CreateChangeSetRequest();
        req.withChangeSetName(changeSetName).withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM).withChangeSetType(changeSetType);
        if (ChangeSetType.CREATE.equals(changeSetType)) {
            this.listener.getLogger().format("Creating CloudFormation change set %s for new stack %s %n", changeSetName, this.stack);
            if ((templateBody == null || templateBody.isEmpty()) && (templateUrl == null || templateUrl.isEmpty())) {
                throw new IllegalArgumentException("Either a file or url for the template must be specified");
            }
            req.withTemplateBody(templateBody).withTemplateURL(templateUrl);
        } else if (ChangeSetType.UPDATE.equals(changeSetType)) {
            this.listener.getLogger().format("Creating CloudFormation change set %s for existing stack %s %n", changeSetName, this.stack);
            if (templateBody != null && !templateBody.isEmpty()) {
                req.setTemplateBody(templateBody);
            } else if (templateUrl != null && !templateUrl.isEmpty()) {
                req.setTemplateURL(templateUrl);
            } else {
                req.setUsePreviousTemplate(true);
            }
        } else {
            throw new IllegalArgumentException("Cannot create a CloudFormation change set without a valid change set type.");
        }
        req.withParameters(params).withTags(tags).withRoleARN(roleArn);
        this.client.createChangeSet(req);
        new EventPrinter(this.client, this.listener).waitAndPrintChangeSetEvents(this.stack, changeSetName, this.client.waiters().changeSetCreateComplete(), pollIntervallMillis);
        this.listener.getLogger().format("Created CloudFormation change set %s for stack %s %n", changeSetName, this.stack);
    } catch (ExecutionException e) {
        try {
            if (this.changeSetExists(changeSetName) && !this.changeSetHasChanges(changeSetName)) {
                // Ignore the failed creation of a change set with no changes.
                this.listener.getLogger().format("Created empty change set %s for stack %s %n", changeSetName, this.stack);
                return;
            }
        } catch (Throwable throwable) {
            e.addSuppressed(throwable);
        }
        this.listener.getLogger().format("Failed to create CloudFormation change set %s for stack %s %n", changeSetName, this.stack);
        throw e;
    }
}
Also used : CreateChangeSetRequest(com.amazonaws.services.cloudformation.model.CreateChangeSetRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with CreateChangeSetRequest

use of com.amazonaws.services.cloudformation.model.CreateChangeSetRequest in project pipeline-aws-plugin by jenkinsci.

the class CloudFormationStack method doCreateChangeSet.

private void doCreateChangeSet(String changeSetName, String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags, Collection<String> notificationARNs, PollConfiguration pollConfiguration, ChangeSetType changeSetType, String roleArn, RollbackConfiguration rollbackConfig) throws ExecutionException {
    try {
        CreateChangeSetRequest req = new CreateChangeSetRequest();
        req.withChangeSetName(changeSetName).withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM, Capability.CAPABILITY_AUTO_EXPAND).withChangeSetType(changeSetType);
        if (ChangeSetType.CREATE.equals(changeSetType)) {
            this.listener.getLogger().format("Creating CloudFormation change set %s for new stack %s %n", changeSetName, this.stack);
            if ((templateBody == null || templateBody.isEmpty()) && (templateUrl == null || templateUrl.isEmpty())) {
                throw new IllegalArgumentException("Either a file or url for the template must be specified");
            }
            req.withTemplateBody(templateBody).withTemplateURL(templateUrl);
        } else if (ChangeSetType.UPDATE.equals(changeSetType)) {
            this.listener.getLogger().format("Creating CloudFormation change set %s for existing stack %s %n", changeSetName, this.stack);
            if (templateBody != null && !templateBody.isEmpty()) {
                req.setTemplateBody(templateBody);
            } else if (templateUrl != null && !templateUrl.isEmpty()) {
                req.setTemplateURL(templateUrl);
            } else {
                req.setUsePreviousTemplate(true);
            }
        } else {
            throw new IllegalArgumentException("Cannot create a CloudFormation change set without a valid change set type.");
        }
        req.withParameters(params).withTags(tags).withNotificationARNs(notificationARNs).withRoleARN(roleArn).withRollbackConfiguration(rollbackConfig);
        this.client.createChangeSet(req);
        new EventPrinter(this.client, this.listener).waitAndPrintChangeSetEvents(this.stack, changeSetName, this.client.waiters().changeSetCreateComplete(), pollConfiguration);
        this.listener.getLogger().format("Created CloudFormation change set %s for stack %s %n", changeSetName, this.stack);
    } catch (ExecutionException e) {
        try {
            if (this.changeSetExists(changeSetName) && this.emptyChangeSet(changeSetName)) {
                // Ignore the failed creation of a change set with no changes.
                this.listener.getLogger().format("Created empty change set %s for stack %s %n", changeSetName, this.stack);
                return;
            }
        } catch (Throwable throwable) {
            e.addSuppressed(throwable);
        }
        this.listener.getLogger().format("Failed to create CloudFormation change set %s for stack %s %n", changeSetName, this.stack);
        throw e;
    }
}
Also used : CreateChangeSetRequest(com.amazonaws.services.cloudformation.model.CreateChangeSetRequest) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with CreateChangeSetRequest

use of com.amazonaws.services.cloudformation.model.CreateChangeSetRequest in project pipeline-aws-plugin by jenkinsci.

the class CloudformationStackTests method updateStackWithStackChangeSet.

@Test
public void updateStackWithStackChangeSet() throws ExecutionException {
    TaskListener taskListener = Mockito.mock(TaskListener.class);
    Mockito.when(taskListener.getLogger()).thenReturn(System.out);
    AmazonCloudFormation client = Mockito.mock(AmazonCloudFormation.class);
    Mockito.when(client.waiters()).thenReturn(new AmazonCloudFormationWaiters(client));
    Mockito.when(client.describeStacks(new DescribeStacksRequest().withStackName("foo"))).thenReturn(new DescribeStacksResult().withStacks(new Stack().withStackStatus("CREATE_COMPLETE")));
    CloudFormationStack stack = new CloudFormationStack(client, "foo", taskListener);
    stack.createChangeSet("c1", "templateBody", null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, ChangeSetType.UPDATE, "myarn", null);
    ArgumentCaptor<CreateChangeSetRequest> captor = ArgumentCaptor.forClass(CreateChangeSetRequest.class);
    Mockito.verify(client).createChangeSet(captor.capture());
    Assertions.assertThat(captor.getValue()).isEqualTo(new CreateChangeSetRequest().withChangeSetType(ChangeSetType.UPDATE).withStackName("foo").withTemplateBody("templateBody").withCapabilities(Capability.values()).withParameters(Collections.emptyList()).withChangeSetName("c1").withRoleARN("myarn"));
    Mockito.verify(this.eventPrinter).waitAndPrintChangeSetEvents(Mockito.eq("foo"), Mockito.eq("c1"), Mockito.any(Waiter.class), Mockito.eq(PollConfiguration.DEFAULT));
}
Also used : AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) CreateChangeSetRequest(com.amazonaws.services.cloudformation.model.CreateChangeSetRequest) TaskListener(hudson.model.TaskListener) AmazonCloudFormationWaiters(com.amazonaws.services.cloudformation.waiters.AmazonCloudFormationWaiters) Waiter(com.amazonaws.waiters.Waiter) DescribeStacksResult(com.amazonaws.services.cloudformation.model.DescribeStacksResult) Stack(com.amazonaws.services.cloudformation.model.Stack) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

CreateChangeSetRequest (com.amazonaws.services.cloudformation.model.CreateChangeSetRequest)5 AmazonCloudFormation (com.amazonaws.services.cloudformation.AmazonCloudFormation)3 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)3 DescribeStacksResult (com.amazonaws.services.cloudformation.model.DescribeStacksResult)3 AmazonCloudFormationWaiters (com.amazonaws.services.cloudformation.waiters.AmazonCloudFormationWaiters)3 Waiter (com.amazonaws.waiters.Waiter)3 TaskListener (hudson.model.TaskListener)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Stack (com.amazonaws.services.cloudformation.model.Stack)2 ExecutionException (java.util.concurrent.ExecutionException)2