Example usage for com.amazonaws.services.cloudformation AmazonCloudFormationAsyncClient AmazonCloudFormationAsyncClient

List of usage examples for com.amazonaws.services.cloudformation AmazonCloudFormationAsyncClient AmazonCloudFormationAsyncClient

Introduction

In this page you can find the example usage for com.amazonaws.services.cloudformation AmazonCloudFormationAsyncClient AmazonCloudFormationAsyncClient.

Prototype

AmazonCloudFormationAsyncClient(AwsAsyncClientParams asyncClientParams) 

Source Link

Document

Constructs a new asynchronous client to invoke service methods on AWS CloudFormation using the specified parameters.

Usage

From source file:com.carrotgarden.maven.aws.cfn.CarrotCloudForm.java

License:BSD License

private AmazonCloudFormation newClient() {

    final AmazonCloudFormation amazonClient = new AmazonCloudFormationAsyncClient(credentials);

    logger.info("stack endpoint : {}", endpoint);

    amazonClient.setEndpoint(endpoint);/*from w  w w.java 2 s. c o m*/

    return amazonClient;

}

From source file:com.mweagle.tereus.aws.CloudFormation.java

License:Open Source License

public Optional<DescribeStacksResult> createStack(final CreateStackRequest request, final Region awsRegion,
        Logger logger) {/*  w  w  w. jav a  2  s. c o  m*/
    DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
    final AmazonCloudFormationAsyncClient awsClient = new AmazonCloudFormationAsyncClient(
            credentialProviderChain.getCredentials());
    awsClient.setRegion(awsRegion);
    logger.info("Creating stack: {}", request.getStackName());
    Optional<DescribeStacksResult> completionResult = Optional.empty();

    try {
        // There are no prior events for a creation request
        Future<CreateStackResult> createStackRequest = awsClient.createStackAsync(request);
        final CreateStackResult stackResult = createStackRequest.get();
        logger.info("Stack ({}) creation in progress.", stackResult.getStackId());
        completionResult = waitForStackComplete(awsClient, stackResult.getStackId(), Collections.emptyList(),
                logger);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return completionResult;
}

From source file:com.mweagle.tereus.aws.CloudFormation.java

License:Open Source License

public Optional<DescribeStacksResult> updateStack(final UpdateStackRequest request, final Region awsRegion,
        Logger logger) {// ww  w.ja  va 2 s  . c  o  m
    DefaultAWSCredentialsProviderChain credentialProviderChain = new DefaultAWSCredentialsProviderChain();
    final AmazonCloudFormationAsyncClient awsClient = new AmazonCloudFormationAsyncClient(
            credentialProviderChain.getCredentials());
    awsClient.setRegion(awsRegion);
    logger.info("Updating stack: {}", request.getStackName());
    Optional<DescribeStacksResult> completionResult = Optional.empty();

    try {
        final List<StackEvent> priorEvents = getStackEvents(awsClient, request.getStackName(), logger);
        logger.info("Total number of pre-existing stack events: {}", priorEvents.size());
        Future<UpdateStackResult> updateStackResult = awsClient.updateStackAsync(request);
        final UpdateStackResult stackResult = updateStackResult.get();
        logger.info("Stack ({}) creation in progress.", stackResult.getStackId());
        completionResult = waitForStackComplete(awsClient, request.getStackName(), priorEvents, logger);
    } catch (Exception ex) {
        logger.error(ex);
    }
    return completionResult;
}

From source file:com.mweagle.tereus.commands.pipelines.UpdatePipeline.java

License:Open Source License

protected void publishGlobals(ScriptEngine engine) {
    super.publishGlobals(engine);

    // Publish a function that accepts a stack target name, defined by the patch
    // file, that represents the target.  We'll push all this
    Function<String, String> fnStackInfo = (stackName) -> {
        // Get the information...
        final GetTemplateRequest templateRequest = new GetTemplateRequest().withStackName(stackName);
        final AmazonCloudFormationAsyncClient awsClient = new AmazonCloudFormationAsyncClient(
                super.getAwsCredentials());
        awsClient.setRegion(super.getRegion());
        final GetTemplateResult stackTemplateResult = awsClient.getTemplate(templateRequest);

        // Get the stack info and return it
        final DescribeStacksRequest describeRequest = new DescribeStacksRequest().withStackName(stackName);
        final DescribeStacksResult describeResult = awsClient.describeStacks(describeRequest);

        // And finally the tags and parameters
        final Stack stackInfo = !describeResult.getStacks().isEmpty() ? describeResult.getStacks().get(0)
                : null;//from  w ww.j a  v  a 2 s.c om
        final Map<String, Object> tags = (stackInfo != null)
                ? stackInfo.getTags().stream().collect(Collectors.toMap(Tag::getKey, Tag::getValue))
                : Collections.emptyMap();

        final Map<String, Object> params = (stackInfo != null)
                ? stackInfo.getParameters().stream()
                        .collect(Collectors.toMap(Parameter::getParameterKey, Parameter::getParameterValue))
                : Collections.emptyMap();

        HashMap<String, Object> results = new HashMap<>();
        results.put("tags", tags);
        results.put("params", params);
        results.put("stack", stackInfo);
        results.put("template", stackTemplateResult.getTemplateBody().toString());
        Gson gson = new Gson();
        return gson.toJson(results);
    };
    engine.put("TargetStackInfoImpl", fnStackInfo);

    Supplier<String> fnArgs = () -> {
        HashMap<String, Map<String, Object>> args = new HashMap<>();
        args.put("arguments", this.arguments);
        Gson gson = new Gson();
        return gson.toJson(args);
    };
    engine.put("ArgumentsImpl", fnArgs);
}

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

License:Open Source License

protected void updateStack(UpdateInput updateInput, String transformedTemplate, String stackTargetName)
        throws UnsupportedEncodingException {
    if (updateInput.dryRun) {
        updateInput.logger.info("Dry run requested (-n/--noop). Stack update bypassed.");
    } else {/*from w  ww  .  j a  va  2  s . c  o m*/
        // Fetch the stack parameters
        final DescribeStacksRequest stackRequest = new DescribeStacksRequest().withStackName(stackTargetName);
        final AmazonCloudFormationAsyncClient awsClient = new AmazonCloudFormationAsyncClient(
                updateInput.awsCredentials);
        awsClient.setRegion(updateInput.awsRegion);
        final DescribeStacksResult result = awsClient.describeStacks(stackRequest);
        final Stack existantStack = result.getStacks().get(0);

        final Optional<Parameter> s3Bucket = findNamedParameter(CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME,
                existantStack.getParameters());

        Preconditions.checkArgument(s3Bucket.isPresent(),
                "Failed to determine S3 BucketName from existant template via parameter name: "
                        + CONSTANTS.PARAMETER_NAMES.S3_BUCKET_NAME);

        // Super, now put the new content to S3, update the parameter list
        // to include the new URL, and submit the updated stack.
        final byte[] templateBytes = transformedTemplate.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(s3Bucket.get().getParameterValue(), keyName, is,
                Optional.of(Long.valueOf(templateBytes.length)))) {
            // Upload the template
            resource.upload();

            // Go ahead and create the stack.
            final UpdateStackRequest request = new UpdateStackRequest().withStackName(stackTargetName);
            request.setTemplateURL(resource.getResourceURL().get());
            request.setParameters(existantStack.getParameters());
            request.setCapabilities(Arrays.asList("CAPABILITY_IAM"));

            updateInput.logger.debug("Updating stack: {}", stackTargetName);
            final Optional<DescribeStacksResult> updateResult = new CloudFormation().updateStack(request,
                    updateInput.awsRegion, updateInput.logger);

            // If everything worked out, then release the template
            // URL s.t. subsequent ASG instantiated instances have access
            // to the template content
            if (updateResult.isPresent()) {
                updateInput.logger.info("Stack successfully updated");
                updateInput.logger.info(updateResult.get().toString());
                resource.setReleased(true);
            }
        }
    }
}