Skip to content

Commit

Permalink
Merge pull request #13 from josh-padnick/no-ami-fix
Browse files Browse the repository at this point in the history
Delete command should not error out if there are no existing snapshots
  • Loading branch information
brikis98 authored Sep 14, 2016
2 parents bb52978 + 2bad794 commit 8648323
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.5.2 (September 14, 2016)

* BUG: The ec2-snapper `delete` command now gracefully exits if no snapshots exist for the given instance rather than
exiting with an error.

# 0.5.1 (September 11, 2016)

* ENHANCEMENT: ec2-snapper now also tags the EBS volume snapshots it creates as part of the process of creating an AMI.
Expand Down
17 changes: 14 additions & 3 deletions delete_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,26 @@ func deleteSnapshots(c DeleteCommand) error {
return err
}
c.InstanceId = instanceId
} else {
result, err := svc.DescribeInstances(&ec2.DescribeInstancesInput{InstanceIds: []*string{aws.String(c.InstanceId)}})
if err != nil {
return err
}
if len(result.Reservations) == 0 || len(result.Reservations[0].Instances) == 0 {
return fmt.Errorf("Could not find an instance with id %s", c.InstanceId)
}
}

images, err := findImages(c.InstanceId, svc)
if err != nil {
return err
}

if len(images) == 0 {
c.Ui.Info("NO ACTION TAKEN. There are no existing snapshots of instance " + c.InstanceId + " to delete.")
return nil
}

// Check that at least the --require-at-least number of AMIs exists
// - Note that even if this passes, we still want to avoid deleting so many AMIs that we go below the threshold
if len(images) <= c.RequireAtLeast {
Expand Down Expand Up @@ -237,9 +251,6 @@ func findImages(instanceId string, svc *ec2.EC2) ([]*ec2.Image, error) {
} else if err != nil {
return noImages, err
}
if len(resp.Images) == 0 {
return noImages, errors.New("No AMIs were found for EC2 instance \"" + instanceId + "\"")
}

return resp.Images, nil
}
Expand Down
16 changes: 16 additions & 0 deletions integration_create_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ func TestDeleteRespectsAtLeast(t *testing.T) {
verifySnapshotWorks(snapshotId, svc, logger, t)
}

// An integration test that runs an EC2 instance, does not take any snapshots of it, and then calls the delete_command
// to ensure that it exits gracefully even if no snapshots exist.
func TestDeleteHandlesNoSnapshots(t *testing.T) {
t.Parallel()

logger, ui := createLoggerAndUi("TestDeleteHandlesNoSnapshots")
session := session.New(&aws.Config{Region: aws.String(AWS_REGION_FOR_TESTING)})
svc := ec2.New(session)

instance, instanceName := launchInstance(svc, logger, t)
defer terminateInstance(instance, svc, logger, t)
waitForInstanceToStart(instance, svc, logger, t)

deleteSnapshotForInstance(instanceName, "0h", 0, ui, logger, t)
}

func TestCreateWithInvalidInstanceName(t *testing.T) {
t.Parallel()

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
}

// CLI stuff
c := cli.NewCLI("ec2-snapper", "0.5.1")
c := cli.NewCLI("ec2-snapper", "0.5.2")
c.Args = os.Args[1:]

c.Commands = map[string]cli.CommandFactory{
Expand Down

0 comments on commit 8648323

Please sign in to comment.