builder/amazonebs: Implement Artifact.Destroy

This commit is contained in:
Mitchell Hashimoto 2013-06-18 16:24:35 -07:00
parent 0f354c79d1
commit 4f229dea09
3 changed files with 29 additions and 4 deletions

View File

@ -1,14 +1,19 @@
package amazonebs package amazonebs
import ( import (
"errors"
"fmt" "fmt"
"github.com/mitchellh/goamz/ec2"
"github.com/mitchellh/packer/packer"
"log"
"strings" "strings"
) )
type artifact struct { type artifact struct {
// A map of regions to AMI IDs. // A map of regions to AMI IDs.
amis map[string]string amis map[string]string
// EC2 connection for performing API stuff.
conn *ec2.EC2
} }
func (*artifact) BuilderId() string { func (*artifact) BuilderId() string {
@ -36,5 +41,20 @@ func (a *artifact) String() string {
} }
func (a *artifact) Destroy() error { 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
} }

View File

@ -25,7 +25,7 @@ west: bar`
amis["east"] = "foo" amis["east"] = "foo"
amis["west"] = "bar" amis["west"] = "bar"
a := &artifact{amis} a := &artifact{amis, nil}
result := a.String() result := a.String()
assert.Equal(result, expected, "should match output") assert.Equal(result, expected, "should match output")
} }

View File

@ -161,7 +161,12 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
} }
// Build the artifact and return it // 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() { func (b *Builder) Cancel() {