Skip to content

Commit

Permalink
issue #95 - convert autoscaling to boto3
Browse files Browse the repository at this point in the history
  • Loading branch information
jantman committed Jan 28, 2016
1 parent 6b5f7dd commit 7ffc12e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
27 changes: 15 additions & 12 deletions awslimitchecker/services/autoscaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
"""

import abc # noqa
import boto
import boto.ec2.autoscale
import logging

from .base import _AwsService
Expand All @@ -57,10 +55,8 @@ def connect(self):
"""Connect to API if not already connected; set self.conn."""
if self.conn is not None:
return
elif self.region:
self.conn = self.connect_via(boto.ec2.autoscale.connect_to_region)
else:
self.conn = boto.connect_autoscale()
self.conn = self.connect_client('autoscaling')

def find_usage(self):
"""
Expand All @@ -75,16 +71,24 @@ def find_usage(self):

self.limits['Auto Scaling groups']._add_current_usage(
len(
boto_query_wrapper(self.conn.get_all_groups)
boto_query_wrapper(
self.conn.describe_auto_scaling_groups,
alc_marker_path=['NextToken'],
alc_data_path=['AutoScalingGroups'],
alc_marker_param='NextToken'
)['AutoScalingGroups']
),
aws_type='AWS::AutoScaling::AutoScalingGroup',
)

self.limits['Launch configurations']._add_current_usage(
len(
boto_query_wrapper(
self.conn.get_all_launch_configurations
)
self.conn.describe_launch_configurations,
alc_marker_path=['NextToken'],
alc_data_path=['LaunchConfigurations'],
alc_marker_param='NextToken'
)['LaunchConfigurations']
),
aws_type='AWS::AutoScaling::LaunchConfiguration',
)
Expand Down Expand Up @@ -145,9 +149,8 @@ def _update_limits_from_api(self):
"""
self.connect()
logger.info("Querying EC2 DescribeAccountAttributes for limits")
lims = boto_query_wrapper(self.conn.get_account_limits)
lims = boto_query_wrapper(self.conn.describe_account_limits)
self.limits['Auto Scaling groups']._set_api_limit(
lims.max_autoscaling_groups)
lims['MaxNumberOfAutoScalingGroups'])
self.limits['Launch configurations']._set_api_limit(
lims.max_launch_configurations
)
lims['MaxNumberOfLaunchConfigurations'])
88 changes: 45 additions & 43 deletions awslimitchecker/tests/services/test_autoscaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
"""

import sys
from boto.ec2.autoscale import AutoScaleConnection, connect_to_region
from boto.ec2.autoscale.limits import AccountLimits
from awslimitchecker.services.autoscaling import _AutoscalingService

# https://code.google.com/p/mock/issues/detail?id=249
Expand Down Expand Up @@ -70,46 +68,24 @@ def test_init(self):
def test_connect(self):
"""test connect()"""
mock_conn = Mock()
mock_conn_via = Mock()
cls = _AutoscalingService(21, 43)
with patch('%s.boto.connect_autoscale' % self.pbm) as mock_autoscaling:
with patch('%s.connect_via' % self.pb) as mock_connect_via:
mock_autoscaling.return_value = mock_conn
mock_connect_via.return_value = mock_conn_via
with patch('%s.connect_client' % self.pb) as mock_connect_client:
mock_connect_client.return_value = mock_conn
cls.connect()
assert mock_autoscaling.mock_calls == [call()]
assert mock_connect_via.mock_calls == []
assert mock_conn.mock_calls == []
assert mock_connect_client.mock_calls == [call('autoscaling')]
assert cls.conn == mock_conn

def test_connect_region(self):
"""test connect()"""
mock_conn = Mock()
mock_conn_via = Mock()
cls = _AutoscalingService(21, 43, region='myreg')
with patch('%s.boto.connect_autoscale' % self.pbm) as mock_autoscaling:
with patch('%s.connect_via' % self.pb) as mock_connect_via:
mock_autoscaling.return_value = mock_conn
mock_connect_via.return_value = mock_conn_via
cls.connect()
assert mock_autoscaling.mock_calls == []
assert mock_connect_via.mock_calls == [
call(connect_to_region)
]
assert mock_conn.mock_calls == []
assert cls.conn == mock_conn_via

