From 5f61950096e2d33cfb42021d5f661a71ae192357 Mon Sep 17 00:00:00 2001 From: Hugo Lopes Tavares Date: Thu, 1 Feb 2018 17:09:10 -0500 Subject: [PATCH] Make SpotPrice optional when requesting a spot fleet When price is omitted, AWS will default to the on-demand price --- moto/ec2/models.py | 5 +- moto/ec2/responses/spot_fleets.py | 6 +- requirements-dev.txt | 2 +- .../test_cloudformation_stack_integration.py | 72 +++++++++++++++++++ tests/test_ec2/test_spot_fleet.py | 27 +++++++ 5 files changed, 108 insertions(+), 4 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index f877d3772..bfc672ed7 100755 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -2943,7 +2943,7 @@ class SpotFleetRequest(TaggedEC2Resource): 'Properties']['SpotFleetRequestConfigData'] ec2_backend = ec2_backends[region_name] - spot_price = properties['SpotPrice'] + spot_price = properties.get('SpotPrice') target_capacity = properties['TargetCapacity'] iam_fleet_role = properties['IamFleetRole'] allocation_strategy = properties['AllocationStrategy'] @@ -2977,7 +2977,8 @@ class SpotFleetRequest(TaggedEC2Resource): launch_spec_index += 1 else: # lowestPrice cheapest_spec = sorted( - self.launch_specs, key=lambda spec: float(spec.spot_price))[0] + # FIXME: change `+inf` to the on demand price scaled to weighted capacity when it's not present + self.launch_specs, key=lambda spec: float(spec.spot_price or '+inf'))[0] weight_so_far = weight_to_add + (weight_to_add % cheapest_spec.weighted_capacity) weight_map[cheapest_spec] = int( weight_so_far // cheapest_spec.weighted_capacity) diff --git a/moto/ec2/responses/spot_fleets.py b/moto/ec2/responses/spot_fleets.py index 81d1e0146..0366af9d6 100644 --- a/moto/ec2/responses/spot_fleets.py +++ b/moto/ec2/responses/spot_fleets.py @@ -40,7 +40,7 @@ class SpotFleets(BaseResponse): def request_spot_fleet(self): spot_config = self._get_dict_param("SpotFleetRequestConfig.") - spot_price = spot_config['spot_price'] + spot_price = spot_config.get('spot_price') target_capacity = spot_config['target_capacity'] iam_fleet_role = spot_config['iam_fleet_role'] allocation_strategy = spot_config['allocation_strategy'] @@ -78,7 +78,9 @@ DESCRIBE_SPOT_FLEET_TEMPLATE = """