Adding PolicyType to scaling policy and implementing filtering in describe_policies (#797)

* Adding PolicyType to FakeScalingPolicy

* Implement filtering for AutoScalingBackend.describe_policies(...)

* Unit test for describe_policies fuction in autoscaling
This commit is contained in:
Jia Chen 2017-01-11 20:40:57 -05:00 committed by Steve Pulec
parent f212d70104
commit 2a02259a3c
3 changed files with 69 additions and 6 deletions

View File

@ -16,9 +16,10 @@ class InstanceState(object):
class FakeScalingPolicy(object):
def __init__(self, name, adjustment_type, as_name, scaling_adjustment,
def __init__(self, name, policy_type, adjustment_type, as_name, scaling_adjustment,
cooldown, autoscaling_backend):
self.name = name
self.policy_type = policy_type
self.adjustment_type = adjustment_type
self.as_name = as_name
self.scaling_adjustment = scaling_adjustment
@ -407,16 +408,19 @@ class AutoScalingBackend(BaseBackend):
desired_capacity = int(desired_capacity)
self.set_desired_capacity(group_name, desired_capacity)
def create_autoscaling_policy(self, name, adjustment_type, as_name,
def create_autoscaling_policy(self, name, policy_type, adjustment_type, as_name,
scaling_adjustment, cooldown):
policy = FakeScalingPolicy(name, adjustment_type, as_name,
policy = FakeScalingPolicy(name, policy_type, adjustment_type, as_name,
scaling_adjustment, cooldown, self)
self.policies[name] = policy
return policy
def describe_policies(self):
return list(self.policies.values())
def describe_policies(self, autoscaling_group_name=None, policy_names=None, policy_types=None):
return [policy for policy in self.policies.values()
if (not autoscaling_group_name or policy.as_name == autoscaling_group_name) and
(not policy_names or policy.name in policy_names) and
(not policy_types or policy.policy_type in policy_types)]
def delete_policy(self, group_name):
self.policies.pop(group_name, None)

View File

@ -120,6 +120,7 @@ class AutoScalingResponse(BaseResponse):
def put_scaling_policy(self):
policy = self.autoscaling_backend.create_autoscaling_policy(
name=self._get_param('PolicyName'),
policy_type=self._get_param('PolicyType'),
adjustment_type=self._get_param('AdjustmentType'),
as_name=self._get_param('AutoScalingGroupName'),
scaling_adjustment=self._get_int_param('ScalingAdjustment'),
@ -129,7 +130,10 @@ class AutoScalingResponse(BaseResponse):
return template.render(policy=policy)
def describe_policies(self):
policies = self.autoscaling_backend.describe_policies()
policies = self.autoscaling_backend.describe_policies(
autoscaling_group_name=self._get_param('AutoScalingGroupName'),
policy_names=self._get_multi_param('PolicyNames.member'),
policy_types=self._get_multi_param('PolicyTypes.member'))
template = self.response_template(DESCRIBE_SCALING_POLICIES_TEMPLATE)
return template.render(policies=policies)
@ -373,6 +377,7 @@ DESCRIBE_SCALING_POLICIES_TEMPLATE = """<DescribePoliciesResponse xmlns="http://
<AdjustmentType>{{ policy.adjustment_type }}</AdjustmentType>
<ScalingAdjustment>{{ policy.scaling_adjustment }}</ScalingAdjustment>
<PolicyName>{{ policy.name }}</PolicyName>
<PolicyType>{{ policy.policy_type }}</PolicyType>
<AutoScalingGroupName>{{ policy.as_name }}</AutoScalingGroupName>
<Cooldown>{{ policy.cooldown }}</Cooldown>
<Alarms/>

View File

@ -524,3 +524,57 @@ def test_autoscaling_taqs_update_boto3():
AutoScalingGroupNames=["test_asg"]
)
response['AutoScalingGroups'][0]['Tags'].should.have.length_of(2)
@mock_autoscaling
def test_autoscaling_describe_policies_boto3():
client = boto3.client('autoscaling', region_name='us-east-1')
_ = client.create_launch_configuration(
LaunchConfigurationName='test_launch_configuration'
)
_ = client.create_auto_scaling_group(
AutoScalingGroupName='test_asg',
LaunchConfigurationName='test_launch_configuration',
MinSize=0,
MaxSize=20,
DesiredCapacity=5,
Tags=[{
"ResourceId": 'test_asg',
"Key": 'test_key',
"Value": 'test_value',
"PropagateAtLaunch": True
}]
)
client.put_scaling_policy(
AutoScalingGroupName='test_asg',
PolicyName='test_policy_down',
PolicyType='SimpleScaling',
AdjustmentType='PercentChangeInCapacity',
ScalingAdjustment=-10,
Cooldown=60,
MinAdjustmentMagnitude=1)
client.put_scaling_policy(
AutoScalingGroupName='test_asg',
PolicyName='test_policy_up',
PolicyType='SimpleScaling',
AdjustmentType='PercentChangeInCapacity',
ScalingAdjustment=10,
Cooldown=60,
MinAdjustmentMagnitude=1)
response = client.describe_policies()
response['ScalingPolicies'].should.have.length_of(2)
response = client.describe_policies(AutoScalingGroupName='test_asg')
response['ScalingPolicies'].should.have.length_of(2)
response = client.describe_policies(PolicyTypes=['StepScaling'])
response['ScalingPolicies'].should.have.length_of(0)
response = client.describe_policies(
AutoScalingGroupName='test_asg',
PolicyNames=['test_policy_down'],
PolicyTypes=['SimpleScaling']
)
response['ScalingPolicies'].should.have.length_of(1)
response['ScalingPolicies'][0]['PolicyName'].should.equal('test_policy_down')