Example usage for com.amazonaws.services.cloudformation AmazonCloudFormation describeStacks

List of usage examples for com.amazonaws.services.cloudformation AmazonCloudFormation describeStacks

Introduction

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

Prototype

DescribeStacksResult describeStacks();

Source Link

Document

Simplified method form for invoking the DescribeStacks operation.

Usage

From source file:com.netflix.spinnaker.clouddriver.aws.provider.agent.AmazonCloudFormationCachingAgent.java

License:Apache License

@Override
public CacheResult loadData(ProviderCache providerCache) {
    log.info("Describing items in {}", getAgentType());
    AmazonCloudFormation cloudformation = amazonClientProvider.getAmazonCloudFormation(account, region);

    Collection<CacheData> stackCacheData = new ArrayList<>();

    try {// w  ww.  j a v a2s  .co m
        List<Stack> stacks = cloudformation.describeStacks().getStacks();

        for (Stack stack : stacks) {
            Map<String, Object> stackAttributes = new HashMap<>();
            stackAttributes.put("stackId", stack.getStackId());
            stackAttributes.put("tags",
                    stack.getTags().stream().collect(Collectors.toMap(Tag::getKey, Tag::getValue)));
            stackAttributes.put("outputs", stack.getOutputs().stream()
                    .collect(Collectors.toMap(Output::getOutputKey, Output::getOutputValue)));
            stackAttributes.put("stackName", stack.getStackName());
            stackAttributes.put("region", region);
            stackAttributes.put("accountName", account.getName());
            stackAttributes.put("accountId", account.getAccountId());
            stackAttributes.put("stackStatus", stack.getStackStatus());
            stackAttributes.put("creationTime", stack.getCreationTime());

            if (stack.getStackStatus().equals("ROLLBACK_COMPLETE")) {
                DescribeStackEventsRequest request = new DescribeStackEventsRequest()
                        .withStackName(stack.getStackName());
                cloudformation.describeStackEvents(request).getStackEvents().stream()
                        .filter(e -> e.getResourceStatus().equals("CREATE_FAILED")).findFirst()
                        .map(StackEvent::getResourceStatusReason)
                        .map(statusReason -> stackAttributes.put("stackStatusReason", statusReason));
            }
            String stackCacheKey = Keys.getCloudFormationKey(stack.getStackId(), region, account.getName());
            Map<String, Collection<String>> relationships = new HashMap<>();
            relationships.put(STACKS.getNs(), Collections.singletonList(stackCacheKey));
            stackCacheData.add(new DefaultCacheData(stackCacheKey, stackAttributes, relationships));
        }
    } catch (AmazonCloudFormationException e) {
        log.error("Error retrieving stacks", e);
    }

    log.info("Caching {} items in {}", stackCacheData.size(), getAgentType());
    HashMap<String, Collection<CacheData>> result = new HashMap<>();
    result.put(STACKS.getNs(), stackCacheData);
    return new DefaultCacheResult(result);
}