Example usage for com.amazonaws.services.cloudformation.model Stack getParameters

List of usage examples for com.amazonaws.services.cloudformation.model Stack getParameters

Introduction

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

Prototype


public java.util.List<Parameter> getParameters() 

Source Link

Document

A list of Parameter structures.

Usage

From source file:com.cleanenergyexperts.aws.cf.CloudFormationMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    getLog().info("Bucket Name: " + bucketName);
    //getLog().info("Cloud Formation Stack Name: " + stackName);

    if (artifactFile == null || !artifactFile.isFile()) {
        throw new MojoExecutionException("Cannot find artifact file to upload");
    }/*ww  w.ja v  a  2  s.c om*/
    String artifactKey = artifactFile.getName();
    getLog().info("Artifact Name: " + artifactKey);

    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonCloudFormationClient cfClient = new AmazonCloudFormationClient(awsCredentials);
    cfClient.setEndpoint(getCloudFormationEndPoint());
    AmazonS3Client s3Client = new AmazonS3Client(awsCredentials);

    // Upload Artifact to S3
    try {
        getLog().info("Uploading artifact to S3...");
        s3Client.putObject(bucketName, artifactKey, artifactFile);
    } catch (AmazonServiceException e) {
        throw new MojoExecutionException("[SERVICE] Could Not Upload File to S3", e);
    } catch (AmazonClientException e) {
        throw new MojoExecutionException("[CLIENT] Could Not Upload File to S3", e);
    }

    // Update each stack with the new artifact file
    for (String stackName : stackNames) {
        getLog().info("Cloud Formation Stack Name: " + stackName);
        String templateBody = getTemplateBody(cfClient, stackName);
        Stack stack = getStack(cfClient, stackName);

        // If passed additional parameters, update them
        List<Parameter> parameters = stack.getParameters();
        if (stackParameters != null && !stackParameters.isEmpty()) {
            List<Parameter> tmpParams = new ArrayList<Parameter>();

            // Add Existing Parameters we haven't locally overwritten
            for (Parameter oldParam : parameters) {
                String oldKey = oldParam.getParameterKey();
                if (!stackParameters.containsKey(oldKey)) {
                    tmpParams.add(oldParam);
                }
            }

            // Add Overwrite parameters
            for (String key : stackParameters.keySet()) {
                Parameter newParam = new Parameter();
                newParam.setParameterKey(key);
                newParam.setParameterValue(stackParameters.get(key));
                tmpParams.add(newParam);
            }
            parameters = tmpParams;
        }

        // Update the Stack
        UpdateStackRequest updateStackRequest = new UpdateStackRequest();
        updateStackRequest.setStackName(stackName);
        updateStackRequest.setTemplateBody(templateBody);
        updateStackRequest.setParameters(parameters);
        updateStackRequest.setCapabilities(stack.getCapabilities());
        try {
            getLog().info("Updating Cloud Formation Stack...");
            cfClient.updateStack(updateStackRequest);
        } catch (AmazonServiceException e) {
            throw new MojoExecutionException("[SERVICE] Could Not Update Cloud Formation Stack", e);
        } catch (AmazonClientException e) {
            throw new MojoExecutionException("[CLIENT] Could Not Update Cloud Formation Stack", e);
        }
        getLog().info("Cloud Formation Stack " + stackName + "is now updating...");
    }

    getLog().info("All stacks have been updated. Complete.");
}

From source file:com.haskins.cloudtrailviewer.dialog.resourcedetail.detailpanels.CfStackDetail.java

License:Open Source License

