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

The following examples show how to use com.amazonaws.services.cloudformation.model.OnFailure. 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: CloudFormationStack.java    From pipeline-aws-plugin with Apache License 2.0 6 votes vote down vote up
public Map<String, String> create(String templateBody, String templateUrl, Collection<Parameter> params, Collection<Tag> tags, Collection<String> notificationARNs, PollConfiguration pollConfiguration, String roleArn, String onFailure, Boolean enableTerminationProtection) throws ExecutionException {
	if ((templateBody == null || templateBody.isEmpty()) && (templateUrl == null || templateUrl.isEmpty())) {
		throw new IllegalArgumentException("Either a file or url for the template must be specified");
	}

	CreateStackRequest req = new CreateStackRequest();
	req.withStackName(this.stack).withCapabilities(Capability.CAPABILITY_IAM, Capability.CAPABILITY_NAMED_IAM, Capability.CAPABILITY_AUTO_EXPAND).withEnableTerminationProtection(enableTerminationProtection);
	req.withTemplateBody(templateBody).withTemplateURL(templateUrl).withParameters(params).withTags(tags).withNotificationARNs(notificationARNs)
			.withTimeoutInMinutes(pollConfiguration.getTimeout() == null ? null : (int) pollConfiguration.getTimeout().toMinutes())
			.withRoleARN(roleArn)
			.withOnFailure(OnFailure.valueOf(onFailure));
	this.client.createStack(req);

	new EventPrinter(this.client, this.listener).waitAndPrintStackEvents(this.stack, this.client.waiters().stackCreateComplete(), pollConfiguration);

	Map<String, String> outputs = this.describeOutputs();
	outputs.put(UPDATE_STATUS_OUTPUT, "true");
	return outputs;
}
 
Example #2
Source File: CloudformationStackTests.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void createStack() 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);

	Map<String, String> outputs = stack.create("templateBody", null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, "myarn", OnFailure.DO_NOTHING.toString(), null);

	ArgumentCaptor<CreateStackRequest> captor = ArgumentCaptor.forClass(CreateStackRequest.class);
	Mockito.verify(client).createStack(captor.capture());
	Assertions.assertThat(captor.getValue()).isEqualTo(new CreateStackRequest()
															   .withStackName("foo")
															   .withTemplateBody("templateBody")
															   .withCapabilities(Capability.values())
															   .withParameters(Collections.emptyList())
															   .withTimeoutInMinutes((int) PollConfiguration.DEFAULT.getTimeout().toMinutes())
															   .withOnFailure(OnFailure.DO_NOTHING)
															   .withRoleARN("myarn")
	);
	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 #3
Source File: CloudformationStackTests.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void createStackWithTemplateUrl() 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);

	PollConfiguration pollConfiguration = PollConfiguration.builder()
			.timeout(Duration.ofMinutes(3))
			.pollInterval(Duration.ofSeconds(17))
			.build();
	Map<String, String> outputs = stack.create(null, "bar", Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), pollConfiguration, "myarn", OnFailure.DO_NOTHING.toString(), true);

	ArgumentCaptor<CreateStackRequest> captor = ArgumentCaptor.forClass(CreateStackRequest.class);
	Mockito.verify(client).createStack(captor.capture());
	Assertions.assertThat(captor.getValue()).isEqualTo(new CreateStackRequest()
															   .withStackName("foo")
															   .withEnableTerminationProtection(true)
															   .withTemplateURL("bar")
															   .withCapabilities(Capability.values())
															   .withParameters(Collections.emptyList())
															   .withTimeoutInMinutes(3)
															   .withOnFailure(OnFailure.DO_NOTHING)
															   .withRoleARN("myarn")
	);
	Mockito.verify(this.eventPrinter).waitAndPrintStackEvents(Mockito.eq("foo"), Mockito.any(Waiter.class), Mockito.eq(pollConfiguration));
	Assertions.assertThat(outputs).containsEntry("bar", "baz").containsEntry("jenkinsStackUpdateStatus", "true");
}
 
Example #4
Source File: CloudformationStackTests.java    From pipeline-aws-plugin with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void createStackWithNoTemplate() throws ExecutionException {
	AmazonCloudFormation client = Mockito.mock(AmazonCloudFormation.class);
	try {
		TaskListener taskListener = Mockito.mock(TaskListener.class);
		Mockito.when(client.waiters()).thenReturn(new AmazonCloudFormationWaiters(client));

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

		stack.create(null, null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), PollConfiguration.DEFAULT, "myarn", OnFailure.ROLLBACK.toString(), null);
	} finally {
		Mockito.verifyZeroInteractions(client);
	}
}
 
