Example usage for com.amazonaws.services.cloudformation.model DescribeStacksResult getStacks

List of usage examples for com.amazonaws.services.cloudformation.model DescribeStacksResult getStacks

Introduction

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

Prototype


public java.util.List<Stack> getStacks() 

Source Link

Document

A list of stack structures.

Usage

From source file:br.com.ingenieux.mojo.cloudformation.cmd.WaitForStackCommand.java

License:Apache License

public void execute() throws Exception {
    Set<StackEvent> events = new TreeSet<>((o1, o2) -> {
        return o1.getEventId().compareTo(o2.getEventId());
    });/*from  w  ww. j ava  2  s. c o  m*/

    boolean done = false;

    Date timeoutsAt = new Date(System.currentTimeMillis() + 60000L * ctx.getTimeoutMins());

    do {
        boolean timedOut = !timeoutsAt.after(new Date(System.currentTimeMillis()));

        if (timedOut)
            throw new IllegalStateException("Timed Out");

        {
            final DescribeStackEventsRequest req = new DescribeStackEventsRequest()
                    .withStackName(ctx.getStackId());
            String nextToken = null;

            do {
                req.withNextToken(nextToken);

                Optional<DescribeStackEventsResult> stackEvents = getDescribeStackEventsResult(req);

                if (!stackEvents.isPresent()) {
                    return;
                } else {
                    for (StackEvent e : stackEvents.get().getStackEvents()) {
                        if (!events.contains(e)) {
                            ctx.getNotifier().info("" + e);

                            events.add(e);
                        }
                    }
                }

            } while (null != nextToken);
        }

        {
            final DescribeStacksResult stacks = ctx.getClient()
                    .describeStacks(new DescribeStacksRequest().withStackName(ctx.getStackId()));

            Optional<Stack> foundStack = stacks.getStacks().stream().filter(
                    stack -> ctx.getStatusesToMatch().contains(StackStatus.fromValue(stack.getStackStatus())))
                    .findFirst();

            done = foundStack.isPresent();
        }

        if (!done) {
            Thread.sleep(15000);
        }

    } while (!done);
}

From source file:br.com.ingenieux.mojo.cloudformation.LoadStackOutputsMojo.java

License:Apache License

private Collection<Output> listOutputs() {
    if (isEmpty(stackId)) {
        return Collections.emptyList();
    }//  www  .jav  a2  s  . c  o m

    String nextToken = null;
    final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(stackId);
    List<Output> result = new ArrayList<>();

    do {
        request.setNextToken(nextToken);

        final DescribeStacksResult response = getService().describeStacks(request);

        result.addAll(response.getStacks().stream().flatMap(stack -> stack.getOutputs().stream())
                .collect(Collectors.toList()));

        nextToken = response.getNextToken();
    } while (null != nextToken);

    return result;
}

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

License:BSD License

public Stack findStack() throws Exception {

    final DescribeStacksRequest request = new DescribeStacksRequest();

    final DescribeStacksResult result = amazonClient.describeStacks(request);

    for (final Stack stack : result.getStacks()) {
        if (name.equals(stack.getStackName())) {
            return stack;
        }//  ww  w.j  a  v a2 s.  c o m
    }

    return null;

}

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

License:Apache License

protected Stack getStack(AmazonCloudFormationClient cfClient, String stackName) throws MojoExecutionException {
    Stack stack = null;/*from w w  w  .  j a  v a 2  s .  co m*/
    try {
        DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest();
        describeStacksRequest.setStackName(stackName);
        getLog().info("Getting Cloud Formation Stack Details...");
        DescribeStacksResult describeStacksResult = cfClient.describeStacks(describeStacksRequest);
        if (describeStacksResult == null || describeStacksResult.getStacks() == null
                || describeStacksResult.getStacks().isEmpty()) {
            throw new MojoExecutionException("[NULL] Could Not Get Cloud Formation Stack Details");
        }
        stack = describeStacksResult.getStacks().get(0);
    } catch (AmazonServiceException e) {
        throw new MojoExecutionException("[SERVICE] Could Not Get Cloud Formation Stack Details", e);
    } catch (AmazonClientException e) {
        throw new MojoExecutionException("[CLIENT] Could Not Get Cloud Formation Stack Details", e);
    }
    return stack;
}

From source file:com.clicktravel.infrastructure.provisioning.aws.cloudformation.Stack.java

License:Apache License

public com.amazonaws.services.cloudformation.model.Stack describe() {
    final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(name);
    final DescribeStacksResult result = cloudFormationClient.describeStacks(request);
    return result.getStacks().get(0);
}

From source file:com.deploymentio.cfnstacker.CloudFormationClient.java

License:Apache License

/**
 * Finds the stack with the given name. Will only find stack still
 * running/*from   www  . j  av  a2  s.com*/
 * 
 * @param name name of the stack
 * @return the stack object or <code>null</code> if no running stack with
 *         this name was found
 */
public Stack findStack(String name) {
    DescribeStacksResult describeStacksResult = null;
    String nextToken = null;

    do {
        describeStacksResult = client.describeStacks(new DescribeStacksRequest().withNextToken(nextToken));
        nextToken = describeStacksResult.getNextToken();

        for (Stack stack : describeStacksResult.getStacks()) {
            if (stack.getStackName().equals(name)) {
                return stack;
            }
        }
    } while (!StringUtils.isEmpty(nextToken));

    return null;
}

From source file:com.github.kaklakariada.aws.sam.service.CloudformationService.java

License:Open Source License

private List<Stack> describeStack(String stackName) {
    final DescribeStacksResult result = cloudFormation
            .describeStacks(new DescribeStacksRequest().withStackName(stackName));
    return result.getStacks();
}

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()) });
        }/*from   ww  w .j  a  v  a  2s  .  com*/
        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;/*from  w ww  .  j  av a2 s  . c o  m*/
        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 {// ww w.  j a  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);
            }
        }
    }
}