59 lines
1.2 KiB
Go
Raw Normal View History

package common
import (
2018-10-17 11:03:31 -07:00
"fmt"
2018-09-10 15:43:01 -07:00
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
func listEC2Regions(ec2conn ec2iface.EC2API) ([]string, error) {
2018-09-10 15:43:01 -07:00
var regions []string
resultRegions, err := ec2conn.DescribeRegions(nil)
if err != nil {
return []string{}, err
}
for _, region := range resultRegions.Regions {
regions = append(regions, *region.RegionName)
2016-01-11 13:04:35 -08:00
}
return regions, nil
}
2018-11-26 11:33:20 +01:00
// ValidateRegion returns an nil if the regions are valid
// and exists; otherwise an error.
// ValidateRegion calls ec2conn.DescribeRegions to get the list of
// regions available to this account.
func (c *AccessConfig) ValidateRegion(regions ...string) error {
ec2conn, err := c.NewEC2Connection()
if err != nil {
2018-10-17 11:03:31 -07:00
return err
}
validRegions, err := listEC2Regions(ec2conn)
if err != nil {
return err
}
var invalidRegions []string
for _, region := range regions {
2019-01-16 11:02:13 -08:00
if region == "" {
continue
}
found := false
for _, validRegion := range validRegions {
if region == validRegion {
found = true
break
}
}
if !found {
invalidRegions = append(invalidRegions, region)
}
}
2018-10-17 11:03:31 -07:00
if len(invalidRegions) > 0 {
2018-11-26 11:33:20 +01:00
return fmt.Errorf("Invalid region(s): %v, available regions: %v", invalidRegions, validRegions)
}
return nil
}