Skip to content

Commit

Permalink
fixes #1 - AwsLimit should take a reference to an _AwsService class i…
Browse files Browse the repository at this point in the history
…nstead of a service name string
  • Loading branch information
jantman committed Jun 13, 2015
1 parent ac94090 commit 7d327eb
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
11 changes: 5 additions & 6 deletions awslimitchecker/limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

class AwsLimit(object):

def __init__(self, name, service_name, default_limit,
def __init__(self, name, service, default_limit,
def_warning_threshold, def_critical_threshold,
limit_type=None, limit_subtype=None):
"""
Expand All @@ -51,10 +51,9 @@ def __init__(self, name, service_name, default_limit,
:param name: the name of this limit (may contain spaces);
if possible, this should be the name used by AWS, i.e. TrustedAdvisor
:type name: string
:param service_name: the name of the service this limit is for;
this should be the ``service_name`` attribute of an
:py:class:`~._AwsService` class.
:type service_name: string
:param service: the :py:class:`~._AwsService` class that
this limit is for
:type service_name: :py:class:`~._AwsService`
:param default_limit: the default value of this limit for new accounts
:type default_limit: int
:param def_warning_threshold: the default warning threshold, as an
Expand All @@ -75,7 +74,7 @@ def __init__(self, name, service_name, default_limit,
raise ValueError("critical threshold must be greater than warning "
"threshold")
self.name = name
self.service_name = service_name
self.service = service
self.default_limit = default_limit
self.limit_type = limit_type
self.limit_subtype = limit_subtype
Expand Down
22 changes: 11 additions & 11 deletions awslimitchecker/services/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def _get_limits_ebs(self):
limits = {}
limits['Provisioned IOPS'] = AwsLimit(
'Provisioned IOPS',
self.service_name,
self,
40000,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -247,7 +247,7 @@ def _get_limits_ebs(self):
)
limits['Provisioned IOPS (SSD) volume storage (TiB)'] = AwsLimit(
'Provisioned IOPS (SSD) volume storage (TiB)',
self.service_name,
self,
20,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -256,7 +256,7 @@ def _get_limits_ebs(self):
)
limits['General Purpose (SSD) volume storage (TiB)'] = AwsLimit(
'General Purpose (SSD) volume storage (TiB)',
self.service_name,
self,
20,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -265,7 +265,7 @@ def _get_limits_ebs(self):
)
limits['Magnetic volume storage (TiB)'] = AwsLimit(
'Magnetic volume storage (TiB)',
self.service_name,
self,
20,
self.warning_threshold,
self.critical_threshold,
Expand Down Expand Up @@ -312,7 +312,7 @@ def _get_limits_instances(self):
lim = special_limits[i_type][0]
limits[key] = AwsLimit(
key,
self.service_name,
self,
lim,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -323,7 +323,7 @@ def _get_limits_instances(self):
key = 'Running On-Demand EC2 instances'
limits[key] = AwsLimit(
key,
self.service_name,
self,
default_limits[0],
self.warning_threshold,
self.critical_threshold,
Expand Down Expand Up @@ -388,7 +388,7 @@ def _get_limits_networking(self):
limits = {}
limits['Security groups per VPC'] = AwsLimit(
'Security groups per VPC',
self.service_name,
self,
100,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -397,7 +397,7 @@ def _get_limits_networking(self):
)
limits['Rules per VPC security group'] = AwsLimit(
'Rules per VPC security group',
self.service_name,
self,
50,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -407,7 +407,7 @@ def _get_limits_networking(self):
# self.conn.get_all_addresses - domain == 'vpc'
limits['EC2-VPC Elastic IPs'] = AwsLimit(
'EC2-VPC Elastic IPs',
self.service_name,
self,
5,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -417,7 +417,7 @@ def _get_limits_networking(self):
# self.conn.get_all_addresses - domain == 'standard'
limits['EC2-Classic Elastic IPs'] = AwsLimit(
'EC2-Classic Elastic IPs',
self.service_name,
self,
5,
self.warning_threshold,
self.critical_threshold,
Expand All @@ -426,7 +426,7 @@ def _get_limits_networking(self):
# self.conn.get_all_network_interfaces()
limits['VPC security groups per elastic network interface'] = AwsLimit(
'VPC security groups per elastic network interface',
self.service_name,
self,
5,
self.warning_threshold,
self.critical_threshold,
Expand Down
2 changes: 1 addition & 1 deletion awslimitchecker/tests/services/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_get_limits_all(self):
for x in limits:
assert isinstance(limits[x], AwsLimit)
assert x == limits[x].name
assert limits[x].service_name == 'EC2'
assert limits[x].service == cls

def test_get_limits_ebs(self):
cls = _Ec2Service(21, 43)
Expand Down
56 changes: 30 additions & 26 deletions awslimitchecker/tests/test_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@
import pytest
import sys
from awslimitchecker.limit import AwsLimit, AwsLimitUsage
from awslimitchecker.services.base import _AwsService


class TestAwsLimit(object):

def setup(self):
self.mock_svc = Mock(spec_set=_AwsService)

def test_init(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
7,
11
)
assert limit.name == 'limitname'
assert limit.service_name == 'svcname'
assert limit.service == self.mock_svc
assert limit.default_limit == 3
assert limit.limit_type is None
assert limit.limit_subtype is None
Expand All @@ -67,7 +71,7 @@ def test_init_valueerror(self):
with pytest.raises(ValueError) as excinfo:
AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
11,
7
Expand All @@ -82,15 +86,15 @@ def test_init_valueerror(self):
def test_init_type(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
1,
6,
12,
limit_type='foo',
limit_subtype='bar',
)
assert limit.name == 'limitname'
assert limit.service_name == 'svcname'
assert limit.service == self.mock_svc
assert limit.default_limit == 1
assert limit.limit_type == 'foo'
assert limit.limit_subtype == 'bar'
Expand All @@ -102,7 +106,7 @@ def test_init_type(self):
def test_set_limit_override(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -115,7 +119,7 @@ def test_set_limit_override(self):
def test_set_limit_override_ta_False(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -128,7 +132,7 @@ def test_set_limit_override_ta_False(self):
def test_add_current_usage(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -144,7 +148,7 @@ def test_add_current_usage(self):
def test_get_current_usage(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -155,7 +159,7 @@ def test_get_current_usage(self):
def test_get_current_usage_str_none(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -165,7 +169,7 @@ def test_get_current_usage_str_none(self):
def test_get_current_usage_str(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -176,7 +180,7 @@ def test_get_current_usage_str(self):
def test_get_current_usage_str_id(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -187,7 +191,7 @@ def test_get_current_usage_str_id(self):
def test_get_current_usage_str_multi(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -200,7 +204,7 @@ def test_get_current_usage_str_multi(self):
def test_get_current_usage_str_multi_id(self):
limit = AwsLimit(
'limitname',
'svcname',
self.mock_svc,
3,
1,
2
Expand All @@ -212,16 +216,16 @@ def test_get_current_usage_str_multi_id(self):
'foo3bar=3, foo4bar=4)'

def test_get_limit_default(self):
limit = AwsLimit('limitname', 'svcname', 3, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 3, 1, 2)
assert limit.get_limit() == 3

def test_get_limit_override(self):
limit = AwsLimit('limitname', 'svcname', 3, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 3, 1, 2)
limit.set_limit_override(55)
assert limit.get_limit() == 55

def test_check_thresholds_pct(self):
limit = AwsLimit('limitname', 'svcname', 3, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 3, 1, 2)
u1 = AwsLimitUsage(limit, 4, id='foo4bar')
u2 = AwsLimitUsage(limit, 3, id='foo3bar')
u3 = AwsLimitUsage(limit, 2, id='foo2bar')
Expand All @@ -240,7 +244,7 @@ def test_check_thresholds_pct(self):
assert mock_get_limit.mock_calls == [call()]

def test_check_thresholds_pct_warn(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 4, id='foo4bar')
u2 = AwsLimitUsage(limit, 50, id='foo3bar')
u3 = AwsLimitUsage(limit, 2, id='foo2bar')
Expand All @@ -259,7 +263,7 @@ def test_check_thresholds_pct_warn(self):
assert mock_get_limit.mock_calls == [call()]

def test_check_thresholds_int_warn(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 4, id='foo4bar')
u2 = AwsLimitUsage(limit, 1, id='foo3bar')
u3 = AwsLimitUsage(limit, 2, id='foo2bar')
Expand All @@ -278,7 +282,7 @@ def test_check_thresholds_int_warn(self):
assert mock_get_limit.mock_calls == [call()]

def test_check_thresholds_int_warn_crit(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 4, id='foo4bar')
u2 = AwsLimitUsage(limit, 1, id='foo3bar')
u3 = AwsLimitUsage(limit, 7, id='foo2bar')
Expand All @@ -297,7 +301,7 @@ def test_check_thresholds_int_warn_crit(self):
assert mock_get_limit.mock_calls == [call()]

def test_check_thresholds_pct_crit(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 4, id='foo4bar')
u2 = AwsLimitUsage(limit, 3, id='foo3bar')
u3 = AwsLimitUsage(limit, 95, id='foo2bar')
Expand All @@ -316,7 +320,7 @@ def test_check_thresholds_pct_crit(self):
assert mock_get_limit.mock_calls == [call()]

def test_check_thresholds_int_crit(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 9, id='foo4bar')
u2 = AwsLimitUsage(limit, 3, id='foo3bar')
u3 = AwsLimitUsage(limit, 95, id='foo2bar')
Expand All @@ -335,7 +339,7 @@ def test_check_thresholds_int_crit(self):
assert mock_get_limit.mock_calls == [call()]

def test_check_thresholds_pct_warn_crit(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
u1 = AwsLimitUsage(limit, 50, id='foo4bar')
u2 = AwsLimitUsage(limit, 3, id='foo3bar')
u3 = AwsLimitUsage(limit, 95, id='foo2bar')
Expand All @@ -354,19 +358,19 @@ def test_check_thresholds_pct_warn_crit(self):
assert mock_get_limit.mock_calls == [call()]

def test_get_warnings(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
m = Mock()
limit._warnings = m
assert limit.get_warnings() == m

def test_get_criticals(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
m = Mock()
limit._criticals = m
assert limit.get_criticals() == m

def test_get_thresholds(self):
limit = AwsLimit('limitname', 'svcname', 100, 1, 2)
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
assert limit._get_thresholds() == (
None,
1,
Expand Down

0 comments on commit 7d327eb

Please sign in to comment.