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); }
/** * 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; } }
private CreateStackRequest createCreateStackRequest(AuthenticatedContext ac, CloudStack stack, String cFStackName, String subnet, String cfTemplate) { return new CreateStackRequest() .withStackName(cFStackName) .withOnFailure(OnFailure.DO_NOTHING) .withTemplateBody(cfTemplate) .withTags(awsTagPreparationService.prepareTags(ac, stack.getTags())) .withCapabilities(CAPABILITY_IAM) .withParameters(getStackParameters(ac, stack, cFStackName, subnet)); }
@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; } } }