com.amazonaws.services.cloudformation.model.UpdateStackRequest Java Examples

The following examples show how to use com.amazonaws.services.cloudformation.model.UpdateStackRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AwsLaunchTemplateImageUpdateService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void updateImage(AuthenticatedContext authenticatedContext, CloudStack stack, CloudResource cfResource) {
    AwsCredentialView credentialView = new AwsCredentialView(authenticatedContext.getCloudCredential());
    String regionName = authenticatedContext.getCloudContext().getLocation().getRegion().getRegionName();
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, regionName);

    String imageName = stack.getImage().getImageName();
    String cfStackName = cfResource.getName();
    String cfTemplate = getCfTemplate(cfResource, cloudFormationClient);
    Json templateJson = new Json(cfTemplate);

    Map<String, String> encryptedImages = getEncryptedImagesMappedByAutoscalingGroupName(authenticatedContext, stack);
    stack.getGroups().forEach(group -> {
        String imageIdPath = String.format("Resources.%s.Properties.LaunchTemplateData.ImageId", AwsGroupView.getLaunchTemplateName(group.getName()));
        Object oldImageId = templateJson.getValue(imageIdPath);
        boolean encryptedImage = !"{\"Ref\":\"AMI\"}".equals(oldImageId.toString());
        if (encryptedImage) {
            replaceEncryptedImageInGroup(encryptedImages, imageName, templateJson, group, imageIdPath);
        }
    });

    String newCfTemplate = templateJson.getValue();
    UpdateStackRequest updateStackRequest = awsStackRequestHelper.createUpdateStackRequest(authenticatedContext, stack, cfStackName, newCfTemplate);
    cloudFormationClient.updateStack(updateStackRequest);
}
 
Example #2
Source File: AwsLaunchTemplateImageUpdateServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateImage() throws IOException {
    String cfStackName = "cf";
    CloudResource cfResource = CloudResource.builder()
            .type(ResourceType.CLOUDFORMATION_STACK)
            .name(cfStackName)
            .build();
    String template = Files.read(new File("src/test/resources/json/aws-cf-template.json"));
    String cfTemplateBody = JsonUtil.minify(String.format(template, "{\"Ref\":\"AMI\"}"));
    when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody));

    UpdateStackRequest updateStackRequest = new UpdateStackRequest();
    when(awsStackRequestHelper.createUpdateStackRequest(ac, stack, cfStackName, cfTemplateBody)).thenReturn(updateStackRequest);

    underTest.updateImage(ac, stack, cfResource);

    verify(awsStackRequestHelper).createUpdateStackRequest(ac, stack, cfStackName, cfTemplateBody);
    verify(cloudFormationClient).updateStack(updateStackRequest);
}
 
Example #3
Source File: AwsLaunchTemplateImageUpdateServiceTest.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUpdateEncryptedImage() throws IOException {
    String cfStackName = "cf";
    CloudResource cfResource = CloudResource.builder()
            .type(ResourceType.CLOUDFORMATION_STACK)
            .name(cfStackName)
            .build();
    String template = Files.read(new File("src/test/resources/json/aws-cf-template.json"));
    String cfTemplateBody = JsonUtil.minify(String.format(template, "\"ami-old\""));
    when(cloudFormationClient.getTemplate(any())).thenReturn(new GetTemplateResult().withTemplateBody(cfTemplateBody));

    UpdateStackRequest updateStackRequest = new UpdateStackRequest();
    ArgumentCaptor<String> cfTemplateBodyCaptor = ArgumentCaptor.forClass(String.class);
    when(awsStackRequestHelper.createUpdateStackRequest(eq(ac), eq(stack), eq(cfStackName), any())).thenReturn(updateStackRequest);

    underTest.updateImage(ac, stack, cfResource);

    verify(awsStackRequestHelper).createUpdateStackRequest(eq(ac), eq(stack), eq(cfStackName), cfTemplateBodyCaptor.capture());
    String expectedCfTemplate = JsonUtil.minify(String.format(template, "\"" + IMAGE_NAME + "\""));
    Assertions.assertThat(cfTemplateBodyCaptor.getValue()).isEqualTo(expectedCfTemplate);
    verify(cloudFormationClient).updateStack(updateStackRequest);
}
 
