Skip to content

Commit

Permalink
EC2: create_fleet() now correctly handles Overrides with a single val…
Browse files Browse the repository at this point in the history
…ue (#8316)
  • Loading branch information
bblommers authored Nov 15, 2024
1 parent 962d367 commit 10bdd08
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions moto/ec2/responses/fleets.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ def create_fleet(self) -> str:
launch_template_configs = self._get_multi_param(
param_prefix="LaunchTemplateConfigs"
)
# Our multi_param function doesn't understand when to return a list, a string, or a dictionary
# So we'll have to help it a little, by explicitly asking for a dict if we're getting anything else back
for idx, config in enumerate(launch_template_configs, start=1):
if (overrides := config.get("Overrides")) and not isinstance(
overrides, dict
):
config["Overrides"] = self._get_multi_param(
f"LaunchTemplateConfigs.{idx}.Overrides",
skip_result_conversion=True,
)
if (spec := config.get("LaunchTemplateSpecification")) and not isinstance(
spec, dict
):
config["LaunchTemplateSpecification"] = self._get_multi_param_dict(
f"LaunchTemplateConfigs.{idx}.LaunchTemplateSpecification"
)

excess_capacity_termination_policy = self._get_param(
"ExcessCapacityTerminationPolicy"
Expand Down
29 changes: 29 additions & 0 deletions tests/test_ec2/test_fleets.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,35 @@ def test_create_fleet_request_with_tags():
assert tag in instance["Tags"]


@mock_aws
@pytest.mark.parametrize(
"overrides", [{"InstanceType": "t2.nano"}, {"AvailabilityZone": "us-west-1"}]
)
def test_create_fleet_using_launch_template_config__overrides_single(overrides):
conn = boto3.client("ec2", region_name="us-east-2")

template_id, _ = get_launch_template(conn, instance_type="t2.medium")

fleet_id = conn.create_fleet(
LaunchTemplateConfigs=[
{
"LaunchTemplateSpecification": {"LaunchTemplateId": template_id},
# Verify we parse the incoming parameters correctly,
# even when only supplying a dictionary with a single key
"Overrides": [overrides],
},
],
TargetCapacitySpecification={
"DefaultTargetCapacityType": "spot",
"TotalTargetCapacity": 1,
},
)["FleetId"]

fleet = conn.describe_fleets(FleetIds=[fleet_id])["Fleets"][0]
lt_config = fleet["LaunchTemplateConfigs"][0]
assert lt_config["Overrides"] == [overrides]


@mock_aws
def test_create_fleet_using_launch_template_config__overrides():
conn = boto3.client("ec2", region_name="us-east-2")
Expand Down

0 comments on commit 10bdd08

Please sign in to comment.