Skip to content

Commit

Permalink
add VPC 'Internet gateways' limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Jun 30, 2015
1 parent d3d749b commit 41d40c1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions awslimitchecker/services/vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ def find_usage(self):
aws_type='AWS::EC2::VPC',
id=vpc_id,
)

# Internet gateways
gws = self.conn.get_all_internet_gateways()
self.limits['Internet gateways']._add_current_usage(
len(gws),
aws_type='AWS::EC2::InternetGateway',
)
self._have_usage = True
logger.debug("Done checking usage.")

Expand Down Expand Up @@ -200,6 +207,15 @@ def get_limits(self):
limit_type='AWS::EC2::Route',
limit_subtype='AWS::EC2::RouteTable',
)

limits['Internet gateways'] = AwsLimit(
'Internet gateways',
self,
5,
self.warning_threshold,
self.critical_threshold,
limit_type='AWS::EC2::InternetGateway',
)
self.limits = limits
return limits

Expand Down
51 changes: 51 additions & 0 deletions awslimitchecker/tests/services/test_vpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
from boto.vpc.subnet import Subnet
from boto.vpc.networkacl import NetworkAcl
from boto.vpc.routetable import RouteTable
from boto.vpc.internetgateway import InternetGateway

from awslimitchecker.services.vpc import _VpcService

Expand Down Expand Up @@ -97,6 +98,7 @@ def test_get_limits(self):
res = cls.get_limits()
assert sorted(res.keys()) == sorted([
'Entries per route table',
'Internet gateways',
'VPCs',
'Subnets per VPC',
'Network ACLs per VPC',
Expand Down Expand Up @@ -126,12 +128,14 @@ def test_find_usage_vpc(self):
subnets = []
acls = []
tables = []
gateways = []

mock_conn = Mock(spec_set=VPCConnection)
mock_conn.get_all_vpcs.return_value = vpcs
mock_conn.get_all_subnets.return_value = subnets
mock_conn.get_all_network_acls.return_value = acls
mock_conn.get_all_route_tables.return_value = tables
mock_conn.get_all_internet_gateways.return_value = gateways

with patch('%s.connect' % self.pb) as mock_connect:
cls = _VpcService(21, 43)
Expand All @@ -147,6 +151,7 @@ def test_find_usage_vpc(self):
call.get_all_subnets(),
call.get_all_network_acls(),
call.get_all_route_tables(),
call.get_all_internet_gateways(),
]

def test_find_usage_subnets(self):
Expand All @@ -161,12 +166,14 @@ def test_find_usage_subnets(self):
subnets = [mock1, mock2, mock3]
acls = []
tables = []
gateways = []

mock_conn = Mock(spec_set=VPCConnection)
mock_conn.get_all_vpcs.return_value = vpcs
mock_conn.get_all_subnets.return_value = subnets
mock_conn.get_all_network_acls.return_value = acls
mock_conn.get_all_route_tables.return_value = tables
mock_conn.get_all_internet_gateways.return_value = gateways

with patch('%s.connect' % self.pb) as mock_connect:
cls = _VpcService(21, 43)
Expand All @@ -187,6 +194,7 @@ def test_find_usage_subnets(self):
call.get_all_subnets(),
call.get_all_network_acls(),
call.get_all_route_tables(),
call.get_all_internet_gateways(),
]

def test_find_usage_acls(self):
Expand All @@ -207,12 +215,14 @@ def test_find_usage_acls(self):
subnets = []
acls = [mock1, mock2, mock3]
tables = []
gateways = []

mock_conn = Mock(spec_set=VPCConnection)
mock_conn.get_all_vpcs.return_value = vpcs
mock_conn.get_all_subnets.return_value = subnets
mock_conn.get_all_network_acls.return_value = acls
mock_conn.get_all_route_tables.return_value = tables
mock_conn.get_all_internet_gateways.return_value = gateways

with patch('%s.connect' % self.pb) as mock_connect:
cls = _VpcService(21, 43)
Expand Down Expand Up @@ -241,6 +251,7 @@ def test_find_usage_acls(self):
call.get_all_subnets(),
call.get_all_network_acls(),
call.get_all_route_tables(),
call.get_all_internet_gateways(),
]

def test_find_usage_route_tables(self):
Expand All @@ -261,12 +272,14 @@ def test_find_usage_route_tables(self):
subnets = []
acls = []
tables = [mock1, mock2, mock3]
gateways = []

mock_conn = Mock(spec_set=VPCConnection)
mock_conn.get_all_vpcs.return_value = vpcs
mock_conn.get_all_subnets.return_value = subnets
mock_conn.get_all_network_acls.return_value = acls
mock_conn.get_all_route_tables.return_value = tables
mock_conn.get_all_internet_gateways.return_value = gateways

with patch('%s.connect' % self.pb) as mock_connect:
cls = _VpcService(21, 43)
Expand Down Expand Up @@ -295,6 +308,44 @@ def test_find_usage_route_tables(self):
call.get_all_subnets(),
call.get_all_network_acls(),
call.get_all_route_tables(),
call.get_all_internet_gateways(),
]

def test_find_usage_internet_gateways(self):
mock1 = Mock(spec_set=InternetGateway)
type(mock1).id = 'gw-1'
mock2 = Mock(spec_set=InternetGateway)
type(mock2).id = 'gw-2'

vpcs = []
subnets = []
acls = []
tables = []
gateways = [mock1, mock2]

mock_conn = Mock(spec_set=VPCConnection)
mock_conn.get_all_vpcs.return_value = vpcs
mock_conn.get_all_subnets.return_value = subnets
mock_conn.get_all_network_acls.return_value = acls
mock_conn.get_all_route_tables.return_value = tables
mock_conn.get_all_internet_gateways.return_value = gateways

with patch('%s.connect' % self.pb) as mock_connect:
cls = _VpcService(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 True
assert len(cls.limits['Internet gateways'].get_current_usage()) == 1
assert cls.limits['Internet gateways'].get_current_usage()[
0].get_value() == 2
assert mock_conn.mock_calls == [
call.get_all_vpcs(),
call.get_all_subnets(),
call.get_all_network_acls(),
call.get_all_route_tables(),
call.get_all_internet_gateways(),
]

def test_required_iam_permissions(self):
Expand Down

0 comments on commit 41d40c1

Please sign in to comment.