From 2dfc5d3e71993dc37b68df842ca0de072c40f5ca Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 2 Dec 2014 18:22:26 -0800 Subject: [PATCH] builder/amazon: inject special metadata for atlas artifacts --- builder/amazon/common/artifact.go | 24 ++++++++++++++++++++---- builder/amazon/common/artifact_test.go | 22 +++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/builder/amazon/common/artifact.go b/builder/amazon/common/artifact.go index 8831d313b..8d6265508 100644 --- a/builder/amazon/common/artifact.go +++ b/builder/amazon/common/artifact.go @@ -2,12 +2,13 @@ package common import ( "fmt" - "github.com/mitchellh/goamz/aws" - "github.com/mitchellh/goamz/ec2" - "github.com/mitchellh/packer/packer" "log" "sort" "strings" + + "github.com/mitchellh/goamz/aws" + "github.com/mitchellh/goamz/ec2" + "github.com/mitchellh/packer/packer" ) // Artifact is an artifact implementation that contains built AMIs. @@ -53,7 +54,12 @@ func (a *Artifact) String() string { } func (a *Artifact) State(name string) interface{} { - return nil + switch name { + case "atlas.artifact.metadata": + return a.stateAtlasMetadata() + default: + return nil + } } func (a *Artifact) Destroy() error { @@ -79,3 +85,13 @@ func (a *Artifact) Destroy() error { return nil } + +func (a *Artifact) stateAtlasMetadata() interface{} { + metadata := make(map[string]string) + for region, imageId := range a.Amis { + k := fmt.Sprintf("region.%s", region) + metadata[k] = imageId + } + + return metadata +} diff --git a/builder/amazon/common/artifact_test.go b/builder/amazon/common/artifact_test.go index ccca4fa0a..46dfedf3f 100644 --- a/builder/amazon/common/artifact_test.go +++ b/builder/amazon/common/artifact_test.go @@ -1,8 +1,10 @@ package common import ( - "github.com/mitchellh/packer/packer" + "reflect" "testing" + + "github.com/mitchellh/packer/packer" ) func TestArtifact_Impl(t *testing.T) { @@ -26,6 +28,24 @@ func TestArtifactId(t *testing.T) { } } +func TestArtifactState_atlasMetadata(t *testing.T) { + a := &Artifact{ + Amis: map[string]string{ + "east": "foo", + "west": "bar", + }, + } + + actual := a.State("atlas.artifact.metadata") + expected := map[string]string{ + "region.east": "foo", + "region.west": "bar", + } + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("bad: %#v", actual) + } +} + func TestArtifactString(t *testing.T) { expected := `AMIs were created: