Run black on moto & test directories.
This commit is contained in:
parent
c820395dbf
commit
96e5b1993d
507 changed files with 52541 additions and 47814 deletions
|
|
@ -2,6 +2,6 @@ from __future__ import unicode_literals
|
|||
from .models import autoscaling_backends
|
||||
from ..core.models import base_decorator, deprecated_base_decorator
|
||||
|
||||
autoscaling_backend = autoscaling_backends['us-east-1']
|
||||
autoscaling_backend = autoscaling_backends["us-east-1"]
|
||||
mock_autoscaling = base_decorator(autoscaling_backends)
|
||||
mock_autoscaling_deprecated = deprecated_base_decorator(autoscaling_backends)
|
||||
|
|
|
|||
|
|
@ -12,13 +12,12 @@ class ResourceContentionError(RESTError):
|
|||
def __init__(self):
|
||||
super(ResourceContentionError, self).__init__(
|
||||
"ResourceContentionError",
|
||||
"You already have a pending update to an Auto Scaling resource (for example, a group, instance, or load balancer).")
|
||||
"You already have a pending update to an Auto Scaling resource (for example, a group, instance, or load balancer).",
|
||||
)
|
||||
|
||||
|
||||
class InvalidInstanceError(AutoscalingClientError):
|
||||
|
||||
def __init__(self, instance_id):
|
||||
super(InvalidInstanceError, self).__init__(
|
||||
"ValidationError",
|
||||
"Instance [{0}] is invalid."
|
||||
.format(instance_id))
|
||||
"ValidationError", "Instance [{0}] is invalid.".format(instance_id)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ from moto.elb import elb_backends
|
|||
from moto.elbv2 import elbv2_backends
|
||||
from moto.elb.exceptions import LoadBalancerNotFoundError
|
||||
from .exceptions import (
|
||||
AutoscalingClientError, ResourceContentionError, InvalidInstanceError
|
||||
AutoscalingClientError,
|
||||
ResourceContentionError,
|
||||
InvalidInstanceError,
|
||||
)
|
||||
|
||||
# http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/AS_Concepts.html#Cooldown
|
||||
|
|
@ -22,8 +24,13 @@ ASG_NAME_TAG = "aws:autoscaling:groupName"
|
|||
|
||||
|
||||
class InstanceState(object):
|
||||
def __init__(self, instance, lifecycle_state="InService",
|
||||
health_status="Healthy", protected_from_scale_in=False):
|
||||
def __init__(
|
||||
self,
|
||||
instance,
|
||||
lifecycle_state="InService",
|
||||
health_status="Healthy",
|
||||
protected_from_scale_in=False,
|
||||
):
|
||||
self.instance = instance
|
||||
self.lifecycle_state = lifecycle_state
|
||||
self.health_status = health_status
|
||||
|
|
@ -31,8 +38,16 @@ class InstanceState(object):
|
|||
|
||||
|
||||
class FakeScalingPolicy(BaseModel):
|
||||
def __init__(self, name, policy_type, adjustment_type, as_name, scaling_adjustment,
|
||||
cooldown, autoscaling_backend):
|
||||
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
|
||||
|
|
@ -45,21 +60,38 @@ class FakeScalingPolicy(BaseModel):
|
|||
self.autoscaling_backend = autoscaling_backend
|
||||
|
||||
def execute(self):
|
||||
if self.adjustment_type == 'ExactCapacity':
|
||||
if self.adjustment_type == "ExactCapacity":
|
||||
self.autoscaling_backend.set_desired_capacity(
|
||||
self.as_name, self.scaling_adjustment)
|
||||
elif self.adjustment_type == 'ChangeInCapacity':
|
||||
self.as_name, self.scaling_adjustment
|
||||
)
|
||||
elif self.adjustment_type == "ChangeInCapacity":
|
||||
self.autoscaling_backend.change_capacity(
|
||||
self.as_name, self.scaling_adjustment)
|
||||
elif self.adjustment_type == 'PercentChangeInCapacity':
|
||||
self.as_name, self.scaling_adjustment
|
||||
)
|
||||
elif self.adjustment_type == "PercentChangeInCapacity":
|
||||
self.autoscaling_backend.change_capacity_percent(
|
||||
self.as_name, self.scaling_adjustment)
|
||||
self.as_name, self.scaling_adjustment
|
||||
)
|
||||
|
||||
|
||||
class FakeLaunchConfiguration(BaseModel):
|
||||
def __init__(self, name, image_id, key_name, ramdisk_id, kernel_id, security_groups, user_data,
|
||||
instance_type, instance_monitoring, instance_profile_name,
|
||||
spot_price, ebs_optimized, associate_public_ip_address, block_device_mapping_dict):
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
image_id,
|
||||
key_name,
|
||||
ramdisk_id,
|
||||
kernel_id,
|
||||
security_groups,
|
||||
user_data,
|
||||
instance_type,
|
||||
instance_monitoring,
|
||||
instance_profile_name,
|
||||
spot_price,
|
||||
ebs_optimized,
|
||||
associate_public_ip_address,
|
||||
block_device_mapping_dict,
|
||||
):
|
||||
self.name = name
|
||||
self.image_id = image_id
|
||||
self.key_name = key_name
|
||||
|
|
@ -80,8 +112,8 @@ class FakeLaunchConfiguration(BaseModel):
|
|||
config = backend.create_launch_configuration(
|
||||
name=name,
|
||||
image_id=instance.image_id,
|
||||
kernel_id='',
|
||||
ramdisk_id='',
|
||||
kernel_id="",
|
||||
ramdisk_id="",
|
||||
key_name=instance.key_name,
|
||||
security_groups=instance.security_groups,
|
||||
user_data=instance.user_data,
|
||||
|
|
@ -91,13 +123,15 @@ class FakeLaunchConfiguration(BaseModel):
|
|||
spot_price=None,
|
||||
ebs_optimized=instance.ebs_optimized,
|
||||
associate_public_ip_address=instance.associate_public_ip,
|
||||
block_device_mappings=instance.block_device_mapping
|
||||
block_device_mappings=instance.block_device_mapping,
|
||||
)
|
||||
return config
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
properties = cloudformation_json['Properties']
|
||||
def create_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
properties = cloudformation_json["Properties"]
|
||||
|
||||
instance_profile_name = properties.get("IamInstanceProfile")
|
||||
|
||||
|
|
@ -115,20 +149,26 @@ class FakeLaunchConfiguration(BaseModel):
|
|||
instance_profile_name=instance_profile_name,
|
||||
spot_price=properties.get("SpotPrice"),
|
||||
ebs_optimized=properties.get("EbsOptimized"),
|
||||
associate_public_ip_address=properties.get(
|
||||
"AssociatePublicIpAddress"),
|
||||
block_device_mappings=properties.get("BlockDeviceMapping.member")
|
||||
associate_public_ip_address=properties.get("AssociatePublicIpAddress"),
|
||||
block_device_mappings=properties.get("BlockDeviceMapping.member"),
|
||||
)
|
||||
return config
|
||||
|
||||
@classmethod
|
||||
def update_from_cloudformation_json(cls, original_resource, new_resource_name, cloudformation_json, region_name):
|
||||
def update_from_cloudformation_json(
|
||||
cls, original_resource, new_resource_name, cloudformation_json, region_name
|
||||
):
|
||||
cls.delete_from_cloudformation_json(
|
||||
original_resource.name, cloudformation_json, region_name)
|
||||
return cls.create_from_cloudformation_json(new_resource_name, cloudformation_json, region_name)
|
||||
original_resource.name, cloudformation_json, region_name
|
||||
)
|
||||
return cls.create_from_cloudformation_json(
|
||||
new_resource_name, cloudformation_json, region_name
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def delete_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
def delete_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
backend = autoscaling_backends[region_name]
|
||||
try:
|
||||
backend.delete_launch_configuration(resource_name)
|
||||
|
|
@ -153,34 +193,49 @@ class FakeLaunchConfiguration(BaseModel):
|
|||
@property
|
||||
def instance_monitoring_enabled(self):
|
||||
if self.instance_monitoring:
|
||||
return 'true'
|
||||
return 'false'
|
||||
return "true"
|
||||
return "false"
|
||||
|
||||
def _parse_block_device_mappings(self):
|
||||
block_device_map = BlockDeviceMapping()
|
||||
for mapping in self.block_device_mapping_dict:
|
||||
block_type = BlockDeviceType()
|
||||
mount_point = mapping.get('device_name')
|
||||
if 'ephemeral' in mapping.get('virtual_name', ''):
|
||||
block_type.ephemeral_name = mapping.get('virtual_name')
|
||||
mount_point = mapping.get("device_name")
|
||||
if "ephemeral" in mapping.get("virtual_name", ""):
|
||||
block_type.ephemeral_name = mapping.get("virtual_name")
|
||||
else:
|
||||
block_type.volume_type = mapping.get('ebs._volume_type')
|
||||
block_type.snapshot_id = mapping.get('ebs._snapshot_id')
|
||||
block_type.volume_type = mapping.get("ebs._volume_type")
|
||||
block_type.snapshot_id = mapping.get("ebs._snapshot_id")
|
||||
block_type.delete_on_termination = mapping.get(
|
||||
'ebs._delete_on_termination')
|
||||
block_type.size = mapping.get('ebs._volume_size')
|
||||
block_type.iops = mapping.get('ebs._iops')
|
||||
"ebs._delete_on_termination"
|
||||
)
|
||||
block_type.size = mapping.get("ebs._volume_size")
|
||||
block_type.iops = mapping.get("ebs._iops")
|
||||
block_device_map[mount_point] = block_type
|
||||
return block_device_map
|
||||
|
||||
|
||||
class FakeAutoScalingGroup(BaseModel):
|
||||
def __init__(self, name, availability_zones, desired_capacity, max_size,
|
||||
min_size, launch_config_name, vpc_zone_identifier,
|
||||
default_cooldown, health_check_period, health_check_type,
|
||||
load_balancers, target_group_arns, placement_group, termination_policies,
|
||||
autoscaling_backend, tags,
|
||||
new_instances_protected_from_scale_in=False):
|
||||
def __init__(
|
||||
self,
|
||||
name,
|
||||
availability_zones,
|
||||
desired_capacity,
|
||||
max_size,
|
||||
min_size,
|
||||
launch_config_name,
|
||||
vpc_zone_identifier,
|
||||
default_cooldown,
|
||||
health_check_period,
|
||||
health_check_type,
|
||||
load_balancers,
|
||||
target_group_arns,
|
||||
placement_group,
|
||||
termination_policies,
|
||||
autoscaling_backend,
|
||||
tags,
|
||||
new_instances_protected_from_scale_in=False,
|
||||
):
|
||||
self.autoscaling_backend = autoscaling_backend
|
||||
self.name = name
|
||||
|
||||
|
|
@ -190,17 +245,22 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
self.min_size = min_size
|
||||
|
||||
self.launch_config = self.autoscaling_backend.launch_configurations[
|
||||
launch_config_name]
|
||||
launch_config_name
|
||||
]
|
||||
self.launch_config_name = launch_config_name
|
||||
|
||||
self.default_cooldown = default_cooldown if default_cooldown else DEFAULT_COOLDOWN
|
||||
self.default_cooldown = (
|
||||
default_cooldown if default_cooldown else DEFAULT_COOLDOWN
|
||||
)
|
||||
self.health_check_period = health_check_period
|
||||
self.health_check_type = health_check_type if health_check_type else "EC2"
|
||||
self.load_balancers = load_balancers
|
||||
self.target_group_arns = target_group_arns
|
||||
self.placement_group = placement_group
|
||||
self.termination_policies = termination_policies
|
||||
self.new_instances_protected_from_scale_in = new_instances_protected_from_scale_in
|
||||
self.new_instances_protected_from_scale_in = (
|
||||
new_instances_protected_from_scale_in
|
||||
)
|
||||
|
||||
self.suspended_processes = []
|
||||
self.instance_states = []
|
||||
|
|
@ -215,8 +275,10 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
|
||||
if vpc_zone_identifier:
|
||||
# extract azs for vpcs
|
||||
subnet_ids = vpc_zone_identifier.split(',')
|
||||
subnets = self.autoscaling_backend.ec2_backend.get_all_subnets(subnet_ids=subnet_ids)
|
||||
subnet_ids = vpc_zone_identifier.split(",")
|
||||
subnets = self.autoscaling_backend.ec2_backend.get_all_subnets(
|
||||
subnet_ids=subnet_ids
|
||||
)
|
||||
vpc_zones = [subnet.availability_zone for subnet in subnets]
|
||||
|
||||
if availability_zones and set(availability_zones) != set(vpc_zones):
|
||||
|
|
@ -229,7 +291,7 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
if not update:
|
||||
raise AutoscalingClientError(
|
||||
"ValidationError",
|
||||
"At least one Availability Zone or VPC Subnet is required."
|
||||
"At least one Availability Zone or VPC Subnet is required.",
|
||||
)
|
||||
return
|
||||
|
||||
|
|
@ -237,8 +299,10 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
self.vpc_zone_identifier = vpc_zone_identifier
|
||||
|
||||
@classmethod
|
||||
def create_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
properties = cloudformation_json['Properties']
|
||||
def create_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
properties = cloudformation_json["Properties"]
|
||||
|
||||
launch_config_name = properties.get("LaunchConfigurationName")
|
||||
load_balancer_names = properties.get("LoadBalancerNames", [])
|
||||
|
|
@ -253,7 +317,8 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
min_size=properties.get("MinSize"),
|
||||
launch_config_name=launch_config_name,
|
||||
vpc_zone_identifier=(
|
||||
','.join(properties.get("VPCZoneIdentifier", [])) or None),
|
||||
",".join(properties.get("VPCZoneIdentifier", [])) or None
|
||||
),
|
||||
default_cooldown=properties.get("Cooldown"),
|
||||
health_check_period=properties.get("HealthCheckGracePeriod"),
|
||||
health_check_type=properties.get("HealthCheckType"),
|
||||
|
|
@ -263,18 +328,26 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
termination_policies=properties.get("TerminationPolicies", []),
|
||||
tags=properties.get("Tags", []),
|
||||
new_instances_protected_from_scale_in=properties.get(
|
||||
"NewInstancesProtectedFromScaleIn", False)
|
||||
"NewInstancesProtectedFromScaleIn", False
|
||||
),
|
||||
)
|
||||
return group
|
||||
|
||||
@classmethod
|
||||
def update_from_cloudformation_json(cls, original_resource, new_resource_name, cloudformation_json, region_name):
|
||||
def update_from_cloudformation_json(
|
||||
cls, original_resource, new_resource_name, cloudformation_json, region_name
|
||||
):
|
||||
cls.delete_from_cloudformation_json(
|
||||
original_resource.name, cloudformation_json, region_name)
|
||||
return cls.create_from_cloudformation_json(new_resource_name, cloudformation_json, region_name)
|
||||
original_resource.name, cloudformation_json, region_name
|
||||
)
|
||||
return cls.create_from_cloudformation_json(
|
||||
new_resource_name, cloudformation_json, region_name
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def delete_from_cloudformation_json(cls, resource_name, cloudformation_json, region_name):
|
||||
def delete_from_cloudformation_json(
|
||||
cls, resource_name, cloudformation_json, region_name
|
||||
):
|
||||
backend = autoscaling_backends[region_name]
|
||||
try:
|
||||
backend.delete_auto_scaling_group(resource_name)
|
||||
|
|
@ -289,11 +362,21 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
def physical_resource_id(self):
|
||||
return self.name
|
||||
|
||||
def update(self, availability_zones, desired_capacity, max_size, min_size,
|
||||
launch_config_name, vpc_zone_identifier, default_cooldown,
|
||||
health_check_period, health_check_type,
|
||||
placement_group, termination_policies,
|
||||
new_instances_protected_from_scale_in=None):
|
||||
def update(
|
||||
self,
|
||||
availability_zones,
|
||||
desired_capacity,
|
||||
max_size,
|
||||
min_size,
|
||||
launch_config_name,
|
||||
vpc_zone_identifier,
|
||||
default_cooldown,
|
||||
health_check_period,
|
||||
health_check_type,
|
||||
placement_group,
|
||||
termination_policies,
|
||||
new_instances_protected_from_scale_in=None,
|
||||
):
|
||||
self._set_azs_and_vpcs(availability_zones, vpc_zone_identifier, update=True)
|
||||
|
||||
if max_size is not None:
|
||||
|
|
@ -309,14 +392,17 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
|
||||
if launch_config_name:
|
||||
self.launch_config = self.autoscaling_backend.launch_configurations[
|
||||
launch_config_name]
|
||||
launch_config_name
|
||||
]
|
||||
self.launch_config_name = launch_config_name
|
||||
if health_check_period is not None:
|
||||
self.health_check_period = health_check_period
|
||||
if health_check_type is not None:
|
||||
self.health_check_type = health_check_type
|
||||
if new_instances_protected_from_scale_in is not None:
|
||||
self.new_instances_protected_from_scale_in = new_instances_protected_from_scale_in
|
||||
self.new_instances_protected_from_scale_in = (
|
||||
new_instances_protected_from_scale_in
|
||||
)
|
||||
|
||||
if desired_capacity is not None:
|
||||
self.set_desired_capacity(desired_capacity)
|
||||
|
|
@ -342,25 +428,30 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
# Need to remove some instances
|
||||
count_to_remove = curr_instance_count - self.desired_capacity
|
||||
instances_to_remove = [ # only remove unprotected
|
||||
state for state in self.instance_states
|
||||
state
|
||||
for state in self.instance_states
|
||||
if not state.protected_from_scale_in
|
||||
][:count_to_remove]
|
||||
if instances_to_remove: # just in case not instances to remove
|
||||
instance_ids_to_remove = [
|
||||
instance.instance.id for instance in instances_to_remove]
|
||||
instance.instance.id for instance in instances_to_remove
|
||||
]
|
||||
self.autoscaling_backend.ec2_backend.terminate_instances(
|
||||
instance_ids_to_remove)
|
||||
self.instance_states = list(set(self.instance_states) - set(instances_to_remove))
|
||||
instance_ids_to_remove
|
||||
)
|
||||
self.instance_states = list(
|
||||
set(self.instance_states) - set(instances_to_remove)
|
||||
)
|
||||
|
||||
def get_propagated_tags(self):
|
||||
propagated_tags = {}
|
||||
for tag in self.tags:
|
||||
# boto uses 'propagate_at_launch
|
||||
# boto3 and cloudformation use PropagateAtLaunch
|
||||
if 'propagate_at_launch' in tag and tag['propagate_at_launch'] == 'true':
|
||||
propagated_tags[tag['key']] = tag['value']
|
||||
if 'PropagateAtLaunch' in tag and tag['PropagateAtLaunch']:
|
||||
propagated_tags[tag['Key']] = tag['Value']
|
||||
if "propagate_at_launch" in tag and tag["propagate_at_launch"] == "true":
|
||||
propagated_tags[tag["key"]] = tag["value"]
|
||||
if "PropagateAtLaunch" in tag and tag["PropagateAtLaunch"]:
|
||||
propagated_tags[tag["Key"]] = tag["Value"]
|
||||
return propagated_tags
|
||||
|
||||
def replace_autoscaling_group_instances(self, count_needed, propagated_tags):
|
||||
|
|
@ -371,15 +462,17 @@ class FakeAutoScalingGroup(BaseModel):
|
|||
self.launch_config.user_data,
|
||||
self.launch_config.security_groups,
|
||||
instance_type=self.launch_config.instance_type,
|
||||
tags={'instance': propagated_tags},
|
||||
tags={"instance": propagated_tags},
|
||||
placement=random.choice(self.availability_zones),
|
||||
)
|
||||
for instance in reservation.instances:
|
||||
instance.autoscaling_group = self
|
||||
self.instance_states.append(InstanceState(
|
||||
instance,
|
||||
protected_from_scale_in=self.new_instances_protected_from_scale_in,
|
||||
))
|
||||
self.instance_states.append(
|
||||
InstanceState(
|
||||
instance,
|
||||
protected_from_scale_in=self.new_instances_protected_from_scale_in,
|
||||
)
|
||||
)
|
||||
|
||||
def append_target_groups(self, target_group_arns):
|
||||
append = [x for x in target_group_arns if x not in self.target_group_arns]
|
||||
|
|
@ -402,10 +495,23 @@ class AutoScalingBackend(BaseBackend):
|
|||
self.__dict__ = {}
|
||||
self.__init__(ec2_backend, elb_backend, elbv2_backend)
|
||||
|
||||
def create_launch_configuration(self, name, image_id, key_name, kernel_id, ramdisk_id,
|
||||
security_groups, user_data, instance_type,
|
||||
instance_monitoring, instance_profile_name,
|
||||
spot_price, ebs_optimized, associate_public_ip_address, block_device_mappings):
|
||||
def create_launch_configuration(
|
||||
self,
|
||||
name,
|
||||
image_id,
|
||||
key_name,
|
||||
kernel_id,
|
||||
ramdisk_id,
|
||||
security_groups,
|
||||
user_data,
|
||||
instance_type,
|
||||
instance_monitoring,
|
||||
instance_profile_name,
|
||||
spot_price,
|
||||
ebs_optimized,
|
||||
associate_public_ip_address,
|
||||
block_device_mappings,
|
||||
):
|
||||
launch_configuration = FakeLaunchConfiguration(
|
||||
name=name,
|
||||
image_id=image_id,
|
||||
|
|
@ -428,23 +534,37 @@ class AutoScalingBackend(BaseBackend):
|
|||
def describe_launch_configurations(self, names):
|
||||
configurations = self.launch_configurations.values()
|
||||
if names:
|
||||
return [configuration for configuration in configurations if configuration.name in names]
|
||||
return [
|
||||
configuration
|
||||
for configuration in configurations
|
||||
if configuration.name in names
|
||||
]
|
||||
else:
|
||||
return list(configurations)
|
||||
|
||||
def delete_launch_configuration(self, launch_configuration_name):
|
||||
self.launch_configurations.pop(launch_configuration_name, None)
|
||||
|
||||
def create_auto_scaling_group(self, name, availability_zones,
|
||||
desired_capacity, max_size, min_size,
|
||||
launch_config_name, vpc_zone_identifier,
|
||||
default_cooldown, health_check_period,
|
||||
health_check_type, load_balancers,
|
||||
target_group_arns, placement_group,
|
||||
termination_policies, tags,
|
||||
new_instances_protected_from_scale_in=False,
|
||||
instance_id=None):
|
||||
|
||||
def create_auto_scaling_group(
|
||||
self,
|
||||
name,
|
||||
availability_zones,
|
||||
desired_capacity,
|
||||
max_size,
|
||||
min_size,
|
||||
launch_config_name,
|
||||
vpc_zone_identifier,
|
||||
default_cooldown,
|
||||
health_check_period,
|
||||
health_check_type,
|
||||
load_balancers,
|
||||
target_group_arns,
|
||||
placement_group,
|
||||
termination_policies,
|
||||
tags,
|
||||
new_instances_protected_from_scale_in=False,
|
||||
instance_id=None,
|
||||
):
|
||||
def make_int(value):
|
||||
return int(value) if value is not None else value
|
||||
|
||||
|
|
@ -460,7 +580,9 @@ class AutoScalingBackend(BaseBackend):
|
|||
try:
|
||||
instance = self.ec2_backend.get_instance(instance_id)
|
||||
launch_config_name = name
|
||||
FakeLaunchConfiguration.create_from_instance(launch_config_name, instance, self)
|
||||
FakeLaunchConfiguration.create_from_instance(
|
||||
launch_config_name, instance, self
|
||||
)
|
||||
except InvalidInstanceIdError:
|
||||
raise InvalidInstanceError(instance_id)
|
||||
|
||||
|
|
@ -489,19 +611,37 @@ class AutoScalingBackend(BaseBackend):
|
|||
self.update_attached_target_groups(group.name)
|
||||
return group
|
||||
|
||||
def update_auto_scaling_group(self, name, availability_zones,
|
||||
desired_capacity, max_size, min_size,
|
||||
launch_config_name, vpc_zone_identifier,
|
||||
default_cooldown, health_check_period,
|
||||
health_check_type, placement_group,
|
||||
termination_policies,
|
||||
new_instances_protected_from_scale_in=None):
|
||||
def update_auto_scaling_group(
|
||||
self,
|
||||
name,
|
||||
availability_zones,
|
||||
desired_capacity,
|
||||
max_size,
|
||||
min_size,
|
||||
launch_config_name,
|
||||
vpc_zone_identifier,
|
||||
default_cooldown,
|
||||
health_check_period,
|
||||
health_check_type,
|
||||
placement_group,
|
||||
termination_policies,
|
||||
new_instances_protected_from_scale_in=None,
|
||||
):
|
||||
group = self.autoscaling_groups[name]
|
||||
group.update(availability_zones, desired_capacity, max_size,
|
||||
min_size, launch_config_name, vpc_zone_identifier,
|
||||
default_cooldown, health_check_period, health_check_type,
|
||||
placement_group, termination_policies,
|
||||
new_instances_protected_from_scale_in=new_instances_protected_from_scale_in)
|
||||
group.update(
|
||||
availability_zones,
|
||||
desired_capacity,
|
||||
max_size,
|
||||
min_size,
|
||||
launch_config_name,
|
||||
vpc_zone_identifier,
|
||||
default_cooldown,
|
||||
health_check_period,
|
||||
health_check_type,
|
||||
placement_group,
|
||||
termination_policies,
|
||||
new_instances_protected_from_scale_in=new_instances_protected_from_scale_in,
|
||||
)
|
||||
return group
|
||||
|
||||
def describe_auto_scaling_groups(self, names):
|
||||
|
|
@ -537,32 +677,48 @@ class AutoScalingBackend(BaseBackend):
|
|||
for x in instance_ids
|
||||
]
|
||||
for instance in new_instances:
|
||||
self.ec2_backend.create_tags([instance.instance.id], {ASG_NAME_TAG: group.name})
|
||||
self.ec2_backend.create_tags(
|
||||
[instance.instance.id], {ASG_NAME_TAG: group.name}
|
||||
)
|
||||
group.instance_states.extend(new_instances)
|
||||
self.update_attached_elbs(group.name)
|
||||
|
||||
def set_instance_health(self, instance_id, health_status, should_respect_grace_period):
|
||||
def set_instance_health(
|
||||
self, instance_id, health_status, should_respect_grace_period
|
||||
):
|
||||
instance = self.ec2_backend.get_instance(instance_id)
|
||||
instance_state = next(instance_state for group in self.autoscaling_groups.values()
|
||||
for instance_state in group.instance_states if instance_state.instance.id == instance.id)
|
||||
instance_state = next(
|
||||
instance_state
|
||||
for group in self.autoscaling_groups.values()
|
||||
for instance_state in group.instance_states
|
||||
if instance_state.instance.id == instance.id
|
||||
)
|
||||
instance_state.health_status = health_status
|
||||
|
||||
def detach_instances(self, group_name, instance_ids, should_decrement):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
original_size = len(group.instance_states)
|
||||
|
||||
detached_instances = [x for x in group.instance_states if x.instance.id in instance_ids]
|
||||
detached_instances = [
|
||||
x for x in group.instance_states if x.instance.id in instance_ids
|
||||
]
|
||||
for instance in detached_instances:
|
||||
self.ec2_backend.delete_tags([instance.instance.id], {ASG_NAME_TAG: group.name})
|
||||
self.ec2_backend.delete_tags(
|
||||
[instance.instance.id], {ASG_NAME_TAG: group.name}
|
||||
)
|
||||
|
||||
new_instance_state = [x for x in group.instance_states if x.instance.id not in instance_ids]
|
||||
new_instance_state = [
|
||||
x for x in group.instance_states if x.instance.id not in instance_ids
|
||||
]
|
||||
group.instance_states = new_instance_state
|
||||
|
||||
if should_decrement:
|
||||
group.desired_capacity = original_size - len(instance_ids)
|
||||
else:
|
||||
count_needed = len(instance_ids)
|
||||
group.replace_autoscaling_group_instances(count_needed, group.get_propagated_tags())
|
||||
group.replace_autoscaling_group_instances(
|
||||
count_needed, group.get_propagated_tags()
|
||||
)
|
||||
|
||||
self.update_attached_elbs(group_name)
|
||||
return detached_instances
|
||||
|
|
@ -593,19 +749,32 @@ class AutoScalingBackend(BaseBackend):
|
|||
desired_capacity = int(desired_capacity)
|
||||
self.set_desired_capacity(group_name, desired_capacity)
|
||||
|
||||
def create_autoscaling_policy(self, name, policy_type, adjustment_type, as_name,
|
||||
scaling_adjustment, cooldown):
|
||||
policy = FakeScalingPolicy(name, policy_type, adjustment_type, as_name,
|
||||
scaling_adjustment, cooldown, self)
|
||||
def create_autoscaling_policy(
|
||||
self, name, policy_type, adjustment_type, as_name, scaling_adjustment, cooldown
|
||||
):
|
||||
policy = FakeScalingPolicy(
|
||||
name,
|
||||
policy_type,
|
||||
adjustment_type,
|
||||
as_name,
|
||||
scaling_adjustment,
|
||||
cooldown,
|
||||
self,
|
||||
)
|
||||
|
||||
self.policies[name] = policy
|
||||
return policy
|
||||
|
||||
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 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)
|
||||
|
|
@ -616,16 +785,14 @@ class AutoScalingBackend(BaseBackend):
|
|||
|
||||
def update_attached_elbs(self, group_name):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
group_instance_ids = set(
|
||||
state.instance.id for state in group.instance_states)
|
||||
group_instance_ids = set(state.instance.id for state in group.instance_states)
|
||||
|
||||
# skip this if group.load_balancers is empty
|
||||
# otherwise elb_backend.describe_load_balancers returns all available load balancers
|
||||
if not group.load_balancers:
|
||||
return
|
||||
try:
|
||||
elbs = self.elb_backend.describe_load_balancers(
|
||||
names=group.load_balancers)
|
||||
elbs = self.elb_backend.describe_load_balancers(names=group.load_balancers)
|
||||
except LoadBalancerNotFoundError:
|
||||
# ELBs can be deleted before their autoscaling group
|
||||
return
|
||||
|
|
@ -633,14 +800,15 @@ class AutoScalingBackend(BaseBackend):
|
|||
for elb in elbs:
|
||||
elb_instace_ids = set(elb.instance_ids)
|
||||
self.elb_backend.register_instances(
|
||||
elb.name, group_instance_ids - elb_instace_ids)
|
||||
elb.name, group_instance_ids - elb_instace_ids
|
||||
)
|
||||
self.elb_backend.deregister_instances(
|
||||
elb.name, elb_instace_ids - group_instance_ids)
|
||||
elb.name, elb_instace_ids - group_instance_ids
|
||||
)
|
||||
|
||||
def update_attached_target_groups(self, group_name):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
group_instance_ids = set(
|
||||
state.instance.id for state in group.instance_states)
|
||||
group_instance_ids = set(state.instance.id for state in group.instance_states)
|
||||
|
||||
# no action necessary if target_group_arns is empty
|
||||
if not group.target_group_arns:
|
||||
|
|
@ -649,10 +817,13 @@ class AutoScalingBackend(BaseBackend):
|
|||
target_groups = self.elbv2_backend.describe_target_groups(
|
||||
target_group_arns=group.target_group_arns,
|
||||
load_balancer_arn=None,
|
||||
names=None)
|
||||
names=None,
|
||||
)
|
||||
|
||||
for target_group in target_groups:
|
||||
asg_targets = [{'id': x, 'port': target_group.port} for x in group_instance_ids]
|
||||
asg_targets = [
|
||||
{"id": x, "port": target_group.port} for x in group_instance_ids
|
||||
]
|
||||
self.elbv2_backend.register_targets(target_group.arn, (asg_targets))
|
||||
|
||||
def create_or_update_tags(self, tags):
|
||||
|
|
@ -670,7 +841,7 @@ class AutoScalingBackend(BaseBackend):
|
|||
new_tags.append(old_tag)
|
||||
|
||||
# if key was never in old_tag's add it (create tag)
|
||||
if not any(new_tag['key'] == tag['key'] for new_tag in new_tags):
|
||||
if not any(new_tag["key"] == tag["key"] for new_tag in new_tags):
|
||||
new_tags.append(tag)
|
||||
|
||||
group.tags = new_tags
|
||||
|
|
@ -678,7 +849,8 @@ class AutoScalingBackend(BaseBackend):
|
|||
def attach_load_balancers(self, group_name, load_balancer_names):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
group.load_balancers.extend(
|
||||
[x for x in load_balancer_names if x not in group.load_balancers])
|
||||
[x for x in load_balancer_names if x not in group.load_balancers]
|
||||
)
|
||||
self.update_attached_elbs(group_name)
|
||||
|
||||
def describe_load_balancers(self, group_name):
|
||||
|
|
@ -686,13 +858,13 @@ class AutoScalingBackend(BaseBackend):
|
|||
|
||||
def detach_load_balancers(self, group_name, load_balancer_names):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
group_instance_ids = set(
|
||||
state.instance.id for state in group.instance_states)
|
||||
group_instance_ids = set(state.instance.id for state in group.instance_states)
|
||||
elbs = self.elb_backend.describe_load_balancers(names=group.load_balancers)
|
||||
for elb in elbs:
|
||||
self.elb_backend.deregister_instances(
|
||||
elb.name, group_instance_ids)
|
||||
group.load_balancers = [x for x in group.load_balancers if x not in load_balancer_names]
|
||||
self.elb_backend.deregister_instances(elb.name, group_instance_ids)
|
||||
group.load_balancers = [
|
||||
x for x in group.load_balancers if x not in load_balancer_names
|
||||
]
|
||||
|
||||
def attach_load_balancer_target_groups(self, group_name, target_group_arns):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
|
|
@ -704,36 +876,51 @@ class AutoScalingBackend(BaseBackend):
|
|||
|
||||
def detach_load_balancer_target_groups(self, group_name, target_group_arns):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
group.target_group_arns = [x for x in group.target_group_arns if x not in target_group_arns]
|
||||
group.target_group_arns = [
|
||||
x for x in group.target_group_arns if x not in target_group_arns
|
||||
]
|
||||
for target_group in target_group_arns:
|
||||
asg_targets = [{'id': x.instance.id} for x in group.instance_states]
|
||||
asg_targets = [{"id": x.instance.id} for x in group.instance_states]
|
||||
self.elbv2_backend.deregister_targets(target_group, (asg_targets))
|
||||
|
||||
def suspend_processes(self, group_name, scaling_processes):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
group.suspended_processes = scaling_processes or []
|
||||
|
||||
def set_instance_protection(self, group_name, instance_ids, protected_from_scale_in):
|
||||
def set_instance_protection(
|
||||
self, group_name, instance_ids, protected_from_scale_in
|
||||
):
|
||||
group = self.autoscaling_groups[group_name]
|
||||
protected_instances = [
|
||||
x for x in group.instance_states if x.instance.id in instance_ids]
|
||||
x for x in group.instance_states if x.instance.id in instance_ids
|
||||
]
|
||||
for instance in protected_instances:
|
||||
instance.protected_from_scale_in = protected_from_scale_in
|
||||
|
||||
def notify_terminate_instances(self, instance_ids):
|
||||
for autoscaling_group_name, autoscaling_group in self.autoscaling_groups.items():
|
||||
for (
|
||||
autoscaling_group_name,
|
||||
autoscaling_group,
|
||||
) in self.autoscaling_groups.items():
|
||||
original_instance_count = len(autoscaling_group.instance_states)
|
||||
autoscaling_group.instance_states = list(filter(
|
||||
lambda i_state: i_state.instance.id not in instance_ids,
|
||||
autoscaling_group.instance_states = list(
|
||||
filter(
|
||||
lambda i_state: i_state.instance.id not in instance_ids,
|
||||
autoscaling_group.instance_states,
|
||||
)
|
||||
)
|
||||
difference = original_instance_count - len(
|
||||
autoscaling_group.instance_states
|
||||
))
|
||||
difference = original_instance_count - len(autoscaling_group.instance_states)
|
||||
)
|
||||
if difference > 0:
|
||||
autoscaling_group.replace_autoscaling_group_instances(difference, autoscaling_group.get_propagated_tags())
|
||||
autoscaling_group.replace_autoscaling_group_instances(
|
||||
difference, autoscaling_group.get_propagated_tags()
|
||||
)
|
||||
self.update_attached_elbs(autoscaling_group_name)
|
||||
|
||||
|
||||
autoscaling_backends = {}
|
||||
for region, ec2_backend in ec2_backends.items():
|
||||
autoscaling_backends[region] = AutoScalingBackend(
|
||||
ec2_backend, elb_backends[region], elbv2_backends[region])
|
||||
ec2_backend, elb_backends[region], elbv2_backends[region]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -6,88 +6,88 @@ 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_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'),
|
||||
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')
|
||||
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')
|
||||
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]
|
||||
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)
|
||||
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)
|
||||
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'),
|
||||
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)
|
||||
"NewInstancesProtectedFromScaleIn", False
|
||||
),
|
||||
)
|
||||
template = self.response_template(CREATE_AUTOSCALING_GROUP_TEMPLATE)
|
||||
return template.render()
|
||||
|
|
@ -95,68 +95,73 @@ class AutoScalingResponse(BaseResponse):
|
|||
@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)
|
||||
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')
|
||||
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]')
|
||||
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)
|
||||
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':
|
||||
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)
|
||||
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')
|
||||
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)
|
||||
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')
|
||||
group_name = self._get_param("AutoScalingGroupName")
|
||||
target_group_arns = self.autoscaling_backend.describe_load_balancer_target_groups(
|
||||
group_name)
|
||||
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')
|
||||
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)
|
||||
group_name, target_group_arns
|
||||
)
|
||||
template = self.response_template(DETACH_LOAD_BALANCER_TARGET_GROUPS_TEMPLATE)
|
||||
return template.render()
|
||||
|
||||
|
|
@ -172,7 +177,7 @@ class AutoScalingResponse(BaseResponse):
|
|||
max_records = self._get_int_param("MaxRecords", 50)
|
||||
if max_records > 100:
|
||||
raise ValueError
|
||||
groups = all_groups[start:start + max_records]
|
||||
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
|
||||
|
|
@ -181,42 +186,40 @@ class AutoScalingResponse(BaseResponse):
|
|||
|
||||
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'),
|
||||
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)
|
||||
"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')
|
||||
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)
|
||||
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')
|
||||
tags = self._get_list_prefix("Tags.member")
|
||||
|
||||
self.autoscaling_backend.create_or_update_tags(tags)
|
||||
template = self.response_template(UPDATE_AUTOSCALING_GROUP_TEMPLATE)
|
||||
|
|
@ -224,38 +227,38 @@ class AutoScalingResponse(BaseResponse):
|
|||
|
||||
def describe_auto_scaling_instances(self):
|
||||
instance_states = self.autoscaling_backend.describe_auto_scaling_instances()
|
||||
template = self.response_template(
|
||||
DESCRIBE_AUTOSCALING_INSTANCES_TEMPLATE)
|
||||
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'),
|
||||
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'))
|
||||
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')
|
||||
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')
|
||||
group_name = self._get_param("PolicyName")
|
||||
self.autoscaling_backend.execute_policy(group_name)
|
||||
template = self.response_template(EXECUTE_POLICY_TEMPLATE)
|
||||
return template.render()
|
||||
|
|
@ -263,17 +266,16 @@ class AutoScalingResponse(BaseResponse):
|
|||
@amz_crc32
|
||||
@amzn_request_id
|
||||
def attach_load_balancers(self):
|
||||
group_name = self._get_param('AutoScalingGroupName')
|
||||
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)
|
||||
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')
|
||||
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)
|
||||
|
|
@ -281,26 +283,28 @@ class AutoScalingResponse(BaseResponse):
|
|||
@amz_crc32
|
||||
@amzn_request_id
|
||||
def detach_load_balancers(self):
|
||||
group_name = self._get_param('AutoScalingGroupName')
|
||||
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)
|
||||
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)
|
||||
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')
|
||||
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)
|
||||
group_name, instance_ids, protected_from_scale_in
|
||||
)
|
||||
template = self.response_template(SET_INSTANCE_PROTECTION_TEMPLATE)
|
||||
return template.render()
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
from .responses import AutoScalingResponse
|
||||
|
||||
url_bases = [
|
||||
"https?://autoscaling.(.+).amazonaws.com",
|
||||
]
|
||||
url_bases = ["https?://autoscaling.(.+).amazonaws.com"]
|
||||
|
||||
url_paths = {
|
||||
'{0}/$': AutoScalingResponse.dispatch,
|
||||
}
|
||||
url_paths = {"{0}/$": AutoScalingResponse.dispatch}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue