Skip to content

Commit

Permalink
Merge pull request #171 from /issues/170
Browse files Browse the repository at this point in the history
fixes #170 and bump to 0.4.0 - emergency fix for SES unavailable region bug
  • Loading branch information
jantman committed Mar 15, 2016
2 parents ef26acf + 5f70246 commit b2127a6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog
Pre-release (develop branch)
----------------------------

0.4.1 (2016-03-15)
------------------

* `#170 <https://github.com/jantman/awslimitchecker/issues/170>`_ Critical bug fix in implementation of `#71 <https://github.com/jantman/awslimitchecker/issues/71>`_ - SES only supports three regions (us-east-1, us-west-2, eu-west-1) and causes an unhandled connection error if used in another region.

0.4.0 (2016-03-14)
------------------

Expand Down
17 changes: 13 additions & 4 deletions awslimitchecker/services/ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

from .base import _AwsService
from ..limit import AwsLimit
from botocore.exceptions import EndpointConnectionError

logger = logging.getLogger(__name__)

Expand All @@ -58,10 +59,14 @@ def find_usage(self):
:py:meth:`~.AwsLimit._add_current_usage`.
"""
logger.debug("Checking usage for service %s", self.service_name)
self.connect()
for lim in self.limits.values():
lim._reset_usage()
resp = self.conn.get_send_quota()
try:
self.connect()
resp = self.conn.get_send_quota()
except EndpointConnectionError as ex:
logger.warn('Skipping SES: %s', str(ex))
return
self.limits['Daily sending quota']._add_current_usage(
resp['SentLast24Hours']
)
Expand Down Expand Up @@ -95,8 +100,12 @@ def _update_limits_from_api(self):
Call the service's API action to retrieve limit/quota information, and
update AwsLimit objects in ``self.limits`` with this information.
"""
self.connect()
resp = self.conn.get_send_quota()
try:
self.connect()
resp = self.conn.get_send_quota()
except EndpointConnectionError as ex:
logger.warn('Skipping SES: %s', str(ex))
return
self.limits['Daily sending quota']._set_api_limit(resp['Max24HourSend'])

def required_iam_permissions(self):
Expand Down
48 changes: 48 additions & 0 deletions awslimitchecker/tests/services/test_ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import sys
from awslimitchecker.services.ses import _SesService
from botocore.exceptions import EndpointConnectionError

# https://code.google.com/p/mock/issues/detail?id=249
# py>=3.4 should use unittest.mock not the mock package on pypi
Expand Down Expand Up @@ -107,6 +108,31 @@ def test_find_usage(self):
assert cls.limits['Daily sending quota'].get_current_usage()[
0].get_value() == 122.0

def test_find_usage_invalid_region(self):
def se_get():
raise EndpointConnectionError(endpoint_url='myurl')

mock_conn = Mock()
mock_conn.get_send_quota.side_effect = se_get

with patch('%s.connect' % pb) as mock_connect:
with patch('%s.logger' % pbm) as mock_logger:
cls = _SesService(21, 43)
cls.conn = mock_conn
assert cls._have_usage is False
cls.find_usage()
assert mock_connect.mock_calls == [call()]
assert cls._have_usage is False
assert mock_logger.mock_calls == [
call.debug('Checking usage for service %s', 'SES'),
call.warn(
'Skipping SES: %s',
'Could not connect to the endpoint URL: "myurl"'
)
]
assert mock_conn.mock_calls == [call.get_send_quota()]
assert len(cls.limits['Daily sending quota'].get_current_usage()) == 0

def test_update_limits_from_api(self):
mock_conn = Mock()
mock_conn.get_send_quota.return_value = {
Expand All @@ -123,6 +149,28 @@ def test_update_limits_from_api(self):
assert mock_conn.mock_calls == [call.get_send_quota()]
assert cls.limits['Daily sending quota'].api_limit == 123.0

def test_update_limits_from_api_invalid_region(self):
def se_get():
raise EndpointConnectionError(endpoint_url='myurl')

mock_conn = Mock()
mock_conn.get_send_quota.side_effect = se_get

with patch('%s.connect' % pb) as mock_connect:
with patch('%s.logger' % pbm) as mock_logger:
cls = _SesService(21, 43)
cls.conn = mock_conn
cls._update_limits_from_api()
assert mock_connect.mock_calls == [call()]
assert mock_conn.mock_calls == [call.get_send_quota()]
assert mock_logger.mock_calls == [
call.warn(
'Skipping SES: %s',
'Could not connect to the endpoint URL: "myurl"'
)
]
assert cls.limits['Daily sending quota'].api_limit is None

def test_required_iam_permissions(self):
cls = _SesService(21, 43)
assert cls.required_iam_permissions() == [
Expand Down
2 changes: 1 addition & 1 deletion awslimitchecker/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import logging
logger = logging.getLogger(__name__)

_VERSION = '0.4.0'
_VERSION = '0.4.1'
_PROJECT_URL = 'https://github.com/jantman/awslimitchecker'


Expand Down

0 comments on commit b2127a6

Please sign in to comment.