From 2a02259a3c3f8a6b634af3f429ef5c5a7299c05f Mon Sep 17 00:00:00 2001 From: Jia Chen Date: Wed, 11 Jan 2017 20:40:57 -0500 Subject: [PATCH] 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 --- moto/autoscaling/models.py | 14 ++++-- moto/autoscaling/responses.py | 7 ++- tests/test_autoscaling/test_autoscaling.py | 54 ++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/moto/autoscaling/models.py b/moto/autoscaling/models.py index 2dbdc077d..53a0f62df 100644 --- a/moto/autoscaling/models.py +++ b/moto/autoscaling/models.py @@ -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) diff --git a/moto/autoscaling/responses.py b/moto/autoscaling/responses.py index 1840e029c..976199131 100644 --- a/moto/autoscaling/responses.py +++ b/moto/autoscaling/responses.py @@ -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 = """