builder/amazon/common: extract Artifact

This commit is contained in:
Mitchell Hashimoto 2013-07-20 20:08:41 -07:00
parent 0a76d07363
commit 992a9bfb22
3 changed files with 28 additions and 23 deletions

View File

@ -1,4 +1,4 @@
package ebs package common
import ( import (
"fmt" "fmt"
@ -8,35 +8,39 @@ import (
"strings" "strings"
) )
type artifact struct { // Artifact is an artifact implementation that contains built AMIs.
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
// BuilderId is the unique ID for the builder that created this AMI
BuilderIdValue string
// EC2 connection for performing API stuff. // EC2 connection for performing API stuff.
conn *ec2.EC2 Conn *ec2.EC2
} }
func (*artifact) BuilderId() string { func (a *Artifact) BuilderId() string {
return BuilderId return a.BuilderIdValue
} }
func (*artifact) Files() []string { func (*Artifact) Files() []string {
// We have no files // We have no files
return nil return nil
} }
func (a *artifact) Id() string { func (a *Artifact) Id() string {
parts := make([]string, 0, len(a.amis)) parts := make([]string, 0, len(a.Amis))
for region, amiId := range a.amis { for region, amiId := range a.Amis {
parts = append(parts, fmt.Sprintf("%s:%s", region, amiId)) parts = append(parts, fmt.Sprintf("%s:%s", region, amiId))
} }
return strings.Join(parts, ",") return strings.Join(parts, ",")
} }
func (a *artifact) String() string { func (a *Artifact) String() string {
amiStrings := make([]string, 0, len(a.amis)) amiStrings := make([]string, 0, len(a.Amis))
for region, id := range a.amis { for region, id := range a.Amis {
single := fmt.Sprintf("%s: %s", region, id) single := fmt.Sprintf("%s: %s", region, id)
amiStrings = append(amiStrings, single) amiStrings = append(amiStrings, single)
} }
@ -44,12 +48,12 @@ func (a *artifact) String() string {
return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n")) return fmt.Sprintf("AMIs were created:\n\n%s", strings.Join(amiStrings, "\n"))
} }
func (a *artifact) Destroy() error { func (a *Artifact) Destroy() error {
errors := make([]error, 0) errors := make([]error, 0)
for _, imageId := range a.amis { for _, imageId := range a.Amis {
log.Printf("Deregistering image ID: %s", imageId) log.Printf("Deregistering image ID: %s", imageId)
if _, err := a.conn.DeregisterImage(imageId); err != nil { if _, err := a.Conn.DeregisterImage(imageId); err != nil {
errors = append(errors, err) errors = append(errors, err)
} }

View File

@ -1,4 +1,4 @@
package ebs package common
import ( import (
"cgl.tideland.biz/asserts" "cgl.tideland.biz/asserts"
@ -10,7 +10,7 @@ func TestArtifact_Impl(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true) assert := asserts.NewTestingAsserts(t, true)
var actual packer.Artifact var actual packer.Artifact
assert.Implementor(&artifact{}, &actual, "should be an Artifact") assert.Implementor(&Artifact{}, &actual, "should be an Artifact")
} }
func TestArtifactId(t *testing.T) { func TestArtifactId(t *testing.T) {
@ -22,7 +22,7 @@ func TestArtifactId(t *testing.T) {
amis["east"] = "foo" amis["east"] = "foo"
amis["west"] = "bar" amis["west"] = "bar"
a := &artifact{amis, nil} a := &Artifact{amis, nil}
result := a.Id() result := a.Id()
assert.Equal(result, expected, "should match output") assert.Equal(result, expected, "should match output")
} }
@ -39,7 +39,7 @@ west: bar`
amis["east"] = "foo" amis["east"] = "foo"
amis["west"] = "bar" amis["west"] = "bar"
a := &artifact{amis, nil} 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

@ -131,9 +131,10 @@ 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
artifact := &artifact{ artifact := &awscommon.Artifact{
amis: state["amis"].(map[string]string), Amis: state["amis"].(map[string]string),
conn: ec2conn, BuilderIdValue: BuilderId,
Conn: ec2conn,
} }
return artifact, nil return artifact, nil