remove unnecessary valid flag
This commit is contained in:
parent
d4767845f0
commit
b0cfecf314
|
@ -149,11 +149,9 @@ func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
||||||
|
|
||||||
if c.RawRegion != "" && !c.SkipValidation {
|
if c.RawRegion != "" && !c.SkipValidation {
|
||||||
ec2conn := getValidationSession()
|
ec2conn := getValidationSession()
|
||||||
valid, err := ValidateRegion(c.RawRegion, ec2conn)
|
err := ValidateRegion(c.RawRegion, ec2conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
|
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
|
||||||
} else if !valid {
|
|
||||||
errs = append(errs, fmt.Errorf("Unknown region: %s", c.RawRegion))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,11 +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
|
||||||
valid, err := ValidateRegion(region, ec2conn)
|
err := ValidateRegion(region, ec2conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
|
errs = append(errs, fmt.Errorf("error validating region: %s", err.Error()))
|
||||||
} else if !valid {
|
|
||||||
errs = append(errs, fmt.Errorf("Unknown region: %s", region))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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"
|
||||||
|
@ -30,16 +31,17 @@ func listEC2Regions(ec2conn ec2iface.EC2API) ([]string, error) {
|
||||||
|
|
||||||
// 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, error) {
|
func ValidateRegion(region string, ec2conn ec2iface.EC2API) error {
|
||||||
regions, err := listEC2Regions(ec2conn)
|
regions, err := listEC2Regions(ec2conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, valid := range regions {
|
for _, valid := range regions {
|
||||||
if region == valid {
|
if region == valid {
|
||||||
return true, nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false, nil
|
|
||||||
|
return fmt.Errorf("Invalid region: %s", region)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue