From 4f229dea09da9b50be51505328602ef96318319f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 18 Jun 2013 16:24:35 -0700 Subject: [PATCH] builder/amazonebs: Implement Artifact.Destroy --- builder/amazonebs/artifact.go | 24 ++++++++++++++++++++++-- builder/amazonebs/artifact_test.go | 2 +- builder/amazonebs/builder.go | 7 ++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/builder/amazonebs/artifact.go b/builder/amazonebs/artifact.go index f68a0da3c..aaaab1891 100644 --- a/builder/amazonebs/artifact.go +++ b/builder/amazonebs/artifact.go @@ -1,14 +1,19 @@ package amazonebs import ( - "errors" "fmt" + "github.com/mitchellh/goamz/ec2" + "github.com/mitchellh/packer/packer" + "log" "strings" ) type artifact struct { // A map of regions to AMI IDs. amis map[string]string + + // EC2 connection for performing API stuff. + conn *ec2.EC2 } func (*artifact) BuilderId() string { @@ -36,5 +41,20 @@ func (a *artifact) String() string { } func (a *artifact) Destroy() error { - return errors.New("not implemented yet") + errors := make([]error, 0) + + for _, imageId := range a.amis { + log.Printf("Degistering image ID: %s", imageId) + if _, err := a.conn.DeregisterImage(imageId); err != nil { + errors = append(errors, err) + } + + // TODO(mitchellh): Delete the snapshots associated with an AMI too + } + + if len(errors) > 0 { + return &packer.MultiError{errors} + } + + return nil } diff --git a/builder/amazonebs/artifact_test.go b/builder/amazonebs/artifact_test.go index 992e19aea..a6ba75080 100644 --- a/builder/amazonebs/artifact_test.go +++ b/builder/amazonebs/artifact_test.go @@ -25,7 +25,7 @@ west: bar` amis["east"] = "foo" amis["west"] = "bar" - a := &artifact{amis} + a := &artifact{amis, nil} result := a.String() assert.Equal(result, expected, "should match output") } diff --git a/builder/amazonebs/builder.go b/builder/amazonebs/builder.go index 4248b9e51..38d60ccd7 100644 --- a/builder/amazonebs/builder.go +++ b/builder/amazonebs/builder.go @@ -161,7 +161,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe } // Build the artifact and return it - return &artifact{state["amis"].(map[string]string)}, nil + artifact := &artifact{ + amis: state["amis"].(map[string]string), + conn: ec2conn, + } + + return artifact, nil } func (b *Builder) Cancel() {