/** * Creates a new stack. * * @param name Stack name. * @param parameters Input parameters. * @param templatePath Classpath to the JSON template of the stack. * @return Stack ID */ public String createStack(final String name, final Map<String, String> parameters, final String templatePath, final boolean iamCapabilities) { logger.info(String.format("Executing the Cloud Formation: %s, Stack Name: %s", templatePath, name)); final CreateStackRequest request = new CreateStackRequest() .withStackName(name) .withParameters(convertParameters(parameters)) .withTemplateBody(getTemplateText(templatePath)); if (iamCapabilities) { request.getCapabilities().add("CAPABILITY_IAM"); } final CreateStackResult result = cloudFormationClient.createStack(request); return result.getStackId(); }
public static StackNameAndId createTemporarySimpleStack(AmazonCloudFormation cfnClient, String vpcId, String arn) throws IOException { CreateStackRequest createStackRequest = new CreateStackRequest(); createStackRequest.setStackName(TEMPORARY_STACK); File file = new File(FilesForTesting.SIMPLE_STACK); createStackRequest.setTemplateBody(FileUtils.readFileToString(file , Charset.defaultCharset())); Collection<Parameter> parameters = new LinkedList<>(); parameters.add(createParam("env", EnvironmentSetupForTests.ENV)); parameters.add(createParam("vpc", vpcId)); if (!arn.isEmpty()) { Collection<String> notificationARNs = new LinkedList<>(); notificationARNs.add(arn); logger.debug("Adding arn subscription "+ arn); createStackRequest.setNotificationARNs(notificationARNs); } createStackRequest.setParameters(parameters); CreateStackResult result = cfnClient.createStack(createStackRequest); return new StackNameAndId(TEMPORARY_STACK, result.getStackId()); }
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); }
public void execute() { checkParams(); AmazonCloudFormationClient client = getOrCreateClient(AmazonCloudFormationClient.class); CreateStackRequest createStackRequest = new CreateStackRequest() .withDisableRollback(disableRollback).withOnFailure(onFailure) .withStackName(stackName).withStackPolicyBody(stackPolicyBody) .withStackPolicyURL(stackPolicyURL) .withTemplateBody(templateBody).withTemplateURL(templateURL) .withTimeoutInMinutes(timeoutInMinutes); if (capabilities.size() > 0) { createStackRequest.setCapabilities(capabilities); } if (parameters.size() > 0) { createStackRequest.setParameters(parameters); } if (tags.size() > 0) { createStackRequest.setTags(tags); } try { client.createStack(createStackRequest); System.out.println("Create stack " + stackName + " request submitted."); if(waitForCreation) { WaitForStackToReachStateTask.waitForCloudFormationStackToReachStatus(client, stackName, CREATE_COMPLETE); } } catch (Exception e) { throw new BuildException( "Could not create stack " + e.getMessage(), e); } }
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)); }
@Test @Parameters({ "networkName", "description", "publicInAccount", "regionName", "vpcStackName", "vpcName", "existingSubnet" }) public void createNetwork(String networkName, @Optional("") String description, @Optional("false") boolean publicInAccount, String regionName, @Optional("it-vpc-stack") String vpcStackName, @Optional("it-vpc") String vpcName, boolean existingSubnet) { AmazonCloudFormationClient client = new AmazonCloudFormationClient(); client.setRegion(RegionUtils.getRegion(regionName)); Map<String, Object> networkMap = new HashMap<>(); String vpcCreationJson = existingSubnet ? "public_vpc_with_subnet.json" : "public_vpc_wihout_subnet.json"; try (InputStream vpcJsonInputStream = getClass().getResourceAsStream("/cloudformation/" + vpcCreationJson)) { String vpcCFTemplateString = IOUtils.toString(vpcJsonInputStream); CreateStackRequest stackRequest = createStackRequest(vpcStackName, vpcName, vpcCFTemplateString); client.createStack(stackRequest); List<Output> outputForRequest = getOutputForRequest(vpcStackName, client); if (existingSubnet) { networkMap.put("vpcId", outputForRequest.get(0).getOutputValue()); networkMap.put("subnetId", outputForRequest.get(1).getOutputValue()); } else { networkMap.put("vpcId", outputForRequest.get(1).getOutputValue()); networkMap.put("internetGatewayId", outputForRequest.get(0).getOutputValue()); } } catch (IOException e) { LOGGER.error("can't read vpc cloudformation template file"); throw new RuntimeException(e); } NetworkRequest networkRequest = new NetworkRequest(); networkRequest.setName(networkName); networkRequest.setDescription(description); networkRequest.setParameters(networkMap); if (!existingSubnet) { networkRequest.setSubnetCIDR("10.0.0.0/24"); } networkRequest.setCloudPlatform("AWS"); String id = getCloudbreakClient().networkEndpoint().postPrivate(networkRequest).getId().toString(); getItContext().putContextParam(CloudbreakITContextConstants.NETWORK_ID, id, true); }
@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; } } }
@Override public void addMonitoringTo(CreateStackRequest createStackRequest) { // does nothing in this implementation }
@Override public void addMonitoringTo(CreateStackRequest createStackRequest) throws NotReadyException { Collection<String> arns = getArns(); createStackRequest.setNotificationARNs(arns); }
/** */ public Stack stackCreate() throws Exception { final CreateStackRequest request = new CreateStackRequest(); request.withStackName(name); request.withParameters(paramList); request.withTemplateBody(template); amazonClient.createStack(request); final Stack stack = waitForStackCreate(); return stack; }
void addMonitoringTo(CreateStackRequest createStackRequest) throws NotReadyException;