/** * Returns a string representation of this object. This is useful for testing and debugging. Sensitive data will be * redacted from this string using a placeholder value. * * @return A string representation of this object. * * @see java.lang.Object#toString() */ @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{"); if (getStacks() != null) sb.append("Stacks: ").append(getStacks()).append(","); if (getNextToken() != null) sb.append("NextToken: ").append(getNextToken()); sb.append("}"); return sb.toString(); }
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (obj instanceof DescribeStacksResult == false) return false; DescribeStacksResult other = (DescribeStacksResult) obj; if (other.getStacks() == null ^ this.getStacks() == null) return false; if (other.getStacks() != null && other.getStacks().equals(this.getStacks()) == false) return false; if (other.getNextToken() == null ^ this.getNextToken() == null) return false; if (other.getNextToken() != null && other.getNextToken().equals(this.getNextToken()) == false) return false; return true; }
private Function<DescribeStacksResult, Stack> firstStack() { return (dsr) -> dsr.getStacks().get(0); } }
private Stack getStack(DescribeStacksResult result) { for (Stack aStack : result.getStacks()) { if (getExpandedStackName().equals(aStack.getStackName())) { return aStack; } } return null; }
public static String getStackOutput(String stackName, String key){ DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest(); describeStacksRequest.withStackName(stackName); DescribeStacksResult describeStacksResult = client.describeStacks(describeStacksRequest); List<Output> outputs = describeStacksResult.getStacks().get(0).getOutputs(); Optional<Output> output = outputs.stream() .filter(index -> index.getOutputKey().equals(key)) .findFirst(); return output.map(Output::getOutputValue) .orElseThrow(() -> new RuntimeException("Did not find stack output.")); } }
private void waitForStackCreationToComplete() throws InterruptedException { DescribeStacksRequest check = new DescribeStacksRequest().withStackName(getStackName()); boolean completed = false; while(!completed) { List<Stack> results = client.describeStacks(check).getStacks(); if(results.isEmpty()){ throw new RuntimeException("Stack Does Not Exist."); } else { if(results.stream().anyMatch(this::checkIfStackCreationFailed)) { throw new RuntimeException("Stack Creation or Update Failed"); } else if(results.stream().anyMatch(this::checkIfStackCreationSucceeded)) { System.out.println("Stack Creation Complete"); completed = true; } } if(!completed) Thread.sleep(5000); } }
private Collection<Output> listOutputs() { if (isEmpty(stackId)) { return Collections.emptyList(); } 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; } }
public boolean exists() { try { DescribeStacksResult result = this.client.describeStacks(new DescribeStacksRequest().withStackName(this.stack)); return !result.getStacks().isEmpty(); } catch (AmazonCloudFormationException e) { if ("AccessDenied".equals(e.getErrorCode())) { this.listener.getLogger().format("Got error from describeStacks: %s %n", e.getErrorMessage()); throw e; } else if ("ValidationError".equals(e.getErrorCode()) && e.getErrorMessage().contains("does not exist")) { return false; } else { throw e; } } }
public static List<Stack> describeCloudFormationStack(final String stackName, final AmazonCloudFormation cloudFormationClient) { DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest() .withStackName(stackName); return cloudFormationClient.describeStacks(describeStacksRequest).getStacks(); }
public Map<String, String> describeOutputs() { DescribeStacksResult result = this.client.describeStacks(new DescribeStacksRequest().withStackName(this.stack)); Stack cfnStack = result.getStacks().get(0); Map<String, String> map = new HashMap<>(); for (Output output : cfnStack.getOutputs()) { map.put(output.getOutputKey(), output.getOutputValue()); } return map; }
private List<Stack> describeStack(String stackName) { final DescribeStacksResult result = cloudFormation .describeStacks(new DescribeStacksRequest().withStackName(stackName)); return result.getStacks(); }
private Properties getCloudformationOutputs(AmazonCloudFormation cloudFormation, CreateStackResult stack) { DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest(); describeStacksRequest.setStackName(stack.getStackId()); final DescribeStacksResult describeStacksResult = cloudFormation .describeStacks(describeStacksRequest); Properties outputProps = new Properties(); for (Stack st : describeStacksResult.getStacks()) { StringBuilder outputsStr = new StringBuilder("Infrastructure/Deployment outputs {\n"); for (Output output : st.getOutputs()) { outputProps.setProperty(output.getOutputKey(), output.getOutputValue()); outputsStr.append(output.getOutputKey()).append("=").append(output.getOutputValue()).append("\n"); } //Log cfn outputs logger.info(outputsStr.toString() + "\n}"); } return outputProps; }
public Map<String, String> getStackParameters(String stackName) { DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest(); describeStacksRequest.setStackName(stackName); DescribeStacksResult describeStacksResult = client .describeStacks(describeStacksRequest); List<Stack> stacks = describeStacksResult.getStacks(); Stack stack = stacks.get(0); List<Parameter> parameters = stack.getParameters(); Map<String, String> map = new LinkedHashMap<String, String>(); for (Parameter parameter : parameters) { map.put(parameter.getParameterKey(), parameter.getParameterValue()); } return map; }
@Override protected Map<String, String> createInstance() throws Exception { LinkedHashMap<String, String> userTags = new LinkedHashMap<>(); DescribeStacksResult stacksResult = this.amazonCloudFormation.describeStacks(new DescribeStacksRequest().withStackName(this.stackNameProvider.getStackName())); for (Stack stack : stacksResult.getStacks()) { for (Tag tag : stack.getTags()) { userTags.put(tag.getKey(), tag.getValue()); } } return userTags; } }
@Override protected Map<String, String> createInstance() throws Exception { LinkedHashMap<String, String> userTags = new LinkedHashMap<>(); DescribeStacksResult stacksResult = this.amazonCloudFormation.describeStacks(new DescribeStacksRequest().withStackName(this.stackNameProvider.getStackName())); for (Stack stack : stacksResult.getStacks()) { for (Tag tag : stack.getTags()) { userTags.put(tag.getKey(), tag.getValue()); } } return userTags; } }
@TaskAction public void createChangeSet() throws InterruptedException, IOException { // to enable conventionMappings feature String stackName = getStackName(); List<String> stableStatuses = getStableStatuses(); if (stackName == null) { throw new GradleException("stackName is not specified"); } AmazonCloudFormationPluginExtension ext = getProject().getExtensions().getByType(AmazonCloudFormationPluginExtension.class); AmazonCloudFormation cfn = ext.getClient(); DescribeStacksResult describeStackResult = cfn.describeStacks(new DescribeStacksRequest().withStackName(stackName)); Stack stack = describeStackResult.getStacks().get(0); if (stableStatuses.contains(stack.getStackStatus())) { createChangeSet(cfn); } else { throw new GradleException("invalid status for create change set: " + stack.getStackStatus()); } }
public CloudFormationOutput getStackOutputs(AmazonCloudFormation stackbuilder, String stackName) { DescribeStacksRequest wait = new DescribeStacksRequest(); wait.setStackName(stackName); List<Stack> stacks = getCloudFormationClient().describeStacks(wait).getStacks(); CloudFormationOutput cloudFormationOutput = new CloudFormationOutput(); for (Stack stack : stacks) { if (stack.getStackName().equals(stackName)) { stack.getOutputs().forEach(output -> { if (output.getOutputKey().equals(LAMBDA_EXECUTION_IAM_RESOURCE_NAME)) { cloudFormationOutput.setLambdaExecutionRole(output.getOutputValue()); } if (output.getOutputKey().equals(LAMBDA_EXECUTION_NAME)) { cloudFormationOutput.setLambdaFunctionArn(output.getOutputValue()); } }); return cloudFormationOutput; } } throw new RuntimeException("Unknown Cloudformation error. Try deploying."); }