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 (
"fmt"
@ -8,35 +8,39 @@ import (
"strings"
)
type artifact struct {
// Artifact is an artifact implementation that contains built AMIs.
type Artifact struct {
// 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.
conn *ec2.EC2
Conn *ec2.EC2
}
func (*artifact) BuilderId() string {
return BuilderId
func (a *Artifact) BuilderId() string {
return a.BuilderIdValue
}
func (*artifact) Files() []string {
func (*Artifact) Files() []string {
// We have no files
return nil
}
func (a *artifact) Id() string {
parts := make([]string, 0, len(a.amis))
for region, amiId := range a.amis {
func (a *Artifact) Id() string {
parts := make([]string, 0, len(a.Amis))
for region, amiId := range a.Amis {
parts = append(parts, fmt.Sprintf("%s:%s", region, amiId))
}
return strings.Join(parts, ",")
}
func (a *artifact) String() string {
amiStrings := make([]string, 0, len(a.amis))
for region, id := range a.amis {
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)
}
@ -44,12 +48,12 @@ func (a *artifact) String() string {
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)
for _, imageId := range a.amis {
for _, imageId := range a.Amis {
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)
}

View File

@ -1,4 +1,4 @@
package ebs
package common
import (
"cgl.tideland.biz/asserts"
@ -10,7 +10,7 @@ func TestArtifact_Impl(t *testing.T) {
assert := asserts.NewTestingAsserts(t, true)
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) {
@ -22,7 +22,7 @@ func TestArtifactId(t *testing.T) {
amis["east"] = "foo"
amis["west"] = "bar"
a := &artifact{amis, nil}
a := &Artifact{amis, nil}
result := a.Id()
assert.Equal(result, expected, "should match output")
}
@ -39,7 +39,7 @@ west: bar`
amis["east"] = "foo"
amis["west"] = "bar"
a := &artifact{amis, nil}
a := &Artifact{amis, nil}
result := a.String()
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
artifact := &artifact{
amis: state["amis"].(map[string]string),
conn: ec2conn,
artifact := &awscommon.Artifact{
Amis: state["amis"].(map[string]string),
BuilderIdValue: BuilderId,
Conn: ec2conn,
}
return artifact, nil