from __future__ import unicode_literals from moto.core.responses import BaseResponse 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') launch_configurations = self.autoscaling_backend.describe_launch_configurations(names) template = self.response_template(DESCRIBE_LAUNCH_CONFIGURATIONS_TEMPLATE) return template.render(launch_configurations=launch_configurations) 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_autoscaling_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'), load_balancers=self._get_multi_param('LoadBalancerNames.member'), placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), tags=self._get_list_prefix('Tags.member'), ) template = self.response_template(CREATE_AUTOSCALING_GROUP_TEMPLATE) return template.render() def describe_auto_scaling_groups(self): names = self._get_multi_param("AutoScalingGroupNames.member") groups = self.autoscaling_backend.describe_autoscaling_groups(names) template = self.response_template(DESCRIBE_AUTOSCALING_GROUPS_TEMPLATE) return template.render(groups=groups) def update_auto_scaling_group(self): self.autoscaling_backend.update_autoscaling_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'), load_balancers=self._get_multi_param('LoadBalancerNames.member'), placement_group=self._get_param('PlacementGroup'), termination_policies=self._get_multi_param('TerminationPolicies.member'), ) 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_autoscaling_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_autoscaling_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'), 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() 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() 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/my-test-lc {% 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 %} d05a22f8-b690-11e2-bf8e-2113fEXAMPLE """ DELETE_LAUNCH_CONFIGURATION_TEMPLATE = """ 7347261f-97df-11e2-8756-35eEXAMPLE """ CREATE_AUTOSCALING_GROUP_TEMPLATE = """ 8d798a29-f083-11e1-bdfb-cb223EXAMPLE """ 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 %} {{ group.name }} {{ group.health_check_type }} 2013-05-06T17:47:15.107Z {{ group.launch_config_name }} {% for instance_state in group.instance_states %} HEALTHY us-east-1e {{ instance_state.instance.id }} {{ group.launch_config_name }} {{ instance_state.lifecycle_state }} {% 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 %} {{ 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/my-test-asg-lbs {% 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 %} {% endfor %} 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 %} HEALTHY {{ instance_state.instance.autoscaling_group.name }} us-east-1e {{ instance_state.instance.id }} {{ instance_state.instance.autoscaling_group.launch_config_name }} {{ instance_state.lifecycle_state }} {% 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.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 """