Example usage for com.amazonaws.services.cloudformation.model CreateStackRequest getTags

List of usage examples for com.amazonaws.services.cloudformation.model CreateStackRequest getTags

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation.model CreateStackRequest getTags.

Prototype


public java.util.List<Tag> getTags() 

Source Link

Document

Key-value pairs to associate with this stack.

Usage

From source file:com.mweagle.tereus.commands.CreateCommand.java

License:Open Source License

protected void createStack(Optional<String> stackName, TereusInput tereusInput, JsonElement templateData,
        boolean logTemplate) throws UnsupportedEncodingException {
    if (tereusInput.dryRun) {
        tereusInput.logger.info("Dry run requested (-n/--noop). Stack creation bypassed.");
        if (logTemplate) {
            final String formattedTemplate = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping()
                    .create().toJson(templateData);
            tereusInput.logger.info("Stack Template:\n {}", formattedTemplate);
        }/*from www .j  av a  2  s.co m*/
    } else {
        final String bucketName = tereusInput.params.get(CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME).toString();
        // Upload the template
        final String templateContent = new GsonBuilder().create().toJson(templateData);
        final byte[] templateBytes = templateContent.getBytes("UTF-8");
        final InputStream is = new ByteArrayInputStream(templateBytes);
        final String templateDigest = DigestUtils.sha256Hex(templateBytes);
        final String keyName = String.format("%s-tereus.cf.template", templateDigest);

        try (S3Resource resource = new S3Resource(bucketName, keyName, is,
                Optional.of(Long.valueOf(templateBytes.length)))) {
            resource.upload();
            final EstimateTemplateCostRequest costRequest = new EstimateTemplateCostRequest();
            costRequest.setParameters(toParameterList(tereusInput.params));
            costRequest.setTemplateURL(resource.getResourceURL().get());
            final AmazonCloudFormationClient awsClient = new AmazonCloudFormationClient(
                    tereusInput.awsCredentials);
            awsClient.setRegion(tereusInput.awsRegion);
            final EstimateTemplateCostResult costResult = awsClient.estimateTemplateCost(costRequest);
            tereusInput.logger.info("Cost Estimator: {}", costResult.getUrl());

            // Go ahead and create the stack.
            final String defaultTemplateName = String.format("Tereus-%s", System.currentTimeMillis());
            final CreateStackRequest request = new CreateStackRequest()
                    .withStackName(stackName.orElse(defaultTemplateName))
                    .withTemplateURL(resource.getResourceURL().get())
                    .withParameters(toParameterList(tereusInput.params)).withTags(toTagList(tereusInput.tags))
                    .withCapabilities("CAPABILITY_IAM");
            tereusInput.logger.debug("Creating stack: {}", stackName);
            tereusInput.logger.debug("Stack params: {}", request.getParameters());
            tereusInput.logger.debug("Stack tags: {}", request.getTags());
            final Optional<DescribeStacksResult> result = new CloudFormation().createStack(request,
                    tereusInput.awsRegion, tereusInput.logger);
            if (result.isPresent()) {
                tereusInput.logger.info("Stack successfully created");
                tereusInput.logger.info(result.get().toString());
                resource.setReleased(true);
            }
        }
    }
}