Allow tagging snapshots on creation
Largely copying what was done for volume creation in https://github.com/spulec/moto/pull/1432
This commit is contained in:
parent
4184acc0d2
commit
e2af8bc836
@ -23,8 +23,11 @@ class ElasticBlockStore(BaseResponse):
|
|||||||
def create_snapshot(self):
|
def create_snapshot(self):
|
||||||
volume_id = self._get_param('VolumeId')
|
volume_id = self._get_param('VolumeId')
|
||||||
description = self._get_param('Description')
|
description = self._get_param('Description')
|
||||||
|
tags = self._parse_tag_specification("TagSpecification")
|
||||||
|
snapshot_tags = tags.get('snapshot', {})
|
||||||
if self.is_not_dryrun('CreateSnapshot'):
|
if self.is_not_dryrun('CreateSnapshot'):
|
||||||
snapshot = self.ec2_backend.create_snapshot(volume_id, description)
|
snapshot = self.ec2_backend.create_snapshot(volume_id, description)
|
||||||
|
snapshot.add_tags(snapshot_tags)
|
||||||
template = self.response_template(CREATE_SNAPSHOT_RESPONSE)
|
template = self.response_template(CREATE_SNAPSHOT_RESPONSE)
|
||||||
return template.render(snapshot=snapshot)
|
return template.render(snapshot=snapshot)
|
||||||
|
|
||||||
@ -233,6 +236,16 @@ CREATE_SNAPSHOT_RESPONSE = """<CreateSnapshotResponse xmlns="http://ec2.amazonaw
|
|||||||
<volumeSize>{{ snapshot.volume.size }}</volumeSize>
|
<volumeSize>{{ snapshot.volume.size }}</volumeSize>
|
||||||
<description>{{ snapshot.description }}</description>
|
<description>{{ snapshot.description }}</description>
|
||||||
<encrypted>{{ snapshot.encrypted }}</encrypted>
|
<encrypted>{{ snapshot.encrypted }}</encrypted>
|
||||||
|
<tagSet>
|
||||||
|
{% for tag in snapshot.get_tags() %}
|
||||||
|
<item>
|
||||||
|
<resourceId>{{ tag.resource_id }}</resourceId>
|
||||||
|
<resourceType>{{ tag.resource_type }}</resourceType>
|
||||||
|
<key>{{ tag.key }}</key>
|
||||||
|
<value>{{ tag.value }}</value>
|
||||||
|
</item>
|
||||||
|
{% endfor %}
|
||||||
|
</tagSet>
|
||||||
</CreateSnapshotResponse>"""
|
</CreateSnapshotResponse>"""
|
||||||
|
|
||||||
DESCRIBE_SNAPSHOTS_RESPONSE = """<DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
|
DESCRIBE_SNAPSHOTS_RESPONSE = """<DescribeSnapshotsResponse xmlns="http://ec2.amazonaws.com/doc/2013-10-15/">
|
||||||
|
@ -409,3 +409,45 @@ def test_create_volume_with_tags():
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert response['Tags'][0]['Key'] == 'TEST_TAG'
|
assert response['Tags'][0]['Key'] == 'TEST_TAG'
|
||||||
|
|
||||||
|
|
||||||
|
@mock_ec2
|
||||||
|
def test_create_snapshot_with_tags():
|
||||||
|
client = boto3.client('ec2', 'us-west-2')
|
||||||
|
volume_id = client.create_volume(
|
||||||
|
AvailabilityZone='us-west-2',
|
||||||
|
Encrypted=False,
|
||||||
|
Size=40,
|
||||||
|
TagSpecifications=[
|
||||||
|
{
|
||||||
|
'ResourceType': 'volume',
|
||||||
|
'Tags': [
|
||||||
|
{
|
||||||
|
'Key': 'TEST_TAG',
|
||||||
|
'Value': 'TEST_VALUE'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)['VolumeId']
|
||||||
|
snapshot = client.create_snapshot(
|
||||||
|
VolumeId=volume_id,
|
||||||
|
TagSpecifications=[
|
||||||
|
{
|
||||||
|
'ResourceType': 'snapshot',
|
||||||
|
'Tags': [
|
||||||
|
{
|
||||||
|
'Key': 'TEST_SNAPSHOT_TAG',
|
||||||
|
'Value': 'TEST_SNAPSHOT_VALUE'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
expected_tags = [{
|
||||||
|
'Key': 'TEST_SNAPSHOT_TAG',
|
||||||
|
'Value': 'TEST_SNAPSHOT_VALUE'
|
||||||
|
}]
|
||||||
|
|
||||||
|
assert snapshot['Tags'] == expected_tags
|
||||||
|
Loading…
x
Reference in New Issue
Block a user