Template in the ASG instance lifecycle states.

This commit is contained in:
Steve Pulec 2015-07-13 10:47:45 -04:00
parent 4db141202e
commit c45968b79f
2 changed files with 26 additions and 20 deletions

View File

@ -7,6 +7,12 @@ from moto.ec2 import ec2_backends
DEFAULT_COOLDOWN = 300 DEFAULT_COOLDOWN = 300
class InstanceState(object):
def __init__(self, instance, lifecycle_state="InService"):
self.instance = instance
self.lifecycle_state = lifecycle_state
class FakeScalingPolicy(object): class FakeScalingPolicy(object):
def __init__(self, name, adjustment_type, as_name, scaling_adjustment, def __init__(self, name, adjustment_type, as_name, scaling_adjustment,
cooldown, autoscaling_backend): cooldown, autoscaling_backend):
@ -125,7 +131,7 @@ class FakeAutoScalingGroup(object):
self.placement_group = placement_group self.placement_group = placement_group
self.termination_policies = termination_policies self.termination_policies = termination_policies
self.instances = [] self.instance_states = []
self.set_desired_capacity(desired_capacity) self.set_desired_capacity(desired_capacity)
@classmethod @classmethod
@ -179,7 +185,7 @@ class FakeAutoScalingGroup(object):
else: else:
self.desired_capacity = new_capacity self.desired_capacity = new_capacity
curr_instance_count = len(self.instances) curr_instance_count = len(self.instance_states)
if self.desired_capacity == curr_instance_count: if self.desired_capacity == curr_instance_count:
return return
@ -195,14 +201,14 @@ class FakeAutoScalingGroup(object):
) )
for instance in reservation.instances: for instance in reservation.instances:
instance.autoscaling_group = self instance.autoscaling_group = self
self.instances.extend(reservation.instances) self.instance_states.append(InstanceState(instance))
else: else:
# Need to remove some instances # Need to remove some instances
count_to_remove = curr_instance_count - self.desired_capacity count_to_remove = curr_instance_count - self.desired_capacity
instances_to_remove = self.instances[:count_to_remove] instances_to_remove = self.instance_states[:count_to_remove]
instance_ids_to_remove = [instance.id for instance in instances_to_remove] instance_ids_to_remove = [instance.instance.id for instance in instances_to_remove]
self.autoscaling_backend.ec2_backend.terminate_instances(instance_ids_to_remove) self.autoscaling_backend.ec2_backend.terminate_instances(instance_ids_to_remove)
self.instances = self.instances[count_to_remove:] self.instance_states = self.instance_states[count_to_remove:]
class AutoScalingBackend(BaseBackend): class AutoScalingBackend(BaseBackend):
@ -307,10 +313,10 @@ class AutoScalingBackend(BaseBackend):
self.autoscaling_groups.pop(group_name, None) self.autoscaling_groups.pop(group_name, None)
def describe_autoscaling_instances(self): def describe_autoscaling_instances(self):
instances = [] instance_states = []
for group in self.autoscaling_groups.values(): for group in self.autoscaling_groups.values():
instances.extend(group.instances) instance_states.extend(group.instance_states)
return instances return instance_states
def set_desired_capacity(self, group_name, desired_capacity): def set_desired_capacity(self, group_name, desired_capacity):
group = self.autoscaling_groups[group_name] group = self.autoscaling_groups[group_name]

View File

@ -103,9 +103,9 @@ class AutoScalingResponse(BaseResponse):
return template.render() return template.render()
def describe_auto_scaling_instances(self): def describe_auto_scaling_instances(self):
instances = self.autoscaling_backend.describe_autoscaling_instances() instance_states = self.autoscaling_backend.describe_autoscaling_instances()
template = self.response_template(DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE) template = self.response_template(DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE)
return template.render(instances=instances) return template.render(instance_states=instance_states)
def put_scaling_policy(self): def put_scaling_policy(self):
policy = self.autoscaling_backend.create_autoscaling_policy( policy = self.autoscaling_backend.create_autoscaling_policy(
@ -243,13 +243,13 @@ DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE = """<DescribeAutoScalingGroupsResponse xml
<EnabledMetrics/> <EnabledMetrics/>
<LaunchConfigurationName>{{ group.launch_config_name }}</LaunchConfigurationName> <LaunchConfigurationName>{{ group.launch_config_name }}</LaunchConfigurationName>
<Instances> <Instances>
{% for instance in group.instances %} {% for instance_state in group.instance_states %}
<member> <member>
<HealthStatus>HEALTHY</HealthStatus> <HealthStatus>HEALTHY</HealthStatus>
<AvailabilityZone>us-east-1e</AvailabilityZone> <AvailabilityZone>us-east-1e</AvailabilityZone>
<InstanceId>{{ instance.id }}</InstanceId> <InstanceId>{{ instance_state.instance.id }}</InstanceId>
<LaunchConfigurationName>{{ instance.autoscaling_group.launch_config_name }}</LaunchConfigurationName> <LaunchConfigurationName>{{ group.launch_config_name }}</LaunchConfigurationName>
<LifecycleState>InService</LifecycleState> <LifecycleState>{{ instance_state.lifecycle_state }}</LifecycleState>
</member> </member>
{% endfor %} {% endfor %}
</Instances> </Instances>
@ -315,14 +315,14 @@ DELETE_AUTOSCALING_GROUP_TEMPLATE = """<DeleteAutoScalingGroupResponse xmlns="ht
DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE = """<DescribeAutoScalingInstancesResponse xmlns="http://autoscaling.amazonaws.com/doc/2011-01-01/"> DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE = """<DescribeAutoScalingInstancesResponse xmlns="http://autoscaling.amazonaws.com/doc/2011-01-01/">
<DescribeAutoScalingInstancesResult> <DescribeAutoScalingInstancesResult>
<AutoScalingInstances> <AutoScalingInstances>
{% for instance in instances %} {% for instance_state in instance_states %}
<member> <member>
<HealthStatus>HEALTHY</HealthStatus> <HealthStatus>HEALTHY</HealthStatus>
<AutoScalingGroupName>{{ instance.autoscaling_group.name }}</AutoScalingGroupName> <AutoScalingGroupName>{{ instance_state.instance.autoscaling_group.name }}</AutoScalingGroupName>
<AvailabilityZone>us-east-1e</AvailabilityZone> <AvailabilityZone>us-east-1e</AvailabilityZone>
<InstanceId>{{ instance.id }}</InstanceId> <InstanceId>{{ instance_state.instance.id }}</InstanceId>
<LaunchConfigurationName>{{ instance.autoscaling_group.launch_config_name }}</LaunchConfigurationName> <LaunchConfigurationName>{{ instance_state.instance.autoscaling_group.launch_config_name }}</LaunchConfigurationName>
<LifecycleState>InService</LifecycleState> <LifecycleState>{{ instance_state.lifecycle_state }}</LifecycleState>
</member> </member>
{% endfor %} {% endfor %}
</AutoScalingInstances> </AutoScalingInstances>