Add tagging to ec2.CopySnapshot (#3365)

The `@freeze_time` decorator was removed because it is not necessary (and was
causing the test to be skipped).

Closes #2940
This commit is contained in:
Brian Pandola 2020-10-09 04:33:07 -07:00 committed by GitHub
parent c26bef6f79
commit d00cefa25c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions

View File

@ -19,10 +19,13 @@ class ElasticBlockStore(BaseResponse):
source_snapshot_id = self._get_param("SourceSnapshotId") source_snapshot_id = self._get_param("SourceSnapshotId")
source_region = self._get_param("SourceRegion") source_region = self._get_param("SourceRegion")
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("CopySnapshot"): if self.is_not_dryrun("CopySnapshot"):
snapshot = self.ec2_backend.copy_snapshot( snapshot = self.ec2_backend.copy_snapshot(
source_snapshot_id, source_region, description source_snapshot_id, source_region, description
) )
snapshot.add_tags(snapshot_tags)
template = self.response_template(COPY_SNAPSHOT_RESPONSE) template = self.response_template(COPY_SNAPSHOT_RESPONSE)
return template.render(snapshot=snapshot) return template.render(snapshot=snapshot)
@ -272,6 +275,16 @@ CREATE_SNAPSHOT_RESPONSE = """<CreateSnapshotResponse xmlns="http://ec2.amazonaw
COPY_SNAPSHOT_RESPONSE = """<CopySnapshotResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/"> COPY_SNAPSHOT_RESPONSE = """<CopySnapshotResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
<requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId> <requestId>59dbff89-35bd-4eac-99ed-be587EXAMPLE</requestId>
<snapshotId>{{ snapshot.id }}</snapshotId> <snapshotId>{{ snapshot.id }}</snapshotId>
<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>
</CopySnapshotResponse>""" </CopySnapshotResponse>"""
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/">

View File

@ -9,7 +9,6 @@ import boto
import boto3 import boto3
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
from boto.exception import EC2ResponseError from boto.exception import EC2ResponseError
from freezegun import freeze_time
import sure # noqa import sure # noqa
from moto import mock_ec2_deprecated, mock_ec2 from moto import mock_ec2_deprecated, mock_ec2
@ -834,22 +833,26 @@ def test_volume_property_hidden_when_no_tags_exist():
volume_response.get("Tags").should.equal(None) volume_response.get("Tags").should.equal(None)
@freeze_time
@mock_ec2 @mock_ec2
def test_copy_snapshot(): def test_copy_snapshot():
ec2_client = boto3.client("ec2", region_name="eu-west-1") ec2_client = boto3.client("ec2", region_name="eu-west-1")
dest_ec2_client = boto3.client("ec2", region_name="eu-west-2") dest_ec2_client = boto3.client("ec2", region_name="eu-west-2")
volume_response = ec2_client.create_volume(AvailabilityZone="eu-west-1a", Size=10) volume_response = ec2_client.create_volume(AvailabilityZone="eu-west-1a", Size=10)
tag_spec = [
{"ResourceType": "snapshot", "Tags": [{"Key": "key", "Value": "value"}]}
]
create_snapshot_response = ec2_client.create_snapshot( create_snapshot_response = ec2_client.create_snapshot(
VolumeId=volume_response["VolumeId"] VolumeId=volume_response["VolumeId"], TagSpecifications=tag_spec
) )
copy_snapshot_response = dest_ec2_client.copy_snapshot( copy_snapshot_response = dest_ec2_client.copy_snapshot(
SourceSnapshotId=create_snapshot_response["SnapshotId"], SourceSnapshotId=create_snapshot_response["SnapshotId"],
SourceRegion="eu-west-1", SourceRegion="eu-west-1",
TagSpecifications=tag_spec,
) )
copy_snapshot_response["Tags"].should.equal(tag_spec[0]["Tags"])
ec2 = boto3.resource("ec2", region_name="eu-west-1") ec2 = boto3.resource("ec2", region_name="eu-west-1")
dest_ec2 = boto3.resource("ec2", region_name="eu-west-2") dest_ec2 = boto3.resource("ec2", region_name="eu-west-2")