Search in sources :

Example 6 with DescribeStackResourcesResult

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

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

Example 8 with DescribeStackResourcesResult

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

the class AwsMigrationUtil method allInstancesDeletedFromCloudFormation.

public boolean allInstancesDeletedFromCloudFormation(AuthenticatedContext ac, CloudResource cloudResource) {
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AwsCredentialView awsCredential = new AwsCredentialView(ac.getCloudCredential());
    DescribeStackResourcesResult describeStackResourcesResult = awsClient.createCloudFormationClient(awsCredential, regionName).describeStackResources(new DescribeStackResourcesRequest().withStackName(cloudResource.getName()));
    List<StackResource> asGroups = describeStackResourcesResult.getStackResources().stream().filter(it -> "AWS::AutoScaling::AutoScalingGroup".equals(it.getResourceType())).collect(Collectors.toList());
    LOGGER.debug("AutoScalingGroup fetched: {}", asGroups);
    boolean empty = true;
    int i = 0;
    while (empty && i < asGroups.size()) {
        StackResource asGroup = asGroups.get(i);
        List<String> result = cfStackUtil.getInstanceIds(awsClient.createAutoScalingClient(awsCredential, regionName), asGroup.getPhysicalResourceId());
        LOGGER.debug("{} autoScalingGroup has {} instance(s): {}", asGroup.getPhysicalResourceId(), result.size(), result);
        empty = result.isEmpty();
        i++;
    }
    return empty;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) Logger(org.slf4j.Logger) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) AwsCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.AwsCloudFormationClient) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudFormationStackUtil(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationStackUtil) Collectors(java.util.stream.Collectors) StackResource(com.amazonaws.services.cloudformation.model.StackResource) Inject(javax.inject.Inject) List(java.util.List) Component(org.springframework.stereotype.Component) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) DescribeStackResourcesRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourcesRequest) StackResource(com.amazonaws.services.cloudformation.model.StackResource)

Example 9 with DescribeStackResourcesResult

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

the class AwsMigrationUtilTest method testAllInstancesDeletedFromCloudFormationWhenASGroupFoundFirstASGHasInstance.

@Test
public void testAllInstancesDeletedFromCloudFormationWhenASGroupFoundFirstASGHasInstance() {
    StackResource asg1 = new StackResource().withResourceType("AWS::AutoScaling::AutoScalingGroup").withPhysicalResourceId("id1");
    StackResource asg2 = new StackResource().withResourceType("AWS::AutoScaling::AutoScalingGroup").withPhysicalResourceId("id2");
    when(ac.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region("region")));
    when(awsClient.createCloudFormationClient(any(), any())).thenReturn(amazonCloudFormationClient);
    when(amazonCloudFormationClient.describeStackResources(any())).thenReturn(new DescribeStackResourcesResult().withStackResources(List.of(asg1, asg2)));
    when(awsClient.createAutoScalingClient(any(), any())).thenReturn(amazonAutoScalingClient);
    when(cfStackUtil.getInstanceIds(amazonAutoScalingClient, "id1")).thenReturn(List.of("instanceId1"));
    boolean actual = underTest.allInstancesDeletedFromCloudFormation(ac, cloudResource);
    Assertions.assertFalse(actual);
    verify(cfStackUtil).getInstanceIds(amazonAutoScalingClient, "id1");
    verify(cfStackUtil, never()).getInstanceIds(amazonAutoScalingClient, "id2");
}
Also used : DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) StackResource(com.amazonaws.services.cloudformation.model.StackResource) Test(org.junit.jupiter.api.Test)

Example 10 with DescribeStackResourcesResult

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

the class AwsMigrationUtilTest method testAllInstancesDeletedFromCloudFormationWhenASGroupFoundOSecondASGHasInstance.

@Test
public void testAllInstancesDeletedFromCloudFormationWhenASGroupFoundOSecondASGHasInstance() {
    StackResource asg1 = new StackResource().withResourceType("AWS::AutoScaling::AutoScalingGroup").withPhysicalResourceId("id1");
    StackResource asg2 = new StackResource().withResourceType("AWS::AutoScaling::AutoScalingGroup").withPhysicalResourceId("id2");
    when(ac.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(Location.location(Region.region("region")));
    when(awsClient.createCloudFormationClient(any(), any())).thenReturn(amazonCloudFormationClient);
    when(amazonCloudFormationClient.describeStackResources(any())).thenReturn(new DescribeStackResourcesResult().withStackResources(List.of(asg1, asg2)));
    when(awsClient.createAutoScalingClient(any(), any())).thenReturn(amazonAutoScalingClient);
    when(cfStackUtil.getInstanceIds(amazonAutoScalingClient, "id1")).thenReturn(Collections.emptyList());
    when(cfStackUtil.getInstanceIds(amazonAutoScalingClient, "id2")).thenReturn(List.of("instanceId1"));
    boolean actual = underTest.allInstancesDeletedFromCloudFormation(ac, cloudResource);
    Assertions.assertFalse(actual);
    verify(cfStackUtil).getInstanceIds(amazonAutoScalingClient, "id1");
    verify(cfStackUtil).getInstanceIds(amazonAutoScalingClient, "id2");
}
Also used : DescribeStackResourcesResult(com.amazonaws.services.cloudformation.model.DescribeStackResourcesResult) StackResource(com.amazonaws.services.cloudformation.model.StackResource) 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