Skip to content

Commit

Permalink
Add UnknownRegionError and additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Oct 25, 2021
1 parent 174b849 commit 92c3f10
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
10 changes: 10 additions & 0 deletions botocore/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ class UnknownServiceError(DataNotFoundError):
"Unknown service: '{service_name}'. Valid service names are: "
"{known_service_names}")

class UnknownRegionError(BotoCoreError):
"""Raised when trying to load data for an unknown region.
:ivar region_name: The name of the unknown region.
"""
fmt = (
"Unknown region: '{region_name}'. {error_msg}"
)


class ApiVersionNotFoundError(BotoCoreError):
"""
Expand Down
23 changes: 14 additions & 9 deletions botocore/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import logging
import re

from botocore.exceptions import NoRegionError
from botocore.exceptions import (
NoRegionError, UnknownRegionError
)

LOG = logging.getLogger(__name__)
DEFAULT_URI_TEMPLATE = '{service}.{region}.{dnsSuffix}'
Expand Down Expand Up @@ -135,6 +137,13 @@ def construct_endpoint(self, service_name, region_name=None, partition_name=None
return result
return None

# Iterate over each partition until a match is found.
for partition in self._endpoint_data['partitions']:
result = self._endpoint_for_partition(
partition, service_name, region_name)
if result:
return result

def get_partition_for_region(self, region_name):
for partition in self._endpoint_data['partitions']:
if region_name in partition['regions']:
Expand All @@ -143,14 +152,10 @@ def get_partition_for_region(self, region_name):
continue
if re.compile(partition['regionRegex']).match(region_name):
return partition['partition']
raise ValueError('Invalid region name: {0}'.format(region_name))

# Iterate over each partition until a match is found.
for partition in self._endpoint_data['partitions']:
result = self._endpoint_for_partition(
partition, service_name, region_name)
if result:
return result
raise UnknownRegionError(
region_name=region_name,
error_msg='No partition found for provided region_name.'
)

def _endpoint_for_partition(
self, partition, service_name, region_name, force_partition=False
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,19 @@ def test_provides_correct_partition_for_region(self):
partition = self.session.get_partition_for_region('us-west-2')
self.assertEqual(partition, 'aws')

def test_provides_correct_partition_for_region_regex(self):
partition = self.session.get_partition_for_region('af-south-99')
self.assertEqual(partition, 'aws')

def test_provides_correct_partition_for_region_non_default(self):
partition = self.session.get_partition_for_region('cn-north-1')
self.assertEqual(partition, 'aws-cn')

def test_raises_exception_for_invalid_region(self):
with self.assertRaises(ValueError):
with self.assertRaises(botocore.exceptions.UnknownRegionError):
self.session.get_partition_for_region('no-good-1')


class TestSessionUserAgent(BaseSessionTest):
def test_can_change_user_agent_name(self):
self.session.user_agent_name = 'something-else'
Expand Down

0 comments on commit 92c3f10

Please sign in to comment.