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));
}
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;
}
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));
}
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));
}
Aggregations