From 7ffc12ed248ab457de811edbc5653fab5db5290a Mon Sep 17 00:00:00 2001 From: Jason Antman Date: Wed, 27 Jan 2016 23:16:16 -0500 Subject: [PATCH] issue #95 - convert autoscaling to boto3 --- awslimitchecker/services/autoscaling.py | 27 +++--- .../tests/services/test_autoscaling.py | 88 ++++++++++--------- 2 files changed, 60 insertions(+), 55 deletions(-) diff --git a/awslimitchecker/services/autoscaling.py b/awslimitchecker/services/autoscaling.py index c963b3aa..0152e11f 100644 --- a/awslimitchecker/services/autoscaling.py +++ b/awslimitchecker/services/autoscaling.py @@ -38,8 +38,6 @@ """ import abc # noqa -import boto -import boto.ec2.autoscale import logging from .base import _AwsService @@ -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): """ @@ -75,7 +71,12 @@ 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', ) @@ -83,8 +84,11 @@ def find_usage(self): 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', ) @@ -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']) diff --git a/awslimitchecker/tests/services/test_autoscaling.py b/awslimitchecker/tests/services/test_autoscaling.py index 42a42858..39dd7881 100644 --- a/awslimitchecker/tests/services/test_autoscaling.py +++ b/awslimitchecker/tests/services/test_autoscaling.py @@ -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 @@ -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) @@ -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: @@ -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()) @@ -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: @@ -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