@Test void testDetermineDefaultRegionWhenGlobalDefaultRegionIsNotViableAndOneOfTheAdditionalRegionsIsViable() { AmazonEC2Exception amazonEC2Exception = new AmazonEC2Exception("SomethingBadHappened"); amazonEC2Exception.setErrorCode(EC2_AUTH_FAILURE_ERROR_CODE); when(ec2Client.describeRegions(any(DescribeRegionsRequest.class))).thenReturn(new DescribeRegionsResult()); when(awsClient.createAccessWithMinimalRetries(any(AwsCredentialView.class), any())) .thenThrow(amazonEC2Exception) .thenThrow(amazonEC2Exception) .thenThrow(amazonEC2Exception) .thenReturn(ec2Client); String actual = underTest.determineDefaultRegion(new CloudCredential()); final ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(awsClient, times(4)).createAccessWithMinimalRetries(any(AwsCredentialView.class), captor.capture()); final String expected = captor.getValue(); assertEquals(expected, actual); verify(ec2Client, times(1)).describeRegions(any(DescribeRegionsRequest.class)); }
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; }
AwsDefaultRegionSelector { 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; } }
AwsDefaultRegionSelector { 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; }  }
AwsDefaultRegionSelector { 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; }  String determineDefaultRegion(CloudCredential cloudCredential); }
AwsDefaultRegionSelector { 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; }  String determineDefaultRegion(CloudCredential cloudCredential);  }