def test_connect_again(self):
"""make sure we re-use the connection"""
mock_conn = Mock()
cls = _AutoscalingService(21, 43)
cls.conn = mock_conn
with patch('awslimitchecker.services.autoscaling.boto.connect_'
'autoscale') as mock_autoscaling:
mock_autoscaling.return_value = mock_conn
cls.connect()
assert mock_autoscaling.mock_calls == []
with patch('%s.connect_client' % self.pb) as mock_connect_client:
mock_connect_client.return_value = mock_conn
cls.connect()
assert mock_conn.mock_calls == []
assert mock_connect_client.mock_calls == []

def test_get_limits(self):
cls = _AutoscalingService(21, 43)
Expand All @@ -133,13 +109,24 @@ def test_get_limits_again(self):
assert res == mock_limits

def test_find_usage(self):
mock_conn = Mock(spec_set=AutoScaleConnection)
mock_conn = Mock()

def se_wrapper(func, *args, **kwargs):
if func == mock_conn.get_all_groups:
return [1, 2, 3]
elif func == mock_conn.get_all_launch_configurations:
return [1, 2]
if func == mock_conn.describe_auto_scaling_groups:
return {
'AutoScalingGroups': [
{'AutoScalingGroupName': 'foo'},
{'AutoScalingGroupName': 'bar'},
{'AutoScalingGroupName': 'baz'},
],
}
elif func == mock_conn.describe_launch_configurations:
return {
'LaunchConfigurations': [
{'LaunchConfigurationName': 'foo'},
{'LaunchConfigurationName': 'bar'},
],
}
return None

with patch('%s.connect' % self.pb) as mock_connect:
Expand All @@ -152,8 +139,18 @@ def se_wrapper(func, *args, **kwargs):
assert mock_connect.mock_calls == [call()]
assert mock_conn.mock_calls == []
assert mock_wrapper.mock_calls == [
call(mock_conn.get_all_groups),
call(mock_conn.get_all_launch_configurations)
call(
mock_conn.describe_auto_scaling_groups,
alc_marker_path=['NextToken'],
alc_data_path=['AutoScalingGroups'],
alc_marker_param='NextToken'
),
call(
mock_conn.describe_launch_configurations,
alc_marker_path=['NextToken'],
alc_data_path=['LaunchConfigurations'],
alc_marker_param='NextToken'
)
]
assert cls._have_usage is True
asgs = sorted(cls.limits['Auto Scaling groups'].get_current_usage())
Expand All @@ -172,10 +169,13 @@ def test_required_iam_permissions(self):
]

def test_update_limits_from_api(self):
mock_conn = Mock(spec_set=AutoScaleConnection)
aslimits = AccountLimits(connection=mock_conn)
aslimits.max_autoscaling_groups = 11
aslimits.max_launch_configurations = 22
mock_conn = Mock()
aslimits = {
'MaxNumberOfAutoScalingGroups': 11,
'MaxNumberOfLaunchConfigurations': 22,
'NumberOfAutoScalingGroups': 5,
'NumberOfLaunchConfigurations': 6
}

with patch('%s.connect' % self.pb) as mock_connect:
with patch('%s.boto_query_wrapper' % self.pbm) as mock_wrapper:
Expand All @@ -184,6 +184,8 @@ def test_update_limits_from_api(self):
mock_wrapper.return_value = aslimits
cls._update_limits_from_api()
assert mock_connect.mock_calls == [call()]
assert mock_wrapper.mock_calls == [call(mock_conn.get_account_limits)]
assert mock_wrapper.mock_calls == [
call(mock_conn.describe_account_limits)
]
assert cls.limits['Auto Scaling groups'].api_limit == 11
assert cls.limits['Launch configurations'].api_limit == 22

0 comments on commit 7ffc12e

Please sign in to comment.