Search in sources :

Example 1 with AwsDefaultRegionSelectionFailed

use of com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed in project cloudbreak by hortonworks.

the class AwsDefaultRegionSelectorTest method testDetermineDefaultRegionWhenGlobalDefaultRegionDescribeFailsWithAuthExceptionAndNoAdditionalRegionsAreConfigured.

@Test
void testDetermineDefaultRegionWhenGlobalDefaultRegionDescribeFailsWithAuthExceptionAndNoAdditionalRegionsAreConfigured() {
    AmazonEC2Exception amazonEC2Exception = new AmazonEC2Exception("SomethingBadHappened");
    amazonEC2Exception.setErrorCode(EC2_AUTH_FAILURE_ERROR_CODE);
    when(ec2Client.describeRegions(any(DescribeRegionsRequest.class))).thenThrow(amazonEC2Exception);
    when(awsClient.createAccessWithMinimalRetries(any(AwsCredentialView.class), eq(GLOBAL_DEFAULT_ZONE))).thenReturn(ec2Client);
    when(platformResources.getEnabledRegions()).thenReturn(null);
    AwsDefaultRegionSelectionFailed ex = assertThrows(AwsDefaultRegionSelectionFailed.class, () -> underTest.determineDefaultRegion(new CloudCredential()));
    assertEquals(String.format("Failed to describe available EC2 regions in region '%s'", GLOBAL_DEFAULT_ZONE), ex.getMessage());
    verify(awsClient, times(1)).createAccessWithMinimalRetries(any(AwsCredentialView.class), eq(GLOBAL_DEFAULT_ZONE));
    verify(ec2Client, times(1)).describeRegions(any(DescribeRegionsRequest.class));
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) AwsDefaultRegionSelectionFailed(com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) Test(org.junit.jupiter.api.Test)

Example 2 with AwsDefaultRegionSelectionFailed

use of com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed in project cloudbreak by hortonworks.

the class AwsDefaultRegionSelector method determineDefaultRegion.

public String determineDefaultRegion(CloudCredential cloudCredential) {
    String result = null;
    AwsCredentialView awsCredential = new AwsCredentialView(cloudCredential);
    String originalDefaultRegion = defaultZoneProvider.getDefaultZone(cloudCredential);
    Set<Region> enabledRegions = platformResources.getEnabledRegions();
    LOGGER.debug("Try to describe regions by using the global default region '{}' in EC2.", originalDefaultRegion);
    boolean globalDefaultRegionViable = describeRegionsViaEc2Region(awsCredential, originalDefaultRegion);
    if (!globalDefaultRegionViable && CollectionUtils.isNotEmpty(enabledRegions)) {
        LOGGER.info("Regions could not be described by using the global default region '{}' in EC2. Starting to describe regions with other regions", originalDefaultRegion);
        String regionSelected = enabledRegions.stream().filter(r -> describeRegionsViaEc2Region(awsCredential, r.getRegionName())).findFirst().orElseThrow(() -> {
            List<String> usedRegions = enabledRegions.stream().map(Region::getRegionName).collect(Collectors.toList());
            usedRegions.add(originalDefaultRegion);
            String regions = String.join(",", usedRegions);
            String msg = String.format("Failed to describe available EC2 regions by configuring SDK to use the following regions: '%s'", regions);
            LOGGER.warn(msg);
            return new AwsDefaultRegionSelectionFailed(msg);
        }).getRegionName();
        if (!originalDefaultRegion.equals(regionSelected)) {
            LOGGER.info("The default region for credential needs to be changed from '{}' to '{}'", originalDefaultRegion, regionSelected);
            result = regionSelected;
        }
    } else if (!globalDefaultRegionViable) {
        String msg = String.format("Failed to describe available EC2 regions in region '%s'", originalDefaultRegion);
        LOGGER.warn(msg);
        throw new AwsDefaultRegionSelectionFailed(msg);
    }
    return result;
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) Logger(org.slf4j.Logger) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) Region(com.sequenceiq.cloudbreak.cloud.model.Region) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Collectors(java.util.stream.Collectors) CollectionUtils(org.apache.commons.collections4.CollectionUtils) Inject(javax.inject.Inject) List(java.util.List) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) Service(org.springframework.stereotype.Service) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) AwsDefaultRegionSelectionFailed(com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed) AwsDefaultRegionSelectionFailed(com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed) Region(com.sequenceiq.cloudbreak.cloud.model.Region) List(java.util.List)

