From 5d51329c34abf4312de89f74faa6a4e9dc224e7e Mon Sep 17 00:00:00 2001 From: Graham Lyons Date: Wed, 21 Mar 2018 15:55:58 +0000 Subject: [PATCH] Don't create volumes for AMIs (#1456) * Delete the volume used during AMI creation Creating an AMI doesn't actually result in the creation of an EBS volume, although the associated snapshot does reference one. To that end, delete the volume once we've used it. * Add `owner_id` to `Snapshot`, verify AMI snapshots The default AMIs which are created by moto have EBS volume mappings but the snapshots associated with those don't have the correct owners set. This adds the owner to the snapshot model and passes it through from the JSON data. --- moto/ec2/models.py | 14 ++++-- moto/ec2/responses/elastic_block_store.py | 4 +- tests/test_ec2/test_amis.py | 56 +++++++++++++++++------ 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index b1989d67..51ef2a12 100755 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -1088,7 +1088,8 @@ class Ami(TaggedEC2Resource): # AWS auto-creates these, we should reflect the same. volume = self.ec2_backend.create_volume(15, region_name) self.ebs_snapshot = self.ec2_backend.create_snapshot( - volume.id, "Auto-created snapshot for AMI %s" % self.id) + volume.id, "Auto-created snapshot for AMI %s" % self.id, owner_id) + self.ec2_backend.delete_volume(volume.id) @property def is_public(self): @@ -1840,7 +1841,7 @@ class Volume(TaggedEC2Resource): class Snapshot(TaggedEC2Resource): - def __init__(self, ec2_backend, snapshot_id, volume, description, encrypted=False): + def __init__(self, ec2_backend, snapshot_id, volume, description, encrypted=False, owner_id='123456789012'): self.id = snapshot_id self.volume = volume self.description = description @@ -1849,6 +1850,7 @@ class Snapshot(TaggedEC2Resource): self.ec2_backend = ec2_backend self.status = 'completed' self.encrypted = encrypted + self.owner_id = owner_id def get_filter_value(self, filter_name): if filter_name == 'description': @@ -1940,11 +1942,13 @@ class EBSBackend(object): volume.attachment = None return old_attachment - def create_snapshot(self, volume_id, description): + def create_snapshot(self, volume_id, description, owner_id=None): snapshot_id = random_snapshot_id() volume = self.get_volume(volume_id) - snapshot = Snapshot(self, snapshot_id, volume, - description, volume.encrypted) + params = [self, snapshot_id, volume, description, volume.encrypted] + if owner_id: + params.append(owner_id) + snapshot = Snapshot(*params) self.snapshots[snapshot_id] = snapshot return snapshot diff --git a/moto/ec2/responses/elastic_block_store.py b/moto/ec2/responses/elastic_block_store.py index 31831c18..2d43f8ff 100644 --- a/moto/ec2/responses/elastic_block_store.py +++ b/moto/ec2/responses/elastic_block_store.py @@ -229,7 +229,7 @@ CREATE_SNAPSHOT_RESPONSE = """