Search in sources :

Example 1 with DescribeStackResourcesResult

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);
}
Also used : AmazonCloudFormation(com.amazonaws.services.cloudformation.AmazonCloudFormation) ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) HttpContext(com.sun.net.httpserver.HttpContext) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) ListStackResourcesRequest(com.amazonaws.services.cloudformation.model.ListStackResourcesRequest) GenericXmlApplicationContext(org.springframework.context.support.GenericXmlApplicationContext) ClassPathResource(org.springframework.core.io.ClassPathResource) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) StackResourceRegistry(io.awspring.cloud.core.env.stack.StackResourceRegistry) HttpServer(com.sun.net.httpserver.HttpServer) MetaDataServer(io.awspring.cloud.context.MetaDataServer) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary) StackResource(com.amazonaws.services.cloudformation.model.StackResource) Test(org.junit.jupiter.api.Test)

Example 2 with DescribeStackResourcesResult

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;
}
Also used : DescribeTagsRequest(com.amazonaws.services.ec2.model.DescribeTagsRequest) DescribeTagsResult(com.amazonaws.services.ec2.model.DescribeTagsResult) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) Filter(com.amazonaws.services.ec2.model.Filter) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest)

Example 3 with DescribeStackResourcesResult

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())));
}
Also used : Logger(org.slf4j.Logger) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) LoggerFactory(org.slf4j.LoggerFactory) Collectors(java.util.stream.Collectors) StackResource(com.amazonaws.services.cloudformation.model.StackResource) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) List(java.util.List) Component(org.springframework.stereotype.Component) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) UpdateAutoScalingGroupRequest(com.amazonaws.services.autoscaling.model.UpdateAutoScalingGroupRequest) Map(java.util.Map) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) LaunchConfiguration(com.amazonaws.services.autoscaling.model.LaunchConfiguration) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest)

Example 4 with DescribeStackResourcesResult

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));
}
Also used : AutoScalingGroup(com.amazonaws.services.autoscaling.model.AutoScalingGroup) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) StackResource(com.amazonaws.services.cloudformation.model.StackResource) Test(org.junit.Test)

Example 5 with DescribeStackResourcesResult

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());
}
Also used : DescribeStackEventsResult(com.amazonaws.services.cloudformation.model.DescribeStackEventsResult) StackEvent(com.amazonaws.services.cloudformation.model.StackEvent) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) DescribeStackEventsRequest(com.amazonaws.services.cloudformation.model.DescribeStackEventsRequest) DescribeStacksResult(com.amazonaws.services.cloudformation.model.DescribeStacksResult) Test(org.junit.jupiter.api.Test)

Aggregations

DescribeStackResourcesResult (com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult)15 StackResource (com.amazonaws.services.cloudformation.model.StackResource)11 Test (org.junit.jupiter.api.Test)9 DescribeStackResourcesRequest (com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest)8 DescribeStackEventsRequest (com.amazonaws.services.cloudformation.model.DescribeStackEventsRequest)5 DescribeStackEventsResult (com.amazonaws.services.cloudformation.model.DescribeStackEventsResult)5 DescribeStacksResult (com.amazonaws.services.cloudformation.model.DescribeStacksResult)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 Stack (com.amazonaws.services.cloudformation.model.Stack)3 StackEvent (com.amazonaws.services.cloudformation.model.StackEvent)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Logger (org.slf4j.Logger)3 Component (org.springframework.stereotype.Component)3 AutoScalingGroup (com.amazonaws.services.autoscaling.model.AutoScalingGroup)2 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)2 AmazonCloudFormation (com.amazonaws.services.cloudformation.AmazonCloudFormation)2 ListStackResourcesRequest (com.amazonaws.services.cloudformation.model.ListStackResourcesRequest)2 ListStackResourcesResult (com.amazonaws.services.cloudformation.model.ListStackResourcesResult)2