Skip to content

Commit

Permalink
Merge pull request #1715 from jkinred/develop
Browse files Browse the repository at this point in the history
Find the partition of a region
  • Loading branch information
nateprewitt authored Oct 27, 2021
2 parents 61c531c + 2c4c368 commit 383cb3f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/next-release/enhancement-Session-66470.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "enhancement",
"category": "Session",
"description": "Added `get_partition_for_region` allowing partition lookup by region name."
}
11 changes: 11 additions & 0 deletions botocore/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ class UnknownServiceError(DataNotFoundError):
"{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):
"""
The data associated with either the API version or a compatible one
Expand Down
13 changes: 12 additions & 1 deletion 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 @@ -142,6 +144,15 @@ def construct_endpoint(self, service_name, region_name=None, partition_name=None
if result:
return result

def get_partition_for_region(self, region_name):
for partition in self._endpoint_data['partitions']:
if self._region_match(partition, region_name):
return partition['partition']
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
13 changes: 13 additions & 0 deletions botocore/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,19 @@ def get_available_partitions(self):
resolver = self._get_internal_component('endpoint_resolver')
return resolver.get_available_partitions()

def get_partition_for_region(self, region_name):
"""Lists the partition name of a particular region.
:type region_name: string
:param region_name: Name of the region to list partition for (e.g.,
us-east-1).
:rtype: string
:return: Returns the respective partition name (e.g., aws).
"""
resolver = self._get_internal_component('endpoint_resolver')
return resolver.get_partition_for_region(region_name)

def get_available_regions(self, service_name, partition_name='aws',
allow_non_regional=False):
"""Lists the region and endpoint names of a particular partition.
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,22 @@ def test_provides_empty_list_for_unknown_service_regions(self):
regions = self.session.get_available_regions('__foo__')
self.assertEqual([], regions)

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(botocore.exceptions.UnknownRegionError):
self.session.get_partition_for_region('no-good-1')


class TestSessionUserAgent(BaseSessionTest):
def test_can_change_user_agent_name(self):
Expand Down

0 comments on commit 383cb3f

Please sign in to comment.