Example #5
Source File: TestStackEnvironment.java    From spring-cloud-aws with Apache License 2.0 5 votes vote down vote up
private DescribeStackResourcesResult getStackResources(String stackName)
		throws InterruptedException, IOException {
	try {
		DescribeStacksResult describeStacksResult = this.amazonCloudFormationClient
				.describeStacks(new DescribeStacksRequest().withStackName(stackName));
		for (Stack stack : describeStacksResult.getStacks()) {
			if (isAvailable(stack)) {
				return this.amazonCloudFormationClient
						.describeStackResources(new DescribeStackResourcesRequest()
								.withStackName(stack.getStackName()));
			}
			if (isError(stack)) {
				if (this.stackCreatedByThisInstance) {
					throw new IllegalArgumentException("Could not create stack");
				}
				this.amazonCloudFormationClient.deleteStack(
						new DeleteStackRequest().withStackName(stack.getStackName()));
				return getStackResources(stackName);
			}
			if (isInProgress(stack)) {
				// noinspection BusyWait
				Thread.sleep(5000L);
				return getStackResources(stackName);
			}
		}
	}
	catch (AmazonClientException e) {
		String templateBody = FileCopyUtils.copyToString(new InputStreamReader(
				new ClassPathResource(TEMPLATE_PATH).getInputStream()));
		this.amazonCloudFormationClient.createStack(new CreateStackRequest()
				.withTemplateBody(templateBody).withOnFailure(OnFailure.DELETE)
				.withStackName(stackName)
				.withTags(new Tag().withKey("tag1").withValue("value1"))
				.withParameters(new Parameter().withParameterKey("RdsPassword")
						.withParameterValue(this.rdsPassword)));
		this.stackCreatedByThisInstance = true;
	}

	return getStackResources(stackName);
}
 
Example #6
Source File: CreateStackTask.java    From aws-ant-tasks with Apache License 2.0 5 votes vote down vote up
/**
 * Set an action to execute upon failure. If this element is set,
 * disableRollback should not be set. If disableRollback is set, this should
 * not be set. Will print out a warning if your argument is not supported by
 * our model, but will still try to execute. Not required.
 * 
 * @param onFailure
 *            An action to execute on failure.
 */
public void setOnFailure(String onFailure) {
    try {
        OnFailure.fromValue(onFailure);
    } catch (IllegalArgumentException e) {
        System.out
                .println("The OnFailure "
                        + onFailure
                        + " does not seem to be in our model. If this build fails, this may be why.");
    } finally {
        this.onFailure = onFailure;
    }
}
 
Example #7
Source File: AwsStackRequestHelper.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public CreateStackRequest createCreateStackRequest(AuthenticatedContext ac, CloudStack stack, String cFStackName, String subnet, String cfTemplate) {
    return new CreateStackRequest()
            .withStackName(cFStackName)
            .withOnFailure(OnFailure.DO_NOTHING)
            .withTemplateBody(cfTemplate)
            .withTags(awsTaggingService.prepareCloudformationTags(ac, stack.getTags()))
            .withCapabilities(CAPABILITY_IAM)
            .withParameters(getStackParameters(ac, stack, cFStackName, subnet));
}
 
Example #8
Source File: AwsStackRequestHelper.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public CreateStackRequest createCreateStackRequest(AuthenticatedContext ac, DatabaseStack stack, String cFStackName, String cfTemplate) {
    return new CreateStackRequest()
            .withStackName(cFStackName)
            .withOnFailure(OnFailure.DO_NOTHING)
            .withTemplateBody(cfTemplate)
            .withTags(awsTaggingService.prepareCloudformationTags(ac, stack.getTags()))
            .withCapabilities(CAPABILITY_IAM)
            .withParameters(getStackParameters(ac, stack));
}
 
Example #9
Source File: AwsNetworkConnector.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private CreateStackRequest createStackRequest(String stackName, String cloudFormationTemplate, Map<String, String> tags, String creatorUser) {
    Collection<Tag> awsTags = awsTaggingService.prepareCloudformationTags(null, tags);
    return new CreateStackRequest()
            .withStackName(stackName)
            .withOnFailure(OnFailure.DO_NOTHING)
            .withTemplateBody(cloudFormationTemplate)
            .withTags(awsTags)
            .withCapabilities(CAPABILITY_IAM);
}
 
Example #10
Source File: AwsIntegrationTestStackRule.java    From spring-cloud-stream-app-starters with Apache License 2.0 4 votes vote down vote up
@Override
protected void before() throws Throwable {
	try {
		String awsCredentialsDir = System.getProperty("aws.credentials.path");
		File awsCredentialsFile = new File(awsCredentialsDir, "aws.credentials.properties");
		Properties awsCredentials = new Properties();
		awsCredentials.load(new FileReader(awsCredentialsFile));
		String accessKey = awsCredentials.getProperty("cloud.aws.credentials.accessKey");
		String secretKey = awsCredentials.getProperty("cloud.aws.credentials.secretKey");
		this.cloudFormation = new AmazonCloudFormationClient(new BasicAWSCredentials(accessKey, secretKey));

		YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean();
		yamlPropertiesFactoryBean.setResources(new ClassPathResource("application.yml"));
		Properties applicationProperties = yamlPropertiesFactoryBean.getObject();

		this.stackName = applicationProperties.getProperty("cloud.aws.stack.name");

		after();

		ClassPathResource stackTemplate = new ClassPathResource("AwsIntegrationTestTemplate.json");
		String templateBody = FileCopyUtils.copyToString(new InputStreamReader(stackTemplate.getInputStream()));

		this.cloudFormation.createStack(
				new CreateStackRequest()
						.withTemplateBody(templateBody)
						.withOnFailure(OnFailure.DELETE)
						.withStackName(this.stackName));

		waitForCompletion();

		System.setProperty("cloud.aws.credentials.accessKey", accessKey);
		System.setProperty("cloud.aws.credentials.secretKey", secretKey);
	}
	catch (Exception e) {
		if (!(e instanceof AssumptionViolatedException)) {
			Assume.assumeTrue("Can't perform AWS integration test because of: " + e.getMessage(), false);
		}
		else {
			throw e;
		}
	}
}