diff --git a/moto/ec2/models.py b/moto/ec2/models.py index d39606ec5..d32b9a795 100644 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1134,7 +1134,7 @@ class InstanceTypeBackend(object): def describe_instance_types(self, instance_types=None): matches = INSTANCE_TYPES.values() if instance_types: - matches = [t for t in matches if t.get('apiname') in instance_types] + matches = [t for t in matches if t.get("apiname") in instance_types] if len(instance_types) > len(matches): unknown_ids = set(instance_types) - set(matches) raise InvalidInstanceTypeError(unknown_ids) @@ -1153,14 +1153,15 @@ class InstanceTypeOfferingBackend(object): for key, values in filters.items(): if key == "location": if location_type in ("availability-zone", "availability-zone-id"): - return offering.get('location') in values + return offering.get("location") in values elif location_type == "region": - return any(v for v in values - if offering.get('location').startswith(v)) + return any( + v for v in values if offering.get("location").startswith(v) + ) else: return False elif key == "instance-type": - return offering.get('instance_type') in values + return offering.get("instance_type") in values else: return False return True diff --git a/moto/ec2/responses/instances.py b/moto/ec2/responses/instances.py index d3df45ed4..4d2331c67 100644 --- a/moto/ec2/responses/instances.py +++ b/moto/ec2/responses/instances.py @@ -152,7 +152,8 @@ class InstanceResponse(BaseResponse): location_type_filters = self._get_multi_param("LocationType") filter_dict = filters_from_querystring(self.querystring) offerings = self.ec2_backend.describe_instance_type_offerings( - location_type_filters, filter_dict) + location_type_filters, filter_dict + ) template = self.response_template(EC2_DESCRIBE_INSTANCE_TYPE_OFFERINGS) return template.render(instance_type_offerings=offerings) diff --git a/tests/test_ec2/test_instance_type_offerings.py b/tests/test_ec2/test_instance_type_offerings.py index 2b33f55df..e75b21af1 100644 --- a/tests/test_ec2/test_instance_type_offerings.py +++ b/tests/test_ec2/test_instance_type_offerings.py @@ -22,37 +22,41 @@ def test_describe_instance_type_offerings(): def test_describe_instance_type_offering_filter_by_type(): client = boto3.client("ec2", "us-east-1") offerings = client.describe_instance_type_offerings( - Filters=[{"Name": "instance-type", "Values": ["t2.nano"]}]) + Filters=[{"Name": "instance-type", "Values": ["t2.nano"]}] + ) offerings.should.have.key("InstanceTypeOfferings") offerings["InstanceTypeOfferings"].should_not.be.empty offerings["InstanceTypeOfferings"].should.have.length_of(2) - offerings["InstanceTypeOfferings"][0]['InstanceType'].should.equal('t2.nano') - offerings["InstanceTypeOfferings"][-1]['InstanceType'].should.equal('t2.nano') - offerings["InstanceTypeOfferings"][0]['Location'].should.equal('us-east-1a') + offerings["InstanceTypeOfferings"][0]["InstanceType"].should.equal("t2.nano") + offerings["InstanceTypeOfferings"][-1]["InstanceType"].should.equal("t2.nano") + offerings["InstanceTypeOfferings"][0]["Location"].should.equal("us-east-1a") @mock_ec2 def test_describe_instance_type_offering_filter_by_zone(): client = boto3.client("ec2", "us-east-1") offerings = client.describe_instance_type_offerings( - Filters=[{"Name": "location", "Values": ["us-east-1c"]}]) + LocationType="availability-zone", + Filters=[{"Name": "location", "Values": ["us-east-1c"]}], + ) offerings.should.have.key("InstanceTypeOfferings") offerings["InstanceTypeOfferings"].should_not.be.empty offerings["InstanceTypeOfferings"].should.have.length_of(1) - offerings["InstanceTypeOfferings"][0]['InstanceType'].should.equal('a1.2xlarge') - offerings["InstanceTypeOfferings"][0]['Location'].should.equal('us-east-1c') + offerings["InstanceTypeOfferings"][0]["InstanceType"].should.equal("a1.2xlarge") + offerings["InstanceTypeOfferings"][0]["Location"].should.equal("us-east-1c") @mock_ec2 def test_describe_instance_type_offering_filter_by_region(): client = boto3.client("ec2", "us-east-1") offerings = client.describe_instance_type_offerings( - LocationType="region", Filters=[{"Name": "location", "Values": ["us-west-1"]}]) + LocationType="region", Filters=[{"Name": "location", "Values": ["us-west-1"]}] + ) offerings.should.have.key("InstanceTypeOfferings") offerings["InstanceTypeOfferings"].should_not.be.empty offerings["InstanceTypeOfferings"].should.have.length_of(2) - offerings["InstanceTypeOfferings"][0]['InstanceType'].should.equal('a1.4xlarge') - offerings["InstanceTypeOfferings"][0]['Location'].should.equal('us-west-1a') + offerings["InstanceTypeOfferings"][0]["InstanceType"].should.equal("a1.4xlarge") + offerings["InstanceTypeOfferings"][0]["Location"].should.equal("us-west-1a") diff --git a/tests/test_ec2/test_instance_types.py b/tests/test_ec2/test_instance_types.py index 9f1205c9d..6028b0e4d 100644 --- a/tests/test_ec2/test_instance_types.py +++ b/tests/test_ec2/test_instance_types.py @@ -24,13 +24,19 @@ def test_describe_instance_types(): @mock_ec2 def test_describe_instance_types_filter_by_type(): client = boto3.client("ec2", "us-east-1") - instance_types = client.describe_instance_types(InstanceTypes=['t1.micro', 't2.nano']) + instance_types = client.describe_instance_types( + InstanceTypes=["t1.micro", "t2.nano"] + ) instance_types.should.have.key("InstanceTypes") instance_types["InstanceTypes"].should_not.be.empty instance_types["InstanceTypes"].should.have.length_of(2) - instance_types["InstanceTypes"][0]['InstanceType'].should.equal('t1.micro') - instance_types["InstanceTypes"][1]['InstanceType'].should.equal('t2.nano') + instance_types["InstanceTypes"][0]["InstanceType"].should.be.within( + ["t1.micro", "t2.nano"] + ) + instance_types["InstanceTypes"][1]["InstanceType"].should.be.within( + ["t1.micro", "t2.nano"] + ) @mock_ec2 @@ -38,7 +44,7 @@ def test_describe_instance_types_unknown_type(): client = boto3.client("ec2", "us-east-1") with pytest.raises(ClientError) as err: - client.describe_instance_types(InstanceTypes=['t1.non_existent']) + client.describe_instance_types(InstanceTypes=["t1.non_existent"]) err.response["Error"]["Code"].should.equal("ValidationException") err.response["Error"]["Message"].split(":")[0].should.look_like( "The instance type '{'t1.non_existent'}' does not exist"