builder/amazon/common: remove duplicates from ami_region

/cc @jmassara
This commit is contained in:
Mitchell Hashimoto 2013-08-22 14:58:21 -07:00
parent cd4fb50cb7
commit 374f2fb647
2 changed files with 42 additions and 1 deletions

View File

@ -63,11 +63,28 @@ func (c *AMIConfig) Prepare(t *packer.ConfigTemplate) []error {
}
if len(c.AMIRegions) > 0 {
regionSet := make(map[string]struct{})
regions := make([]string, 0, len(c.AMIRegions))
for _, region := range c.AMIRegions {
// If we already saw the region, then don't look again
if _, ok := regionSet[region]; ok {
continue
}
// Mark that we saw the region
regionSet[region] = struct{}{}
// Verify the region is real
if _, ok := aws.Regions[region]; !ok {
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
continue
}
regions = append(regions, region)
}
c.AMIRegions = regions
}
if len(errs) > 0 {

View File

@ -1,6 +1,7 @@
package common
import (
"reflect"
"testing"
)
@ -10,7 +11,7 @@ func testAMIConfig() *AMIConfig {
}
}
func TestAMIConfigPrepare_Region(t *testing.T) {
func TestAMIConfigPrepare_name(t *testing.T) {
c := testAMIConfig()
if err := c.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %s", err)
@ -21,3 +22,26 @@ func TestAMIConfigPrepare_Region(t *testing.T) {
t.Fatal("should have error")
}
}
func TestAMIConfigPrepare_regions(t *testing.T) {
c := testAMIConfig()
c.AMIRegions = nil
if err := c.Prepare(nil); err != nil {
t.Fatalf("shouldn't have err: %s", err)
}
c.AMIRegions = []string{"foo"}
if err := c.Prepare(nil); err == nil {
t.Fatal("should have error")
}
c.AMIRegions = []string{"us-east-1", "us-west-1", "us-east-1"}
if err := c.Prepare(nil); err != nil {
t.Fatalf("bad: %s", err)
}
expected := []string{"us-east-1", "us-west-1"}
if !reflect.DeepEqual(c.AMIRegions, expected) {
t.Fatalf("bad: %#v", c.AMIRegions)
}
}