From a44b7e7f5c114c9a48131abbeef88422f28669d1 Mon Sep 17 00:00:00 2001 From: Phil Christensen Date: Fri, 9 Feb 2018 14:49:40 -0500 Subject: [PATCH] implementing mockable EBS snapshot copies --- moto/ec2/models.py | 9 +++++++++ moto/ec2/responses/elastic_block_store.py | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index c94752ef6..31bfb4839 100755 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1971,6 +1971,15 @@ class EBSBackend(object): matches = generic_filter(filters, matches) return matches + def copy_snapshot(self, source_snapshot_id, source_region, description=None): + source_snapshot = ec2_backends[source_region].describe_snapshots( + snapshot_ids=[source_snapshot_id])[0] + snapshot_id = random_snapshot_id() + snapshot = Snapshot(self, snapshot_id, volume=source_snapshot.volume, + description=description, encrypted=source_snapshot.encrypted) + self.snapshots[snapshot_id] = snapshot + return snapshot + def get_snapshot(self, snapshot_id): snapshot = self.snapshots.get(snapshot_id, None) if not snapshot: diff --git a/moto/ec2/responses/elastic_block_store.py b/moto/ec2/responses/elastic_block_store.py index cdc5b18e9..aa0d7f73b 100644 --- a/moto/ec2/responses/elastic_block_store.py +++ b/moto/ec2/responses/elastic_block_store.py @@ -16,9 +16,14 @@ class ElasticBlockStore(BaseResponse): return template.render(attachment=attachment) def copy_snapshot(self): + source_snapshot_id = self._get_param('SourceSnapshotId') + source_region = self._get_param('SourceRegion') + description = self._get_param('Description') if self.is_not_dryrun('CopySnapshot'): - raise NotImplementedError( - 'ElasticBlockStore.copy_snapshot is not yet implemented') + snapshot = self.ec2_backend.copy_snapshot( + source_snapshot_id, source_region, description) + template = self.response_template(COPY_SNAPSHOT_RESPONSE) + return template.render(snapshot=snapshot) def create_snapshot(self): volume_id = self._get_param('VolumeId') @@ -248,6 +253,11 @@ CREATE_SNAPSHOT_RESPONSE = """ + 59dbff89-35bd-4eac-99ed-be587EXAMPLE + {{ snapshot.id }} +""" + DESCRIBE_SNAPSHOTS_RESPONSE = """ 59dbff89-35bd-4eac-99ed-be587EXAMPLE