Example 3 with AwsDefaultRegionSelectionFailed

use of com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed in project cloudbreak by hortonworks.

the class EnvironmentServiceIntegrationTest method testCredentialCreateAwsDefaultRegionExceptionMappedToForbidden.

@Test
public void testCredentialCreateAwsDefaultRegionExceptionMappedToForbidden() throws InterruptedException {
    credentialRequest.setAws(getAwsKeyBasedCredentialParameters(false, "yyy", "zzzz"));
    credentialRequest.setCloudPlatform("AWS");
    credentialRequest.setName("testcredential");
    when(requestProvider.getResourceDefinitionRequest(any(), any())).thenReturn(resourceDefinitionRequest);
    when(requestProvider.getCredentialVerificationRequest(any(), any(), anyBoolean())).thenAnswer(invocation -> new CredentialVerificationRequest(invocation.getArgument(0), invocation.getArgument(1)) {

        @Override
        public CredentialVerificationResult await() {
            throw new AwsDefaultRegionSelectionFailed("this is an exceptional exception");
        }
    });
    when(resourceDefinitionRequest.await()).thenReturn(new ResourceDefinitionResult(1L, DEFINITION_AWS));
    Assertions.assertThrows(ForbiddenException.class, () -> client.credentialV1Endpoint().post(credentialRequest));
}
Also used : CredentialVerificationRequest(com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationRequest) CredentialVerificationResult(com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationResult) AwsDefaultRegionSelectionFailed(com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed) ResourceDefinitionResult(com.sequenceiq.cloudbreak.cloud.event.platform.ResourceDefinitionResult) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with AwsDefaultRegionSelectionFailed

use of com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed in project cloudbreak by hortonworks.

the class AwsDefaultRegionSelectorTest method testDetermineDefaultRegionWhenGlobalDefaultRegionIsNotViableAndNoneOfTheAdditionalRegionsIsViable.

@Test
void testDetermineDefaultRegionWhenGlobalDefaultRegionIsNotViableAndNoneOfTheAdditionalRegionsIsViable() {
    when(awsClient.createAccessWithMinimalRetries(any(AwsCredentialView.class), any())).thenReturn(ec2Client);
    AmazonEC2Exception amazonEC2Exception = new AmazonEC2Exception("SomethingBadHappened");
    amazonEC2Exception.setErrorCode(EC2_AUTH_FAILURE_ERROR_CODE);
    when(ec2Client.describeRegions(any(DescribeRegionsRequest.class))).thenThrow(amazonEC2Exception);
    AwsDefaultRegionSelectionFailed exception = assertThrows(AwsDefaultRegionSelectionFailed.class, () -> underTest.determineDefaultRegion(new CloudCredential()));
    assertTrue(exception.getMessage().startsWith("Failed to describe available EC2 regions by configuring SDK to use the following regions:"));
    verify(awsClient, times(4)).createAccessWithMinimalRetries(any(AwsCredentialView.class), any());
    verify(ec2Client, times(4)).describeRegions(any(DescribeRegionsRequest.class));
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) AwsDefaultRegionSelectionFailed(com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed) DescribeRegionsRequest(com.amazonaws.services.ec2.model.DescribeRegionsRequest) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) Test(org.junit.jupiter.api.Test)

Aggregations

AwsDefaultRegionSelectionFailed (com.sequenceiq.cloudbreak.cloud.aws.common.exception.AwsDefaultRegionSelectionFailed)4 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)3 DescribeRegionsRequest (com.amazonaws.services.ec2.model.DescribeRegionsRequest)3 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)3 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)3 Test (org.junit.jupiter.api.Test)3 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)1 CredentialVerificationRequest (com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationRequest)1 CredentialVerificationResult (com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationResult)1 ResourceDefinitionResult (com.sequenceiq.cloudbreak.cloud.event.platform.ResourceDefinitionResult)1 Region (com.sequenceiq.cloudbreak.cloud.model.Region)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 Service (org.springframework.stereotype.Service)1