Skip to content

Commit

Permalink
issue #131 - AwsLimit ta_service_name and ta_limit_name
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Mar 2, 2016
1 parent e0dc416 commit 9a6e0bd
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
42 changes: 40 additions & 2 deletions awslimitchecker/limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class AwsLimit(object):

def __init__(self, name, service, default_limit,
def_warning_threshold, def_critical_threshold,
limit_type=None, limit_subtype=None):
limit_type=None, limit_subtype=None,
ta_service_name=None, ta_limit_name=None):
"""
Describes one specific AWS service limit, as well as its
current utilization, default limit, thresholds, and any
Expand All @@ -65,7 +66,7 @@ def __init__(self, name, service, default_limit,
:type name: string
:param service: the :py:class:`~._AwsService` class that
this limit is for
:type service_name: :py:class:`~._AwsService`
:type service: :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 @@ -80,6 +81,13 @@ def __init__(self, name, service, default_limit,
such as "AWS::EC2::Instance" or "AWS::RDS::DBSubnetGroup".
:param limit_subtype: resource sub-type for this limit, if applicable,
such as "t2.micro" or "SecurityGroup"
:type limit_subtype: str
:param ta_service_name: The service name returned by Trusted Advisor
for this limit, if different from the name of ``service``
:type ta_service_name: str
:param ta_limit_name: The limit name returned by Trusted Advisor for
this limit, if different from ``name``.
:type ta_limit_name: str
:raises: ValueError
"""
if def_warning_threshold >= def_critical_threshold:
Expand All @@ -103,6 +111,8 @@ def __init__(self, name, service, default_limit,
self.crit_count = None
self._warnings = []
self._criticals = []
self._ta_service_name = ta_service_name
self._ta_limit_name = ta_limit_name

def set_limit_override(self, limit_value, override_ta=True):
"""
Expand Down Expand Up @@ -379,6 +389,34 @@ def get_criticals(self):
"""
return self._criticals

@property
def ta_service_name(self):
"""
Return the effective Trusted Advisor service name that this limit's
data will have. This should be ``self._ta_service_name`` if set,
otherwise the name of ``self.service``.
:return: Trusted Advisor service data name
:rtype: str
"""
if self._ta_service_name is not None:
return self._ta_service_name
return self.service.service_name

@property
def ta_limit_name(self):
"""
Return the effective Trusted Advisor limit name that this limit's
data will have. This should be ``self._ta_limit_name`` if set,
otherwise ``self.name``.
:return: Trusted Advisor limit data name
:rtype: str
"""
if self._ta_limit_name is not None:
return self._ta_limit_name
return self.name


class AwsLimitUsage(object):

Expand Down
47 changes: 47 additions & 0 deletions awslimitchecker/tests/test_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TestAwsLimit(object):

def setup(self):
self.mock_svc = Mock(spec_set=_AwsService)
type(self.mock_svc).service_name = 'mysname'

def test_init(self):
limit = AwsLimit(
Expand All @@ -80,6 +81,32 @@ def test_init(self):
assert limit.api_limit is None
assert limit.def_warning_threshold == 7
assert limit.def_critical_threshold == 11
assert limit._ta_service_name is None
assert limit._ta_limit_name is None

def test_init_ta_names(self):
limit = AwsLimit(
'limitname',
self.mock_svc,
3,
7,
11,
ta_service_name='foo',
ta_limit_name='bar'
)
assert limit.name == 'limitname'
assert limit.service == self.mock_svc
assert limit.default_limit == 3
assert limit.limit_type is None
assert limit.limit_subtype is None
assert limit.limit_override is None
assert limit.override_ta is True
assert limit.ta_limit is None
assert limit.api_limit is None
assert limit.def_warning_threshold == 7
assert limit.def_critical_threshold == 11
assert limit._ta_service_name == 'foo'
assert limit._ta_limit_name == 'bar'

def test_init_valueerror(self):
with pytest.raises(ValueError) as excinfo:
Expand Down Expand Up @@ -522,6 +549,26 @@ def test_set_threshold_override(self):
assert limit.crit_percent == 3
assert limit.crit_count == 4

def test_ta_service_name_default(self):
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
assert limit.ta_service_name == 'mysname'

def test_ta_service_name_overridden(self):
limit = AwsLimit(
'limitname', self.mock_svc, 100, 1, 2, ta_service_name='foo'
)
assert limit.ta_service_name == 'foo'

def test_ta_limit_name_default(self):
limit = AwsLimit('limitname', self.mock_svc, 100, 1, 2)
assert limit.ta_limit_name == 'limitname'

def test_ta_limit_name_overridden(self):
limit = AwsLimit(
'limitname', self.mock_svc, 100, 1, 2, ta_limit_name='foo'
)
assert limit.ta_limit_name == 'foo'


class TestAwsLimitUsage(object):

Expand Down

0 comments on commit 9a6e0bd

Please sign in to comment.