Example #4
Source File: CloudformationStackTests.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void updateStack() 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().withOutputs(new Output().withOutputKey("bar").withOutputValue("baz"))));

	CloudFormationStack stack = new CloudFormationStack(client, "foo", taskListener);

	RollbackConfiguration rollbackConfig = new RollbackConfiguration().withMonitoringTimeInMinutes(10);
	Map<String, String> outputs = stack.update("templateBody", null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, "myarn", rollbackConfig);

	ArgumentCaptor<UpdateStackRequest> captor = ArgumentCaptor.forClass(UpdateStackRequest.class);
	Mockito.verify(client).updateStack(captor.capture());
	Assertions.assertThat(captor.getValue()).isEqualTo(new UpdateStackRequest()
															   .withStackName("foo")
															   .withTemplateBody("templateBody")
															   .withCapabilities(Capability.values())
															   .withParameters(Collections.emptyList())
															   .withRoleARN("myarn")
															   .withRollbackConfiguration(rollbackConfig)
	);
	Mockito.verify(this.eventPrinter).waitAndPrintStackEvents(Mockito.eq("foo"), Mockito.any(Waiter.class), Mockito.eq(PollConfiguration.DEFAULT));
	Assertions.assertThat(outputs).containsEntry("bar", "baz").containsEntry("jenkinsStackUpdateStatus", "true");
}
 
Example #5
Source File: CloudformationStackTests.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void updateStackWithTemplateUrl() 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().withOutputs(new Output().withOutputKey("bar").withOutputValue("baz"))));

	CloudFormationStack stack = new CloudFormationStack(client, "foo", taskListener);

	RollbackConfiguration rollbackConfig = new RollbackConfiguration().withMonitoringTimeInMinutes(10);
	Map<String, String> outputs = stack.update(null, "bar", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, "myarn", rollbackConfig);

	ArgumentCaptor<UpdateStackRequest> captor = ArgumentCaptor.forClass(UpdateStackRequest.class);
	Mockito.verify(client).updateStack(captor.capture());
	Assertions.assertThat(captor.getValue()).isEqualTo(new UpdateStackRequest()
															   .withStackName("foo")
															   .withTemplateURL("bar")
															   .withCapabilities(Capability.values())
															   .withParameters(Collections.emptyList())
															   .withRoleARN("myarn")
															   .withRollbackConfiguration(rollbackConfig)
	);
	Mockito.verify(this.eventPrinter).waitAndPrintStackEvents(Mockito.eq("foo"), Mockito.any(Waiter.class), Mockito.eq(PollConfiguration.DEFAULT));
	Assertions.assertThat(outputs).containsEntry("bar", "baz").containsEntry("jenkinsStackUpdateStatus", "true");
}
 
Example #6
Source File: CloudformationStackTests.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void updateStackWithPreviousTemplate() 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().withOutputs(new Output().withOutputKey("bar").withOutputValue("baz"))));

	CloudFormationStack stack = new CloudFormationStack(client, "foo", taskListener);

	RollbackConfiguration rollbackConfig = new RollbackConfiguration().withMonitoringTimeInMinutes(10);
	Map<String, String> outputs = stack.update(null, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, "myarn", rollbackConfig);

	ArgumentCaptor<UpdateStackRequest> captor = ArgumentCaptor.forClass(UpdateStackRequest.class);
	Mockito.verify(client).updateStack(captor.capture());
	Assertions.assertThat(captor.getValue()).isEqualTo(new UpdateStackRequest()
															   .withStackName("foo")
															   .withUsePreviousTemplate(true)
															   .withCapabilities(Capability.values())
															   .withParameters(Collections.emptyList())
															   .withRoleARN("myarn")
															   .withRollbackConfiguration(rollbackConfig)
	);
	Mockito.verify(this.eventPrinter).waitAndPrintStackEvents(Mockito.eq("foo"), Mockito.any(Waiter.class), Mockito.eq(PollConfiguration.DEFAULT));
	Assertions.assertThat(outputs).containsEntry("bar", "baz").containsEntry("jenkinsStackUpdateStatus", "true");
}
 
Example #7
Source File: UpdateStackTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
public void execute() {
    checkParams();
    AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class);
    UpdateStackRequest request = new UpdateStackRequest()
            .withStackName(stackName).withStackPolicyBody(stackPolicyBody)
            .withStackPolicyURL(stackPolicyURL)
            .withTemplateBody(templateBody).withTemplateURL(templateURL)
            .withStackPolicyDuringUpdateBody(stackPolicyDuringUpdateBody)
            .withStackPolicyDuringUpdateURL(stackPolicyDuringUpdateURL)
            .withUsePreviousTemplate(usePreviousTemplate);

    if (capabilities.size() > 0) {
        request.setCapabilities(capabilities);
    }
    if (parameters.size() > 0) {
        request.setParameters(parameters);
    }
    if (notificationArns.size() > 0) {
        request.setNotificationARNs(notificationArns);
    }

    try {
        client.updateStack(request);
        System.out.println("Update stack " + stackName
                + " request submitted.");
    } catch (Exception e) {
        throw new BuildException("Could not update stack: "
                + e.getMessage(), e);
    }
}
 
Example #8
Source File: AwsStackRequestHelper.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
public UpdateStackRequest createUpdateStackRequest(AuthenticatedContext ac, CloudStack stack, String cFStackName, String cfTemplate) {
    return new UpdateStackRequest()
            .withStackName(cFStackName)
            .withParameters(getStackParameters(ac, stack, cFStackName, null))
            .withTemplateBody(cfTemplate);
}