class 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;
    }

}