/** * Returns the current status of the named stack. * * @param stackId Stack ID. * @return Stack status data. */ @Nullable public StackStatus getStackStatus(final String stackId) { final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(stackId); try { final DescribeStacksResult result = cloudFormationClient.describeStacks(request); if (result.getStacks().size() > 0) { final String status = result.getStacks().get(0).getStackStatus(); if (StringUtils.isNotBlank(status)) { return StackStatus.fromValue(status); } } } catch (final AmazonServiceException ase) { // Stack doesn't exist, just return with no status if (ase.getStatusCode() != 400) { throw ase; } } return null; }
/** * Since there doesn't appear to be a first class way through the SDK at this time to get a CF export. We can * iterate through the stacks for a given output key and return the value. * @param outputKey The exported CF variable to search and retrieve the value of. * @return The value for the export if found */ public Optional<String> searchStacksForOutput(String outputKey) { DescribeStacksResult describeStacksResult = null; do { DescribeStacksRequest request = new DescribeStacksRequest(); if (describeStacksResult != null && describeStacksResult.getNextToken() != null) { request.withNextToken(describeStacksResult.getNextToken()); } describeStacksResult = cloudFormationClient.describeStacks(); for (Stack stack : describeStacksResult.getStacks()) { for (Output output : stack.getOutputs()) { if (StringUtils.equals(output.getOutputKey(), outputKey)) { return Optional.of(output.getOutputValue()); } } } } while (describeStacksResult.getNextToken() != null); return Optional.empty(); }
@Test public void getObject_stackWithTagsDefined_createTagsMap() throws Exception { //Arrange AmazonCloudFormation cloudFormation = mock(AmazonCloudFormation.class); StackNameProvider stackNameProvider = mock(StackNameProvider.class); when(stackNameProvider.getStackName()).thenReturn("testStack"); when(cloudFormation.describeStacks(new DescribeStacksRequest().withStackName("testStack"))). thenReturn(new DescribeStacksResult().withStacks(new Stack().withTags( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2") ))); StackResourceUserTagsFactoryBean factoryBean = new StackResourceUserTagsFactoryBean(cloudFormation, stackNameProvider); //Act factoryBean.afterPropertiesSet(); Map<String, String> factoryBeanObject = factoryBean.getObject(); //Assert assertEquals("value1", factoryBeanObject.get("key1")); assertEquals("value2", factoryBeanObject.get("key2")); }
@Test public void parseInternal_stackConfigurationWithExternallyConfiguredCloudFormationClient_returnsConfiguredStackWithExternallyConfiguredClient() throws Exception { //Arrange DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory); reader.loadBeanDefinitions(new ClassPathResource(getClass().getSimpleName() + "-withCustomCloudFormationClient.xml", getClass())); AmazonCloudFormation amazonCloudFormationMock = beanFactory.getBean(AmazonCloudFormation.class); when(amazonCloudFormationMock.listStackResources(new ListStackResourcesRequest().withStackName("test"))). thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary())); when(amazonCloudFormationMock.describeStacks(new DescribeStacksRequest().withStackName("test"))). thenReturn(new DescribeStacksResult().withStacks(new Stack())); //Act StackResourceRegistry stackResourceRegistry = beanFactory.getBean(StackResourceRegistry.class); //Assert assertNotNull(stackResourceRegistry); assertFalse(beanFactory.containsBeanDefinition(getBeanName(AmazonCloudFormationClient.class.getName()))); verify(amazonCloudFormationMock, times(1)).listStackResources(new ListStackResourcesRequest().withStackName("test")); beanFactory.getBean("customStackTags"); verify(amazonCloudFormationMock, times(1)).describeStacks(new DescribeStacksRequest().withStackName("test")); }
/** * Returns the current status of the named stack. * * @param stackId Stack name. * @return Stack outputs data. */ public Map<String, String> getStackParameters(final String stackId) { final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(stackId); final DescribeStacksResult result = cloudFormationClient.describeStacks(request); final Map<String, String> parameters = Maps.newHashMap(); if (result.getStacks().size() > 0) { parameters.putAll(result.getStacks().get(0).getParameters().stream().collect( Collectors.toMap(Parameter::getParameterKey, Parameter::getParameterValue))); } return parameters; }
/** * Returns the current status of the named stack. * * @param stackId Stack name. * @return Stack outputs data. */ public Map<String, String> getStackOutputs(final String stackId) { final DescribeStacksRequest request = new DescribeStacksRequest().withStackName(stackId); final DescribeStacksResult result = cloudFormationClient.describeStacks(request); final Map<String, String> outputs = Maps.newHashMap(); if (result.getStacks().size() > 0) { outputs.putAll(result.getStacks().get(0).getOutputs().stream().collect( Collectors.toMap(Output::getOutputKey, Output::getOutputValue))); } return outputs; }
private void listExistingApplications(CommandLine cmd, String region) { logger.debug("list environments for region: {}", region); DescribeStacksResult dst = null; if (cmd.hasOption("e")) { dst = provider.describeStacks(cmd.getOptionValue("e")); } else { dst = provider.describeStacks(null); } logger.debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); logger.info("active applications: {}", dst); logger.debug("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); }
@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; }
private DescribeStackResourcesResult getStackResources(String stackName) throws InterruptedException, IOException { try { DescribeStacksResult describeStacksResult = this.amazonCloudFormationClient.describeStacks(new DescribeStacksRequest().withStackName(stackName)); for (Stack stack : describeStacksResult.getStacks()) { if (isAvailable(stack)) { return this.amazonCloudFormationClient.describeStackResources(new DescribeStackResourcesRequest().withStackName(stack.getStackName())); } if (isError(stack)) { if (this.stackCreatedByThisInstance) { throw new IllegalArgumentException("Could not create stack"); } this.amazonCloudFormationClient.deleteStack(new DeleteStackRequest().withStackName(stack.getStackName())); return getStackResources(stackName); } if (isInProgress(stack)) { //noinspection BusyWait Thread.sleep(5000L); return getStackResources(stackName); } } } catch (AmazonClientException e) { String templateBody = FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource(TEMPLATE_PATH).getInputStream())); this.amazonCloudFormationClient.createStack(new CreateStackRequest().withTemplateBody(templateBody).withOnFailure(OnFailure.DELETE). withStackName(stackName).withTags(new Tag().withKey("tag1").withValue("value1")). withParameters(new Parameter().withParameterKey("RdsPassword").withParameterValue(this.rdsPassword))); this.stackCreatedByThisInstance = true; } return getStackResources(stackName); }
private static Stack getStack(DescribeStacksResult describeStacksResult, String stackName) { for (Stack stack : describeStacksResult.getStacks()) { if (stack.getStackName().equals(stackName)) { return stack; } } throw new IllegalStateException("No stack found with name '" + stackName + "' (available stacks: " + allStackNames(describeStacksResult) + ")"); }
@Override public String getInstanceId() { DescribeStacksResult describeStacksResult = this.amazonCloudFormationClient.describeStacks(new DescribeStacksRequest()); Stack stack = getStack(describeStacksResult, this.stackName); return getOutputValue(stack, this.outputKey); }
private List<String> fetchCurrentStacks() { List<String> current = new LinkedList<>(); DescribeStacksResult result = cfnClient.describeStacks(); for(Stack stack : result.getStacks()) { current.add(stack.getStackName()); } return current; }
private List<Stack> describeStack(String stackName) { final DescribeStacksResult result = cloudFormation .describeStacks(new DescribeStacksRequest().withStackName(stackName)); return result.getStacks(); }
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; } } return null; }
DescribeStacksResult describeStacks(String environment);