use of com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult in project spring-cloud-aws by awspring.
the class StackConfigurationBeanDefinitionParserTest method stackResourceRegistry_stackConfigurationWithoutStaticName_stackResourceRegistryBeanExposedUnderGeneratedName.
// @checkstyle:off
@Test
void stackResourceRegistry_stackConfigurationWithoutStaticName_stackResourceRegistryBeanExposedUnderGeneratedName() throws Exception {
// @checkstyle:on
// Arrange
HttpServer server = MetaDataServer.setupHttpServer();
HttpContext httpContext = server.createContext("/latest/meta-data/instance-id", new MetaDataServer.HttpResponseWriterHandler("foo"));
GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
AmazonCloudFormation amazonCloudFormation = Mockito.mock(AmazonCloudFormation.class);
when(amazonCloudFormation.describeStackResources(new DescribeStackResourcesRequest().withPhysicalResourceId("foo"))).thenReturn(new DescribeStackResourcesResult().withStackResources(new StackResource().withStackName("test")));
when(amazonCloudFormation.listStackResources(new ListStackResourcesRequest().withStackName("test"))).thenReturn(new ListStackResourcesResult().withStackResourceSummaries(new StackResourceSummary()));
applicationContext.load(new ClassPathResource(getClass().getSimpleName() + "-autoDetectStackName.xml", getClass()));
applicationContext.getBeanFactory().registerSingleton(getBeanName(AmazonCloudFormation.class.getName()), amazonCloudFormation);
applicationContext.refresh();
// Act
StackResourceRegistry autoDetectingStackNameProviderBasedStackResourceRegistry = applicationContext.getBean("io.awspring.cloud.core.env.stack.config.StackResourceRegistryFactoryBean#0", StackResourceRegistry.class);
// Assert
assertThat(autoDetectingStackNameProviderBasedStackResourceRegistry).isNotNull();
server.removeContext(httpContext);
}
use of com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult in project spring-cloud-aws by awspring.
the class AutoDetectingStackNameProvider method autoDetectStackName.
private String autoDetectStackName(String instanceId) {
Assert.notNull(instanceId, "No valid instance id defined");
DescribeStackResourcesResult describeStackResourcesResult = this.amazonCloudFormationClient.describeStackResources(new DescribeStackResourcesRequest().withPhysicalResourceId(instanceId));
if (describeStackResourcesResult != null && describeStackResourcesResult.getStackResources() != null && !describeStackResourcesResult.getStackResources().isEmpty()) {
return describeStackResourcesResult.getStackResources().get(0).getStackName();
}
if (this.amazonEc2Client != null) {
DescribeTagsResult describeTagsResult = this.amazonEc2Client.describeTags(new DescribeTagsRequest().withFilters(new Filter("resource-id", Collections.singletonList(instanceId)), new Filter("resource-type", Collections.singletonList("instance")), new Filter("key", Collections.singletonList("aws:cloudformation:stack-name"))));
if (describeTagsResult != null && describeTagsResult.getTags() != null && !describeTagsResult.getTags().isEmpty()) {
return describeTagsResult.getTags().get(0).getValue();
}
}
return null;
}
use of com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult in project cloudbreak by hortonworks.
the class AutoScalingGroupHandler method getAutoScalingGroups.
public Map<AutoScalingGroup, String> getAutoScalingGroups(AmazonCloudFormationClient cloudFormationClient, AmazonAutoScalingClient autoScalingClient, String stackName) {
DescribeStackResourcesRequest resourcesRequest = new DescribeStackResourcesRequest();
resourcesRequest.setStackName(stackName);
DescribeStackResourcesResult resourcesResult = cloudFormationClient.describeStackResources(resourcesRequest);
Map<String, String> autoScalingGroups = resourcesResult.getStackResources().stream().filter(stackResource -> "AWS::AutoScaling::AutoScalingGroup".equalsIgnoreCase(stackResource.getResourceType())).collect(Collectors.toMap(StackResource::getPhysicalResourceId, StackResource::getLogicalResourceId));
DescribeAutoScalingGroupsRequest request = new DescribeAutoScalingGroupsRequest();
request.setAutoScalingGroupNames(autoScalingGroups.keySet());
List<AutoScalingGroup> scalingGroups = autoScalingClient.describeAutoScalingGroups(request).getAutoScalingGroups();
return scalingGroups.stream().collect(Collectors.toMap(scalingGroup -> scalingGroup, scalingGroup -> autoScalingGroups.get(scalingGroup.getAutoScalingGroupName())));
}
use of com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult in project cloudbreak by hortonworks.
the class AutoScalingGroupHandlerTest method testGetAutoScalingGroups.
@Test
public void testGetAutoScalingGroups() {
CloudResource cfResource = CloudResource.builder().type(ResourceType.CLOUDFORMATION_STACK).name("cf").build();
DescribeStackResourcesResult resourcesResult = new DescribeStackResourcesResult();
StackResource stackResource = new StackResource().withLogicalResourceId("logicalResourceId").withPhysicalResourceId("physicalResourceId").withResourceType("AWS::AutoScaling::AutoScalingGroup");
resourcesResult.getStackResources().add(stackResource);
resourcesResult.getStackResources().add(new StackResource().withResourceType("other"));
when(cloudFormationClient.describeStackResources(any(DescribeStackResourcesRequest.class))).thenReturn(resourcesResult);
DescribeAutoScalingGroupsResult scalingGroupsResult = new DescribeAutoScalingGroupsResult();
AutoScalingGroup autoScalingGroup = new AutoScalingGroup().withAutoScalingGroupName(stackResource.getPhysicalResourceId());
scalingGroupsResult.getAutoScalingGroups().add(autoScalingGroup);
when(autoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(scalingGroupsResult);
Map<AutoScalingGroup, String> autoScalingGroups = underTest.getAutoScalingGroups(cloudFormationClient, autoScalingClient, cfResource);
assertEquals(1, autoScalingGroups.size());
assertEquals(autoScalingGroup, autoScalingGroups.entrySet().stream().findFirst().get().getKey());
assertEquals(stackResource.getLogicalResourceId(), autoScalingGroups.entrySet().stream().findFirst().get().getValue());
ArgumentCaptor<DescribeStackResourcesRequest> stackResourcesRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeStackResourcesRequest.class);
verify(cloudFormationClient).describeStackResources(stackResourcesRequestArgumentCaptor.capture());
assertEquals(cfResource.getName(), stackResourcesRequestArgumentCaptor.getValue().getStackName());
ArgumentCaptor<DescribeAutoScalingGroupsRequest> scalingGroupsRequestArgumentCaptor = ArgumentCaptor.forClass(DescribeAutoScalingGroupsRequest.class);
verify(autoScalingClient).describeAutoScalingGroups(scalingGroupsRequestArgumentCaptor.capture());
assertEquals(1, scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().size());
assertEquals(stackResource.getPhysicalResourceId(), scalingGroupsRequestArgumentCaptor.getValue().getAutoScalingGroupNames().get(0));
}
use of com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult in project cloudbreak by hortonworks.
the class AwsCloudFormationErrorMessageProviderTest method testWhenMessageExtractedFromCfEvent.
@Test
void testWhenMessageExtractedFromCfEvent() {
StackEvent event = new StackEvent().withResourceStatus(ResourceStatus.CREATE_FAILED).withResourceStatusReason("Error");
DescribeStackEventsResult result = new DescribeStackEventsResult().withStackEvents(event);
when(cfRetryClient.describeStacks(any())).thenReturn(new DescribeStacksResult());
when(cfRetryClient.describeStackResources(any())).thenReturn(new DescribeStackResourcesResult());
when(cfRetryClient.describeStackEvents(any(DescribeStackEventsRequest.class))).thenReturn(result);
String actual = underTest.getErrorReason(credentialView, REGION, STACK_NAME, ResourceStatus.CREATE_FAILED);
assertEquals(actual, event.getResourceStatusReason());
}
Aggregations