private void buildUI(DescribeStacksResult detail) {

    JTabbedPane tabs = new JTabbedPane();
    tabs.add("Stack", primaryScrollPane);

    final JTable outputCheckTable = new JTable(outputTableModel);
    JScrollPane healthCheckScrollPane = new JScrollPane(outputCheckTable);
    tabs.add("Output", healthCheckScrollPane);

    final JTable paramsTable = new JTable(parametersTableModel);
    JScrollPane listenersScrollPane = new JScrollPane(paramsTable);
    tabs.add("Parameters", listenersScrollPane);

    this.add(tabs, BorderLayout.CENTER);

    if (!detail.getStacks().isEmpty()) {

        List<Stack> stacks = detail.getStacks();
        Stack stack = stacks.get(0);

        if (stack.getCreationTime() != null) {
            primaryTableModel.addRow(new Object[] { "Created", getDateString(stack.getCreationTime()) });
        }//w w w .ja va  2s .  c o  m
        if (stack.getDescription() != null) {
            primaryTableModel.addRow(new Object[] { "Description", stack.getDescription() });
        }
        if (stack.getDisableRollback() != null) {
            primaryTableModel.addRow(new Object[] { "Disable Rollback", stack.getDisableRollback() });
        }
        if (stack.getLastUpdatedTime() != null) {
            primaryTableModel
                    .addRow(new Object[] { "Last Updated", getDateString(stack.getLastUpdatedTime()) });
        }
        if (stack.getNotificationARNs() != null) {
            primaryTableModel.addRow(new Object[] { "Notification Arns", stack.getNotificationARNs() });
        }
        if (stack.getStackId() != null) {
            primaryTableModel.addRow(new Object[] { "Stacks Id", stack.getStackId() });
        }
        if (stack.getStackName() != null) {
            primaryTableModel.addRow(new Object[] { "Stacks Name", stack.getStackName() });
        }
        if (stack.getStackStatus() != null) {
            primaryTableModel.addRow(new Object[] { "Stacks Status", stack.getStackStatus() });
        }
        if (stack.getStackStatusReason() != null) {
            primaryTableModel.addRow(new Object[] { "Stacks Status Reason", stack.getStackStatusReason() });
        }
        if (stack.getTimeoutInMinutes() != null) {
            primaryTableModel.addRow(new Object[] { "Timeout (minutes)", stack.getTimeoutInMinutes() });
        }

        /**
         * Tags
         */
        List<Tag> tags = stack.getTags();
        for (Tag tag : tags) {
            tagsTableModel.addRow(new Object[] { tag.getKey(), tag.getValue() });
        }

        /**
         * Output
         */
        outputTableModel.addColumn("Description");
        outputTableModel.addColumn("Key");
        outputTableModel.addColumn("Value");

        List<Output> outputs = stack.getOutputs();
        for (Output output : outputs) {
            tagsTableModel.addRow(
                    new Object[] { output.getDescription(), output.getOutputKey(), output.getOutputValue() });
        }

        /**
         * Parameters
         */
        parametersTableModel.addColumn("Key");
        parametersTableModel.addColumn("Value");
        parametersTableModel.addColumn("User Previous Value");

        List<Parameter> parameters = stack.getParameters();
        for (Parameter parameter : parameters) {
            tagsTableModel.addRow(new Object[] { parameter.getParameterKey(), parameter.getParameterValue(),
                    parameter.getUsePreviousValue() });
        }
    }
}

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;/*w  ww.j  a  v a 2s.  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 {//  www.ja v  a 2s.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);
            }
        }
    }
}

From source file:org.xmlsh.aws.cfnDescribeStacks.java

License:BSD License

private void writeStack(Stack stack) throws XMLStreamException {
    startElement("stack");
    attribute("creation-time", stack.getCreationTime());
    attribute("description", stack.getDescription());
    attribute("disable-rollback", stack.getDisableRollback());
    attribute("last-update-time", stack.getLastUpdatedTime());
    attribute("stack-id", stack.getStackId());
    attribute("stack-name", stack.getStackName());
    attribute("stack-status", stack.getStackStatus());
    attribute("stack-status-reason", stack.getStackStatusReason());
    attribute("timeout", stack.getTimeoutInMinutes());

    writeParameters(stack.getParameters());
    writeOutputs(stack.getOutputs());/*from  w  w w  .j a v a2  s .com*/
    writeCapibilities(stack.getCapabilities());
    writeNotifications(stack.getNotificationARNs());
    writeTags(stack.getTags());
}