Search in sources :

Example 1 with StackResource

use of com.amazonaws.services.cloudformation.model.StackResource 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 StackResource

use of com.amazonaws.services.cloudformation.model.StackResource 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 3 with StackResource

use of com.amazonaws.services.cloudformation.model.StackResource 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 4 with StackResource

use of com.amazonaws.services.cloudformation.model.StackResource in project cloudbreak by hortonworks.

the class AwsCloudFormationErrorMessageProviderTest method shouldDecodeAuthorizationError.

@Test
void shouldDecodeAuthorizationError() {
    when(cfRetryClient.describeStacks(any())).thenReturn(new DescribeStacksResult().withStacks(new Stack().withStackStatusReason("The following resource(s) failed to create: [ClusterNodeSecurityGroupmaster0].")));
    String resourceStatusReason = "Resource creation cancelled";
    when(cfRetryClient.describeStackResources(any())).thenReturn(new DescribeStackResourcesResult().withStackResources(new StackResource().withLogicalResourceId("ClusterNodeSecurityGroupmaster0").withResourceStatus(ResourceStatus.CREATE_FAILED.toString()).withResourceStatusReason(resourceStatusReason)));
    when(awsEncodedAuthorizationFailureMessageDecoder.decodeAuthorizationFailureMessageIfNeeded(any(), eq(REGION), anyString())).thenReturn("Decoded auth error");
    when(cfRetryClient.describeStackEvents(any(DescribeStackEventsRequest.class))).thenReturn(new DescribeStackEventsResult());
    String result = underTest.getErrorReason(credentialView, REGION, STACK_NAME, ResourceStatus.CREATE_FAILED);
    assertEquals("The following resource(s) failed to create: [ClusterNodeSecurityGroupmaster0]. " + "ClusterNodeSecurityGroupmaster0: Decoded auth error", result);
    verify(awsEncodedAuthorizationFailureMessageDecoder).decodeAuthorizationFailureMessageIfNeeded(credentialView, REGION, resourceStatusReason);
}
Also used : DescribeStackEventsResult(com.amazonaws.services.cloudformation.model.DescribeStackEventsResult) 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) Stack(com.amazonaws.services.cloudformation.model.Stack) StackResource(com.amazonaws.services.cloudformation.model.StackResource) Test(org.junit.jupiter.api.Test)

Example 5 with StackResource

use of com.amazonaws.services.cloudformation.model.StackResource in project cloudbreak by hortonworks.

the class AwsCloudFormationErrorMessageProvider method getStackResourceStatusReasons.

private String getStackResourceStatusReasons(AwsCredentialView credentialView, String region, String stackName, Set<String> resourceErrorStatuses, AmazonCloudFormationClient cfClient) {
    DescribeStackResourcesRequest describeStackResourcesRequest = new DescribeStackResourcesRequest().withStackName(stackName);
    DescribeStackResourcesResult describeStackResourcesResult = cfClient.describeStackResources(describeStackResourcesRequest);
    String stackResourceStatusReasons = describeStackResourcesResult.getStackResources().stream().filter(stackResource -> resourceErrorStatuses.contains(stackResource.getResourceStatus())).map(stackResource -> getStackResourceMessage(credentialView, region, stackResource)).collect(Collectors.joining(", "));
    return stackResourceStatusReasons;
}
Also used : Arrays(java.util.Arrays) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) LoggerFactory(org.slf4j.LoggerFactory) StackResource(com.amazonaws.services.cloudformation.model.StackResource) DescribeStacksResult(com.amazonaws.services.cloudformation.model.DescribeStacksResult) Inject(javax.inject.Inject) StackEvent(com.amazonaws.services.cloudformation.model.StackEvent) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeStackEventsResult(com.amazonaws.services.cloudformation.model.DescribeStackEventsResult) AwsEncodedAuthorizationFailureMessageDecoder(com.sequenceiq.cloudbreak.cloud.aws.common.util.AwsEncodedAuthorizationFailureMessageDecoder) Logger(org.slf4j.Logger) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) Set(java.util.Set) ResourceStatus(com.amazonaws.services.cloudformation.model.ResourceStatus) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Component(org.springframework.stereotype.Component) Stream(java.util.stream.Stream) CollectionUtils(org.springframework.util.CollectionUtils) DescribeStackEventsRequest(com.amazonaws.services.cloudformation.model.DescribeStackEventsRequest) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) StringUtils(org.springframework.util.StringUtils) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest)

Aggregations

DescribeStackResourcesResult (com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult)11 StackResource (com.amazonaws.services.cloudformation.model.StackResource)11 Test (org.junit.jupiter.api.Test)7 DescribeStackResourcesRequest (com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest)6 DescribeStackEventsRequest (com.amazonaws.services.cloudformation.model.DescribeStackEventsRequest)3 DescribeStackEventsResult (com.amazonaws.services.cloudformation.model.DescribeStackEventsResult)3 DescribeStacksResult (com.amazonaws.services.cloudformation.model.DescribeStacksResult)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 Stack (com.amazonaws.services.cloudformation.model.Stack)2 StackResourceSummary (com.amazonaws.services.cloudformation.model.StackResourceSummary)2 AwsCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient)2