EC2: validate pagination parameters (#6293)

This commit is contained in:
rafcio19 2023-05-09 16:47:15 +01:00 committed by GitHub
parent 87d84c12e0
commit 52846c9555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 5 deletions

View File

@ -14,6 +14,12 @@ from ._base_response import EC2BaseResponse
class InstanceResponse(EC2BaseResponse): class InstanceResponse(EC2BaseResponse):
def describe_instances(self) -> str: def describe_instances(self) -> str:
self.error_on_dryrun() self.error_on_dryrun()
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2/client/describe_instances.html
# You cannot specify this(MaxResults) parameter and the instance IDs parameter in the same request.
if "InstanceId.1" in self.data and "MaxResults" in self.data:
raise InvalidParameterCombination(
"The parameter instancesSet cannot be used with the parameter maxResults"
)
filter_dict = self._filters_from_querystring() filter_dict = self._filters_from_querystring()
instance_ids = self._get_multi_param("InstanceId") instance_ids = self._get_multi_param("InstanceId")
token = self._get_param("NextToken") token = self._get_param("NextToken")

View File

@ -318,23 +318,46 @@ def test_get_paginated_instances():
instances.extend( instances.extend(
conn.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1) conn.create_instances(ImageId=EXAMPLE_AMI_ID, MinCount=1, MaxCount=1)
) )
instance_ids = [i.id for i in instances]
resp1 = client.describe_instances(InstanceIds=instance_ids, MaxResults=5) resp1 = client.describe_instances(MaxResults=5)
res1 = resp1["Reservations"] res1 = resp1["Reservations"]
res1.should.have.length_of(5) res1.should.have.length_of(5)
next_token = resp1["NextToken"] next_token = resp1["NextToken"]
next_token.should_not.equal(None) next_token.should_not.equal(None)
resp2 = client.describe_instances(InstanceIds=instance_ids, NextToken=next_token) resp2 = client.describe_instances(NextToken=next_token)
resp2["Reservations"].should.have.length_of(7) # 12 total - 5 from the first call
assert "NextToken" not in resp2 # This is it - no more pages # at least 12 total - 5 from the first call but there may be more from servermode tests
assert len(resp2["Reservations"]) >= 7
for i in instances: for i in instances:
i.terminate() i.terminate()
@mock_ec2
def test_describe_instances_pagination_error():
client = boto3.client("ec2", region_name="us-east-1")
# Call describe_instances with a bad id should raise an error
with pytest.raises(ClientError) as ex:
paginator = client.get_paginator("describe_instances").paginate(
InstanceIds=["i-12345678"],
PaginationConfig={
"MaxItems": 9999,
"PageSize": 100,
},
)
for page in paginator:
dir(page)
assert ex.value.response["Error"]["Code"] == "InvalidParameterCombination"
assert (
ex.value.response["Error"]["Message"]
== "The parameter instancesSet cannot be used with the parameter maxResults"
)
@mock_ec2 @mock_ec2
def test_create_with_tags(): def test_create_with_tags():
ec2 = boto3.client("ec2", region_name="us-west-2") ec2 = boto3.client("ec2", region_name="us-west-2")