Skip to content

Commit

Permalink
Merge pull request bonclay7#75 from rickhlx/none-snapshots
Browse files Browse the repository at this point in the history
ignore snapshot ids with value of none
  • Loading branch information
bonclay7 authored Aug 17, 2018
2 parents cebe632 + dd845c8 commit 70627e5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
15 changes: 8 additions & 7 deletions amicleaner/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ def remove_amis(self, amis):
self.ec2.deregister_image(ImageId=ami.id)
print("{0} deregistered".format(ami.id))
for block_device in ami.block_device_mappings:
try:
self.ec2.delete_snapshot(
SnapshotId=block_device.snapshot_id
)
except ClientError:
failed_snapshots.append(block_device.snapshot_id)
print("{0} deleted\n".format(block_device.snapshot_id))
if block_device.snapshot_id is not None:
try:
self.ec2.delete_snapshot(
SnapshotId=block_device.snapshot_id
)
except ClientError:
failed_snapshots.append(block_device.snapshot_id)
print("{0} deleted\n".format(block_device.snapshot_id))

return failed_snapshots

Expand Down
19 changes: 18 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from moto import mock_ec2

from amicleaner.core import AMICleaner, OrphanSnapshotCleaner
from amicleaner.resources.models import AMI, AWSTag
from amicleaner.resources.models import AMI, AWSTag, AWSBlockDevice


def test_map_candidates_with_null_arguments():
Expand Down Expand Up @@ -205,6 +205,23 @@ def test_reduce_without_rotation_number():
assert AMICleaner().reduce_candidates(candidates) == candidates


def test_reduce_without_snapshot_id():
# creating block device
first_block_device = AWSBlockDevice()
first_block_device.snapshot_id = None

# creating tests objects
first_ami = AMI()
first_ami.id = 'ami-28c2b348'
first_ami.name = "ubuntu-20160102"
first_ami.block_device_mappings.append(first_block_device)

# creating amis to drop dict
candidates = [first_ami]

assert AMICleaner().reduce_candidates(candidates) == candidates


def test_reduce():
# creating tests objects
first_ami = AMI()
Expand Down

0 comments on commit 70627e5

Please sign in to comment.