from __future__ import unicode_literals
from moto.core.responses import BaseResponse
from moto.core.utils import amz_crc32, amzn_request_id
from .models import autoscaling_backends
class AutoScalingResponse(BaseResponse):
@property
def autoscaling_backend(self):
return autoscaling_backends[self.region]
def create_launch_configuration(self):
instance_monitoring_string = self._get_param(
'InstanceMonitoring.Enabled')
if instance_monitoring_string == 'true':
instance_monitoring = True
else:
instance_monitoring = False
self.autoscaling_backend.create_launch_configuration(
name=self._get_param('LaunchConfigurationName'),
image_id=self._get_param('ImageId'),
key_name=self._get_param('KeyName'),
ramdisk_id=self._get_param('RamdiskId'),
kernel_id=self._get_param('KernelId'),
security_groups=self._get_multi_param('SecurityGroups.member'),
user_data=self._get_param('UserData'),
instance_type=self._get_param('InstanceType'),
instance_monitoring=instance_monitoring,
instance_profile_name=self._get_param('IamInstanceProfile'),
spot_price=self._get_param('SpotPrice'),
ebs_optimized=self._get_param('EbsOptimized'),
associate_public_ip_address=self._get_param(
"AssociatePublicIpAddress"),
block_device_mappings=self._get_list_prefix(
'BlockDeviceMappings.member')
)
template = self.response_template(CREATE_LAUNCH_CONFIGURATION_TEMPLATE)
return template.render()
def describe_launch_configurations(self):
names = self._get_multi_param('LaunchConfigurationNames.member')
all_launch_configurations = self.autoscaling_backend.describe_launch_configurations(names)
marker = self._get_param('NextToken')
all_names = [lc.name for lc in all_launch_configurations]
if marker:
start = all_names.index(marker) + 1
else:
start = 0
max_records = self._get_int_param('MaxRecords', 50) # the default is 100, but using 50 to make testing easier
launch_configurations_resp = all_launch_configurations[start:start + max_records]
next_token = None
if len(all_launch_configurations) > start + max_records:
next_token = launch_configurations_resp[-1].name
template = self.response_template(
DESCRIBE_LAUNCH_CONFIGURATIONS_TEMPLATE)
return template.render(launch_configurations=launch_configurations_resp, next_token=next_token)
def delete_launch_configuration(self):
launch_configurations_name = self.querystring.get(
'LaunchConfigurationName')[0]
self.autoscaling_backend.delete_launch_configuration(
launch_configurations_name)
template = self.response_template(DELETE_LAUNCH_CONFIGURATION_TEMPLATE)
return template.render()
def create_auto_scaling_group(self):
self.autoscaling_backend.create_auto_scaling_group(
name=self._get_param('AutoScalingGroupName'),
availability_zones=self._get_multi_param(
'AvailabilityZones.member'),
desired_capacity=self._get_int_param('DesiredCapacity'),
max_size=self._get_int_param('MaxSize'),
min_size=self._get_int_param('MinSize'),
instance_id=self._get_param('InstanceId'),
launch_config_name=self._get_param('LaunchConfigurationName'),
vpc_zone_identifier=self._get_param('VPCZoneIdentifier'),
default_cooldown=self._get_int_param('DefaultCooldown'),
health_check_period=self._get_int_param('HealthCheckGracePeriod'),
health_check_type=self._get_param('HealthCheckType'),
load_balancers=self._get_multi_param('LoadBalancerNames.member'),
target_group_arns=self._get_multi_param('TargetGroupARNs.member'),
placement_group=self._get_param('PlacementGroup'),
termination_policies=self._get_multi_param(
'TerminationPolicies.member'),
tags=self._get_list_prefix('Tags.member'),
new_instances_protected_from_scale_in=self._get_bool_param(
'NewInstancesProtectedFromScaleIn', False)
)
template = self.response_template(CREATE_AUTOSCALING_GROUP_TEMPLATE)
return template.render()
@amz_crc32
@amzn_request_id
def attach_instances(self):
group_name = self._get_param('AutoScalingGroupName')
instance_ids = self._get_multi_param('InstanceIds.member')
self.autoscaling_backend.attach_instances(
group_name, instance_ids)
template = self.response_template(ATTACH_INSTANCES_TEMPLATE)
return template.render()
@amz_crc32
@amzn_request_id
def set_instance_health(self):
instance_id = self._get_param('InstanceId')
health_status = self._get_param("HealthStatus")
if health_status not in ['Healthy', 'Unhealthy']:
raise ValueError('Valid instance health states are: [Healthy, Unhealthy]')
should_respect_grace_period = self._get_param("ShouldRespectGracePeriod")
self.autoscaling_backend.set_instance_health(instance_id, health_status, should_respect_grace_period)
template = self.response_template(SET_INSTANCE_HEALTH_TEMPLATE)
return template.render()
@amz_crc32
@amzn_request_id
def detach_instances(self):
group_name = self._get_param('AutoScalingGroupName')
instance_ids = self._get_multi_param('InstanceIds.member')
should_decrement_string = self._get_param('ShouldDecrementDesiredCapacity')
if should_decrement_string == 'true':
should_decrement = True
else:
should_decrement = False
detached_instances = self.autoscaling_backend.detach_instances(
group_name, instance_ids, should_decrement)
template = self.response_template(DETACH_INSTANCES_TEMPLATE)
return template.render(detached_instances=detached_instances)
@amz_crc32
@amzn_request_id
def attach_load_balancer_target_groups(self):
group_name = self._get_param('AutoScalingGroupName')
target_group_arns = self._get_multi_param('TargetGroupARNs.member')
self.autoscaling_backend.attach_load_balancer_target_groups(
group_name, target_group_arns)
template = self.response_template(ATTACH_LOAD_BALANCER_TARGET_GROUPS_TEMPLATE)
return template.render()
@amz_crc32
@amzn_request_id
def describe_load_balancer_target_groups(self):
group_name = self._get_param('AutoScalingGroupName')
target_group_arns = self.autoscaling_backend.describe_load_balancer_target_groups(
group_name)
template = self.response_template(DESCRIBE_LOAD_BALANCER_TARGET_GROUPS)
return template.render(target_group_arns=target_group_arns)
@amz_crc32
@amzn_request_id
def detach_load_balancer_target_groups(self):
group_name = self._get_param('AutoScalingGroupName')
target_group_arns = self._get_multi_param('TargetGroupARNs.member')
self.autoscaling_backend.detach_load_balancer_target_groups(
group_name, target_group_arns)
template = self.response_template(DETACH_LOAD_BALANCER_TARGET_GROUPS_TEMPLATE)
return template.render()
def describe_auto_scaling_groups(self):
names = self._get_multi_param("AutoScalingGroupNames.member")
token = self._get_param("NextToken")
all_groups = self.autoscaling_backend.describe_auto_scaling_groups(names)
all_names = [group.name for group in all_groups]
if token:
start = all_names.index(token) + 1
else:
start = 0
max_records = self._get_int_param("MaxRecords", 50)
if max_records > 100:
raise ValueError
groups = all_groups[start:start + max_records]
next_token = None
if max_records and len(all_groups) > start + max_records:
next_token = groups[-1].name
template = self.response_template(DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE)
return template.render(groups=groups, next_token=next_token)
def update_auto_scaling_group(self):
self.autoscaling_backend.update_auto_scaling_group(
name=self._get_param('AutoScalingGroupName'),
availability_zones=self._get_multi_param(
'AvailabilityZones.member'),
desired_capacity=self._get_int_param('DesiredCapacity'),
max_size=self._get_int_param('MaxSize'),
min_size=self._get_int_param('MinSize'),
launch_config_name=self._get_param('LaunchConfigurationName'),
vpc_zone_identifier=self._get_param('VPCZoneIdentifier'),
default_cooldown=self._get_int_param('DefaultCooldown'),
health_check_period=self._get_int_param('HealthCheckGracePeriod'),
health_check_type=self._get_param('HealthCheckType'),
placement_group=self._get_param('PlacementGroup'),
termination_policies=self._get_multi_param(
'TerminationPolicies.member'),
new_instances_protected_from_scale_in=self._get_bool_param(
'NewInstancesProtectedFromScaleIn', None)
)
template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE)
return template.render()
def delete_auto_scaling_group(self):
group_name = self._get_param('AutoScalingGroupName')
self.autoscaling_backend.delete_auto_scaling_group(group_name)
template = self.response_template(DELETE_AUTOSCALING_GROUP_TEMPLATE)
return template.render()
def set_desired_capacity(self):
group_name = self._get_param('AutoScalingGroupName')
desired_capacity = self._get_int_param('DesiredCapacity')
self.autoscaling_backend.set_desired_capacity(
group_name, desired_capacity)
template = self.response_template(SET_DESIRED_CAPACITY_TEMPLATE)
return template.render()
def create_or_update_tags(self):
tags = self._get_list_prefix('Tags.member')
self.autoscaling_backend.create_or_update_tags(tags)
template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE)
return template.render()
def describe_auto_scaling_instances(self):
instance_states = self.autoscaling_backend.describe_auto_scaling_instances()
template = self.response_template(
DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE)
return template.render(instance_states=instance_states)
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'),
cooldown=self._get_int_param('Cooldown'),
)
template = self.response_template(CREATE_SCALING_POLICY_TEMPLATE)
return template.render(policy=policy)
def describe_policies(self):
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)
def delete_policy(self):
group_name = self._get_param('PolicyName')
self.autoscaling_backend.delete_policy(group_name)
template = self.response_template(DELETE_POLICY_TEMPLATE)
return template.render()
def execute_policy(self):
group_name = self._get_param('PolicyName')
self.autoscaling_backend.execute_policy(group_name)
template = self.response_template(EXECUTE_POLICY_TEMPLATE)
return template.render()
@amz_crc32
@amzn_request_id
def attach_load_balancers(self):
group_name = self._get_param('AutoScalingGroupName')
load_balancer_names = self._get_multi_param("LoadBalancerNames.member")
self.autoscaling_backend.attach_load_balancers(
group_name, load_balancer_names)
template = self.response_template(ATTACH_LOAD_BALANCERS_TEMPLATE)
return template.render()
@amz_crc32
@amzn_request_id
def describe_load_balancers(self):
group_name = self._get_param('AutoScalingGroupName')
load_balancers = self.autoscaling_backend.describe_load_balancers(group_name)
template = self.response_template(DESCRIBE_LOAD_BALANCERS_TEMPLATE)
return template.render(load_balancers=load_balancers)
@amz_crc32
@amzn_request_id
def detach_load_balancers(self):
group_name = self._get_param('AutoScalingGroupName')
load_balancer_names = self._get_multi_param("LoadBalancerNames.member")
self.autoscaling_backend.detach_load_balancers(
group_name, load_balancer_names)
template = self.response_template(DETACH_LOAD_BALANCERS_TEMPLATE)
return template.render()
def suspend_processes(self):
autoscaling_group_name = self._get_param('AutoScalingGroupName')
scaling_processes = self._get_multi_param('ScalingProcesses.member')
self.autoscaling_backend.suspend_processes(autoscaling_group_name, scaling_processes)
template = self.response_template(SUSPEND_PROCESSES_TEMPLATE)
return template.render()
def set_instance_protection(self):
group_name = self._get_param('AutoScalingGroupName')
instance_ids = self._get_multi_param('InstanceIds.member')
protected_from_scale_in = self._get_bool_param('ProtectedFromScaleIn')
self.autoscaling_backend.set_instance_protection(
group_name, instance_ids, protected_from_scale_in)
template = self.response_template(SET_INSTANCE_PROTECTION_TEMPLATE)
return template.render()
CREATE_LAUNCH_CONFIGURATION_TEMPLATE = """
7c6e177f-f082-11e1-ac58-3714bEXAMPLE
"""
DESCRIBE_LAUNCH_CONFIGURATIONS_TEMPLATE = """
{% for launch_configuration in launch_configurations %}
{{ launch_configuration.associate_public_ip_address }}
{% for security_group in launch_configuration.security_groups %}
{{ security_group }}
{% endfor %}
2013-01-21T23:04:42.200Z
{{ launch_configuration.kernel_id }}
{% if launch_configuration.instance_profile_name %}
{{ launch_configuration.instance_profile_name }}
{% endif %}
{{ launch_configuration.name }}
{% if launch_configuration.user_data %}
{{ launch_configuration.user_data }}
{% else %}
{% endif %}
{{ launch_configuration.instance_type }}
arn:aws:autoscaling:us-east-1:803981987763:launchConfiguration:9dbbbf87-6141-428a-a409-0752edbe6cad:launchConfigurationName/{{ launch_configuration.name }}
{% if launch_configuration.block_device_mappings %}
{% for mount_point, mapping in launch_configuration.block_device_mappings.items() %}
{{ mount_point }}
{% if mapping.ephemeral_name %}
{{ mapping.ephemeral_name }}
{% else %}
{% if mapping.snapshot_id %}
{{ mapping.snapshot_id }}
{% endif %}
{% if mapping.size %}
{{ mapping.size }}
{% endif %}
{% if mapping.iops %}
{{ mapping.iops }}
{% endif %}
{{ mapping.delete_on_termination }}
{{ mapping.volume_type }}
{% endif %}
{% endfor %}
{% else %}
{% endif %}
{{ launch_configuration.image_id }}
{% if launch_configuration.key_name %}
{{ launch_configuration.key_name }}
{% else %}
{% endif %}
{{ launch_configuration.ramdisk_id }}
{{ launch_configuration.ebs_optimized }}
{{ launch_configuration.instance_monitoring_enabled }}
{% if launch_configuration.spot_price %}
{{ launch_configuration.spot_price }}
{% endif %}
{% endfor %}
{% if next_token %}
{{ next_token }}
{% endif %}
d05a22f8-b690-11e2-bf8e-2113fEXAMPLE
"""
DELETE_LAUNCH_CONFIGURATION_TEMPLATE = """
7347261f-97df-11e2-8756-35eEXAMPLE
"""
CREATE_AUTOSCALING_GROUP_TEMPLATE = """
8d798a29-f083-11e1-bdfb-cb223EXAMPLE
"""
ATTACH_LOAD_BALANCER_TARGET_GROUPS_TEMPLATE = """
"""
ATTACH_INSTANCES_TEMPLATE = """
"""
DESCRIBE_LOAD_BALANCER_TARGET_GROUPS = """
{% for arn in target_group_arns %}
{{ arn }}
Added
{% endfor %}
"""
DETACH_INSTANCES_TEMPLATE = """
{% for instance in detached_instances %}
5091cb52-547a-47ce-a236-c9ccbc2cb2c9EXAMPLE
{{ group_name }}
At 2017-10-15T15:55:21Z instance {{ instance.instance.id }} was detached in response to a user request.
Detaching EC2 instance: {{ instance.instance.id }}
2017-10-15T15:55:21Z
2017-10-15T15:55:21Z
InProgress
InProgress
details
{% endfor %}
"""
DETACH_LOAD_BALANCER_TARGET_GROUPS_TEMPLATE = """
"""
DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE = """
{% for group in groups %}
{% for tag in group.tags %}
{{ tag.resource_type or tag.ResourceType }}
{{ tag.resource_id or tag.ResourceId }}
{{ tag.propagate_at_launch or tag.PropagateAtLaunch }}
{{ tag.key or tag.Key }}
{{ tag.value or tag.Value }}
{% endfor %}
{% for suspended_process in group.suspended_processes %}
{{suspended_process}}
{% endfor %}
{{ group.name }}
{{ group.health_check_type }}
2013-05-06T17:47:15.107Z
{{ group.launch_config_name }}
{% for instance_state in group.instance_states %}
{{ instance_state.health_status }}
{{ instance_state.instance.placement }}
{{ instance_state.instance.id }}
{{ group.launch_config_name }}
{{ instance_state.lifecycle_state }}
{{ instance_state.protected_from_scale_in|string|lower }}
{% endfor %}
{{ group.desired_capacity }}
{% for availability_zone in group.availability_zones %}
{{ availability_zone }}
{% endfor %}
{% if group.load_balancers %}
{% for load_balancer in group.load_balancers %}
{{ load_balancer }}
{% endfor %}
{% else %}
{% endif %}
{% if group.target_group_arns %}
{% for target_group_arn in group.target_group_arns %}
{{ target_group_arn }}
{% endfor %}
{% else %}
{% endif %}
{{ group.min_size }}
{% if group.vpc_zone_identifier %}
{{ group.vpc_zone_identifier }}
{% else %}
{% endif %}
{{ group.health_check_period }}
{{ group.default_cooldown }}
arn:aws:autoscaling:us-east-1:803981987763:autoScalingGroup:ca861182-c8f9-4ca7-b1eb-cd35505f5ebb:autoScalingGroupName/{{ group.name }}
{% if group.termination_policies %}
{% for policy in group.termination_policies %}
{{ policy }}
{% endfor %}
{% else %}
{% endif %}
{{ group.max_size }}
{% if group.placement_group %}
{{ group.placement_group }}
{% endif %}
{{ group.new_instances_protected_from_scale_in|string|lower }}
{% endfor %}
{% if next_token %}
{{ next_token }}
{% endif %}
0f02a07d-b677-11e2-9eb0-dd50EXAMPLE
"""
UPDATE_AUTOSCALING_GROUP_TEMPLATE = """
adafead0-ab8a-11e2-ba13-ab0ccEXAMPLE
"""
DELETE_AUTOSCALING_GROUP_TEMPLATE = """
70a76d42-9665-11e2-9fdf-211deEXAMPLE
"""
DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE = """
{% for instance_state in instance_states %}
{{ instance_state.health_status }}
{{ instance_state.instance.autoscaling_group.name }}
{{ instance_state.instance.placement }}
{{ instance_state.instance.id }}
{{ instance_state.instance.autoscaling_group.launch_config_name }}
{{ instance_state.lifecycle_state }}
{{ instance_state.protected_from_scale_in|string|lower }}
{% endfor %}
df992dc3-b72f-11e2-81e1-750aa6EXAMPLE
"""
CREATE_SCALING_POLICY_TEMPLATE = """
arn:aws:autoscaling:us-east-1:803981987763:scalingPolicy:b0dcf5e8
-02e6-4e31-9719-0675d0dc31ae:autoScalingGroupName/my-test-asg:policyName/my-scal
eout-policy
3cfc6fef-c08b-11e2-a697-2922EXAMPLE
"""
DESCRIBE_SCALING_POLICIES_TEMPLATE = """
{% for policy in policies %}
arn:aws:autoscaling:us-east-1:803981987763:scalingPolicy:c322
761b-3172-4d56-9a21-0ed9d6161d67:autoScalingGroupName/my-test-asg:policyName/MyScaleDownPolicy
{{ policy.adjustment_type }}
{{ policy.scaling_adjustment }}
{{ policy.name }}
{{ policy.policy_type }}
{{ policy.as_name }}
{{ policy.cooldown }}
{% endfor %}
ec3bffad-b739-11e2-b38d-15fbEXAMPLE
"""
SET_DESIRED_CAPACITY_TEMPLATE = """
9fb7e2db-6998-11e2-a985-57c82EXAMPLE
"""
EXECUTE_POLICY_TEMPLATE = """
70a76d42-9665-11e2-9fdf-211deEXAMPLE
"""
DELETE_POLICY_TEMPLATE = """
70a76d42-9665-11e2-9fdf-211deEXAMPLE
"""
ATTACH_LOAD_BALANCERS_TEMPLATE = """
"""
DESCRIBE_LOAD_BALANCERS_TEMPLATE = """
{% for load_balancer in load_balancers %}
{{ load_balancer }}
Added
{% endfor %}
"""
DETACH_LOAD_BALANCERS_TEMPLATE = """
"""
SUSPEND_PROCESSES_TEMPLATE = """
7c6e177f-f082-11e1-ac58-3714bEXAMPLE
"""
SET_INSTANCE_HEALTH_TEMPLATE = """
"""
SET_INSTANCE_PROTECTION_TEMPLATE = """
"""