bugfix ebs volume tag behaviour

This commit modifies the response format of the ec2 calls
`create_volume` and `describe_volumes`. Previously, these calls would
always include a `Tags` key in the response, even when a volume has no tags.
Now, the `Tags` key will not be included in the response if the volume
has no tags.

When an EBS volume has no tags, calls to the aws ec2 endpoints `create_volume`
and `describe_volumes` do not include the `Tags` key in the
`response.Volumes[]` object.

However, moto does include the `Tags` key in this case. This discrepancy
in behaviour can result in code passing a moto test but failing in
production.

Sample snippets that trigger this condition:

```
def create_volume_and_then_get_tags_from_response():
    client = boto3.client('ec2', region_name='us-east-1')
    volume_response = client.create_volume(
        Size=10,
        AvailabilityZone='us-east-1a'
    )
    keys = volume_response['Keys']
```

```
def create_volume_and_then_get_tags_from_describe_volumes():
    client = boto3.client('ec2', region_name='us-east-1')
    volume_response = client.create_volume(
        Size=10,
        AvailabilityZone='us-east-1a'
    )
    volume_describe_response = client.describe_volumes()
    keys = volume_describe_response['Volumes'][0]['Keys']
```

Both sample snippets will succeed in a moto test, but fail with a
`KeyError` when using the aws api.
This commit is contained in:
Tay Frost 2019-03-07 17:07:15 -05:00
parent 15b3ede3cc
commit 7b236c4ded

View File

@ -150,6 +150,7 @@ CREATE_VOLUME_RESPONSE = """<CreateVolumeResponse xmlns="http://ec2.amazonaws.co
<availabilityZone>{{ volume.zone.name }}</availabilityZone> <availabilityZone>{{ volume.zone.name }}</availabilityZone>
<status>creating</status> <status>creating</status>
<createTime>{{ volume.create_time}}</createTime> <createTime>{{ volume.create_time}}</createTime>
{% if volume.get_tags() %}
<tagSet> <tagSet>
{% for tag in volume.get_tags() %} {% for tag in volume.get_tags() %}
<item> <item>
@ -160,6 +161,7 @@ CREATE_VOLUME_RESPONSE = """<CreateVolumeResponse xmlns="http://ec2.amazonaws.co
</item> </item>
{% endfor %} {% endfor %}
</tagSet> </tagSet>
{% endif %}
<volumeType>standard</volumeType> <volumeType>standard</volumeType>
</CreateVolumeResponse>""" </CreateVolumeResponse>"""
@ -191,6 +193,7 @@ DESCRIBE_VOLUMES_RESPONSE = """<DescribeVolumesResponse xmlns="http://ec2.amazon
</item> </item>
{% endif %} {% endif %}
</attachmentSet> </attachmentSet>
{% if volume.get_tags() %}
<tagSet> <tagSet>
{% for tag in volume.get_tags() %} {% for tag in volume.get_tags() %}
<item> <item>
@ -201,6 +204,7 @@ DESCRIBE_VOLUMES_RESPONSE = """<DescribeVolumesResponse xmlns="http://ec2.amazon
</item> </item>
{% endfor %} {% endfor %}
</tagSet> </tagSet>
{% endif %}
<volumeType>standard</volumeType> <volumeType>standard</volumeType>
</item> </item>
{% endfor %} {% endfor %}