packer-cn/builder/amazonebs/artifact.go

65 lines
1.2 KiB
Go

package amazonebs
import (
"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 {
return BuilderId
}
func (*artifact) Files() []string {
// We have no files
return nil
}
func (*artifact) Id() string {
// TODO(mitchellh): Id
return "TODO"
}
func (a *artifact) String() string {
amiStrings := make([]string, 0, len(a.amis))
for region, id := range a.amis {
single := fmt.Sprintf("%s: %s", region, id)
amiStrings = append(amiStrings, single)
}
return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n"))
}
func (a *artifact) Destroy() error {
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 {
if len(errors) == 1 {
return errors[0]
} else {
return &packer.MultiError{errors}
}
}
return nil
}