builder/amazon/common: remove duplicates from ami_region
/cc @jmassara
This commit is contained in:
parent
cd4fb50cb7
commit
374f2fb647
|
@ -63,11 +63,28 @@ func (c *AMIConfig) Prepare(t *packer.ConfigTemplate) []error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(c.AMIRegions) > 0 {
|
if len(c.AMIRegions) > 0 {
|
||||||
|
regionSet := make(map[string]struct{})
|
||||||
|
regions := make([]string, 0, len(c.AMIRegions))
|
||||||
|
|
||||||
for _, region := range 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 {
|
if _, ok := aws.Regions[region]; !ok {
|
||||||
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
|
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
regions = append(regions, region)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.AMIRegions = regions
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(errs) > 0 {
|
if len(errs) > 0 {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -10,7 +11,7 @@ func testAMIConfig() *AMIConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAMIConfigPrepare_Region(t *testing.T) {
|
func TestAMIConfigPrepare_name(t *testing.T) {
|
||||||
c := testAMIConfig()
|
c := testAMIConfig()
|
||||||
if err := c.Prepare(nil); err != nil {
|
if err := c.Prepare(nil); err != nil {
|
||||||
t.Fatalf("shouldn't have err: %s", err)
|
t.Fatalf("shouldn't have err: %s", err)
|
||||||
|
@ -21,3 +22,26 @@ func TestAMIConfigPrepare_Region(t *testing.T) {
|
||||||
t.Fatal("should have error")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue