commit
e7d3a30c0e
|
@ -149,8 +149,9 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
||||||
|
|
||||||
if c.RawRegion != "" && !c.SkipValidation {
|
if c.RawRegion != "" && !c.SkipValidation {
|
||||||
ec2conn := getValidationSession()
|
ec2conn := getValidationSession()
|
||||||
if valid := ValidateRegion(c.RawRegion, ec2conn); !valid {
|
err := ValidateRegion(c.RawRegion, ec2conn)
|
||||||
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
|
if err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,20 +17,20 @@ func TestAccessConfigPrepare_Region(t *testing.T) {
|
||||||
mockConn := &mockEC2Client{}
|
mockConn := &mockEC2Client{}
|
||||||
|
|
||||||
c.RawRegion = "us-east-12"
|
c.RawRegion = "us-east-12"
|
||||||
valid := ValidateRegion(c.RawRegion, mockConn)
|
err := ValidateRegion(c.RawRegion, mockConn)
|
||||||
if valid {
|
if err == nil {
|
||||||
t.Fatalf("should have region validation err: %s", c.RawRegion)
|
t.Fatalf("should have region validation err: %s", c.RawRegion)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.RawRegion = "us-east-1"
|
c.RawRegion = "us-east-1"
|
||||||
valid = ValidateRegion(c.RawRegion, mockConn)
|
err = ValidateRegion(c.RawRegion, mockConn)
|
||||||
if !valid {
|
if err != nil {
|
||||||
t.Fatalf("shouldn't have region validation err: %s", c.RawRegion)
|
t.Fatalf("shouldn't have region validation err: %s", c.RawRegion)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.RawRegion = "custom"
|
c.RawRegion = "custom"
|
||||||
valid = ValidateRegion(c.RawRegion, mockConn)
|
err = ValidateRegion(c.RawRegion, mockConn)
|
||||||
if valid {
|
if err == nil {
|
||||||
t.Fatalf("should have region validation err: %s", c.RawRegion)
|
t.Fatalf("should have region validation err: %s", c.RawRegion)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,8 +112,9 @@ func (c *AMIConfig) prepareRegions(ec2conn ec2iface.EC2API, accessConfig *Access
|
||||||
|
|
||||||
if !c.AMISkipRegionValidation {
|
if !c.AMISkipRegionValidation {
|
||||||
// Verify the region is real
|
// Verify the region is real
|
||||||
if valid := ValidateRegion(region, ec2conn); !valid {
|
err := ValidateRegion(region, ec2conn)
|
||||||
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
|
if err != nil {
|
||||||
|
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -55,13 +55,17 @@ func TestAMIConfigPrepare_regions(t *testing.T) {
|
||||||
c.AMISkipRegionValidation = true
|
c.AMISkipRegionValidation = true
|
||||||
|
|
||||||
var errs []error
|
var errs []error
|
||||||
|
var err error
|
||||||
mockConn := &mockEC2Client{}
|
mockConn := &mockEC2Client{}
|
||||||
if errs = c.prepareRegions(mockConn, nil, errs); len(errs) > 0 {
|
if errs = c.prepareRegions(mockConn, nil, errs); len(errs) > 0 {
|
||||||
t.Fatalf("shouldn't have err: %#v", errs)
|
t.Fatalf("shouldn't have err: %#v", errs)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.AMISkipRegionValidation = false
|
c.AMISkipRegionValidation = false
|
||||||
c.AMIRegions = listEC2Regions(mockConn)
|
c.AMIRegions, err = listEC2Regions(mockConn)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("shouldn't have err: %s", err.Error())
|
||||||
|
}
|
||||||
if errs = c.prepareRegions(mockConn, nil, errs); len(errs) > 0 {
|
if errs = c.prepareRegions(mockConn, nil, errs); len(errs) > 0 {
|
||||||
t.Fatalf("shouldn't have err: %#v", errs)
|
t.Fatalf("shouldn't have err: %#v", errs)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2"
|
"github.com/aws/aws-sdk-go/service/ec2"
|
||||||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
||||||
|
@ -15,23 +16,32 @@ func getValidationSession() *ec2.EC2 {
|
||||||
return ec2conn
|
return ec2conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func listEC2Regions(ec2conn ec2iface.EC2API) []string {
|
func listEC2Regions(ec2conn ec2iface.EC2API) ([]string, error) {
|
||||||
var regions []string
|
var regions []string
|
||||||
resultRegions, _ := ec2conn.DescribeRegions(nil)
|
resultRegions, err := ec2conn.DescribeRegions(nil)
|
||||||
|
if err != nil {
|
||||||
|
return []string{}, err
|
||||||
|
}
|
||||||
for _, region := range resultRegions.Regions {
|
for _, region := range resultRegions.Regions {
|
||||||
regions = append(regions, *region.RegionName)
|
regions = append(regions, *region.RegionName)
|
||||||
}
|
}
|
||||||
|
|
||||||
return regions
|
return regions, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ValidateRegion returns true if the supplied region is a valid AWS
|
// ValidateRegion returns true if the supplied region is a valid AWS
|
||||||
// region and false if it's not.
|
// region and false if it's not.
|
||||||
func ValidateRegion(region string, ec2conn ec2iface.EC2API) bool {
|
func ValidateRegion(region string, ec2conn ec2iface.EC2API) error {
|
||||||
for _, valid := range listEC2Regions(ec2conn) {
|
regions, err := listEC2Regions(ec2conn)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, valid := range regions {
|
||||||
if region == valid {
|
if region == valid {
|
||||||
return true
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
|
return fmt.Errorf("Invalid region: %s", region)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue