From 9493202ef22a7b311be03834f741a9dc167ffd20 Mon Sep 17 00:00:00 2001 From: Phil Christensen Date: Sun, 9 Aug 2015 11:29:22 -0400 Subject: [PATCH 1/3] Added try/except and comment to fix limit checker when in an account that doesn't support EC2-Classic --- awslimitchecker/services/elasticache.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/awslimitchecker/services/elasticache.py b/awslimitchecker/services/elasticache.py index 634f7586..bc3118a5 100644 --- a/awslimitchecker/services/elasticache.py +++ b/awslimitchecker/services/elasticache.py @@ -39,6 +39,7 @@ import abc # noqa from boto.elasticache.layer1 import ElastiCacheConnection +from boto.exception import BotoServerError import logging from .base import _AwsService @@ -121,10 +122,21 @@ def _find_usage_parameter_groups(self): def _find_usage_security_groups(self): """find usage for elasticache security groups""" - groups = self.conn.describe_cache_security_groups()[ - 'DescribeCacheSecurityGroupsResponse'][ - 'DescribeCacheSecurityGroupsResult'][ - 'CacheSecurityGroups'] + + try: + # If EC2-Classic isn't available (e.g., a new account) + # this method will fail with: + # Code: "InvalidParameterValue" + # Message: "Use of cache security groups is not permitted in + # this API version for your account." + # Type: "Sender" + groups = self.conn.describe_cache_security_groups()[ + 'DescribeCacheSecurityGroupsResponse'][ + 'DescribeCacheSecurityGroupsResult'][ + 'CacheSecurityGroups'] + except BotoServerError, e: + groups = [] + self.limits['Security Groups']._add_current_usage( len(groups), aws_type='WS::ElastiCache::SecurityGroup' From 5e4a0340ee8d47e6f4887e64bc8c859b64cf50d1 Mon Sep 17 00:00:00 2001 From: Jason Antman Date: Wed, 12 Aug 2015 17:31:09 -0400 Subject: [PATCH 2/3] Clean up flake8 and add logging for PR #52 --- awslimitchecker/services/elasticache.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/awslimitchecker/services/elasticache.py b/awslimitchecker/services/elasticache.py index bc3118a5..1e7807b4 100644 --- a/awslimitchecker/services/elasticache.py +++ b/awslimitchecker/services/elasticache.py @@ -122,7 +122,6 @@ def _find_usage_parameter_groups(self): def _find_usage_security_groups(self): """find usage for elasticache security groups""" - try: # If EC2-Classic isn't available (e.g., a new account) # this method will fail with: @@ -134,9 +133,11 @@ def _find_usage_security_groups(self): 'DescribeCacheSecurityGroupsResponse'][ 'DescribeCacheSecurityGroupsResult'][ 'CacheSecurityGroups'] - except BotoServerError, e: + except BotoServerError: + logger.debug("caught BotoServerError checking ElastiCache security " + "groups (account without EC2-Classic?)") groups = [] - + self.limits['Security Groups']._add_current_usage( len(groups), aws_type='WS::ElastiCache::SecurityGroup' From f780001f754f9b8bf29213e522ed4b1d507af826 Mon Sep 17 00:00:00 2001 From: Jason Antman Date: Wed, 12 Aug 2015 17:31:19 -0400 Subject: [PATCH 3/3] Add tests for PR #52 --- .../tests/services/test_elasticache.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/awslimitchecker/tests/services/test_elasticache.py b/awslimitchecker/tests/services/test_elasticache.py index 2a6f373b..218b57de 100644 --- a/awslimitchecker/tests/services/test_elasticache.py +++ b/awslimitchecker/tests/services/test_elasticache.py @@ -38,8 +38,8 @@ """ import sys -# TODO confirm this is the correct import from boto.elasticache.layer1 import ElastiCacheConnection +from boto.exception import BotoServerError from awslimitchecker.services.elasticache import _ElastiCacheService # https://code.google.com/p/mock/issues/detail?id=249 @@ -469,6 +469,25 @@ def test_find_usage_security_groups(self): assert len(usage) == 1 assert usage[0].get_value() == 2 + def test_find_usage_security_groups_exception(self): + """test find usage for security groups""" + def se_exc(): + raise BotoServerError(None, None, None) + + mock_conn = Mock(spec_set=ElastiCacheConnection) + mock_conn.describe_cache_security_groups.side_effect = se_exc + cls = _ElastiCacheService(21, 43) + cls.conn = mock_conn + cls._find_usage_security_groups() + + assert mock_conn.mock_calls == [ + call.describe_cache_security_groups(), + ] + + usage = cls.limits['Security Groups'].get_current_usage() + assert len(usage) == 1 + assert usage[0].get_value() == 0 + def test_required_iam_permissions(self): cls = _ElastiCacheService(21, 43) assert cls.required_iam_permissions() == [