Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#240 determine if provided region is a valid aws region #247

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions awslogs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def milis2iso(milis):

def boto3_client(aws_profile, aws_access_key_id, aws_secret_access_key, aws_session_token, aws_region):
core_session = botocore.session.get_session()
known_regions = core_session.get_available_regions('logs')
if aws_region is not None:
if aws_region not in known_regions:
print("WARNING: your selected region {REGION} is probably not valid".format(REGION=aws_region))
core_session.set_config_variable('profile', aws_profile)

credential_provider = core_session.get_component('credential_provider').get_provider('assume-role')
Expand Down Expand Up @@ -103,9 +107,9 @@ def list_logs(self):
streams = list(self._get_streams_from_pattern(self.log_group_name, self.log_stream_name))
if len(streams) > self.FILTER_LOG_EVENTS_STREAMS_LIMIT:
raise exceptions.TooManyStreamsFilteredError(
self.log_stream_name,
len(streams),
self.FILTER_LOG_EVENTS_STREAMS_LIMIT
self.log_stream_name,
len(streams),
self.FILTER_LOG_EVENTS_STREAMS_LIMIT
)
if len(streams) == 0:
raise exceptions.NoStreamsFilteredError(self.log_stream_name)
Expand Down
23 changes: 17 additions & 6 deletions tests/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,8 @@ def test_get_nogroup_nostream_short_forms(self, mock_stdout, botoclient):
def test_get_timestamp(self, mock_stdout, botoclient):
self.set_ABCDE_logs(botoclient)
exit_code = main("awslogs get "
"--timestamp --no-group --no-stream "
"AAA DDD --color=never".split())
"--timestamp --no-group --no-stream "
"AAA DDD --color=never".split())

self.assertEqual(
mock_stdout.getvalue(),
Expand All @@ -418,8 +418,8 @@ def test_get_timestamp(self, mock_stdout, botoclient):
def test_get_ingestion_time(self, mock_stdout, botoclient):
self.set_ABCDE_logs(botoclient)
exit_code = main("awslogs get "
"--ingestion-time --no-group --no-stream "
"AAA DDD --color=never".split())
"--ingestion-time --no-group --no-stream "
"AAA DDD --color=never".split())

self.assertEqual(
mock_stdout.getvalue(),
Expand All @@ -437,8 +437,8 @@ def test_get_ingestion_time(self, mock_stdout, botoclient):
def test_get_timestamp_and_ingestion_time(self, mock_stdout, botoclient):
self.set_ABCDE_logs(botoclient)
exit_code = main("awslogs get "
"--timestamp --ingestion-time --no-group --no-stream "
"AAA DDD --color=never".split())
"--timestamp --ingestion-time --no-group --no-stream "
"AAA DDD --color=never".split())

self.assertEqual(
mock_stdout.getvalue(),
Expand Down Expand Up @@ -630,3 +630,14 @@ def test_boto3_client_creation(self, mock_core_session):

awslogs = AWSLogs()
self.assertEqual(client, awslogs.client)

@patch('botocore.session.get_session')
def test_boto3_client_invalid_region(self, mock_core_session):
client = Mock()
boto_session = Mock()
mock_core_session.return_value = boto_session
boto_session.create_client.return_value = client
boto_session.aws_region.return_value = "Frankfurt"

awslogs = AWSLogs()
self.assertEqual(client, awslogs.client)