EC2 - Error when passing in TagSpec with 0 tags (#5216)

This commit is contained in:
Bert Blommers 2022-06-12 10:15:25 +00:00 committed by GitHub
parent 67cda6d7d6
commit e15c16eaed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 0 deletions

View File

@ -461,6 +461,13 @@ class InvalidParameterValueError(EC2ClientError):
)
class EmptyTagSpecError(EC2ClientError):
def __init__(self):
super().__init__(
"InvalidParameterValue", "Tag specification must have at least one tag"
)
class InvalidParameterValueErrorTagNull(EC2ClientError):
def __init__(self):
super().__init__(

View File

@ -1,4 +1,5 @@
from moto.core.responses import BaseResponse
from ..exceptions import EmptyTagSpecError
from ..utils import convert_tag_spec
@ -12,5 +13,10 @@ class EC2BaseResponse(BaseResponse):
def _parse_tag_specification(self):
# [{"ResourceType": _type, "Tag": [{"Key": k, "Value": v}, ..]}]
tag_spec_set = self._get_multi_param("TagSpecification")
# If we do not pass any Tags, this method will convert this to [_type] instead
if isinstance(tag_spec_set, list) and any(
[isinstance(spec, str) for spec in tag_spec_set]
):
raise EmptyTagSpecError
# {_type: {k: v, ..}}
return convert_tag_spec(tag_spec_set)

View File

@ -401,6 +401,26 @@ def test_create_snapshot_with_tags():
assert snapshot["Tags"] == expected_tags
@mock_ec2
def test_create_volume_without_tags():
client = boto3.client("ec2", "us-east-1")
with pytest.raises(ClientError) as exc:
client.create_volume(
AvailabilityZone="us-east-1a",
Encrypted=False,
Size=40,
TagSpecifications=[
{
"ResourceType": "volume",
"Tags": [],
}
],
)
err = exc.value.response["Error"]
err["Code"].should.equal("InvalidParameterValue")
err["Message"].should.equal("Tag specification must have at least one tag")
@mock_ec2
def test_create_tag_empty_resource():
# create ec2 client in us-west-1