2015-04-05 17:58:48 -04:00
|
|
|
package common
|
|
|
|
|
2018-07-19 02:32:35 -04:00
|
|
|
import (
|
2018-10-17 14:03:31 -04:00
|
|
|
"fmt"
|
2018-10-24 05:26:53 -04:00
|
|
|
|
2018-09-10 18:43:01 -04:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
2018-07-19 02:32:35 -04:00
|
|
|
)
|
|
|
|
|
2018-10-16 17:15:55 -04:00
|
|
|
func listEC2Regions(ec2conn ec2iface.EC2API) ([]string, error) {
|
2018-09-10 18:43:01 -04:00
|
|
|
var regions []string
|
2018-10-16 17:15:55 -04:00
|
|
|
resultRegions, err := ec2conn.DescribeRegions(nil)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2018-07-19 02:32:35 -04:00
|
|
|
for _, region := range resultRegions.Regions {
|
|
|
|
regions = append(regions, *region.RegionName)
|
2016-01-11 16:04:35 -05:00
|
|
|
}
|
2018-07-19 02:32:35 -04:00
|
|
|
|
2018-10-16 17:15:55 -04:00
|
|
|
return regions, nil
|
2016-01-12 22:03:21 -05:00
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2018-11-26 05:33:20 -05: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.
|
2018-10-24 05:26:53 -04:00
|
|
|
func (c *AccessConfig) ValidateRegion(regions ...string) error {
|
|
|
|
ec2conn, err := c.NewEC2Connection()
|
2018-10-16 17:15:55 -04:00
|
|
|
if err != nil {
|
2018-10-17 14:03:31 -04:00
|
|
|
return err
|
2018-10-16 17:15:55 -04:00
|
|
|
}
|
|
|
|
|
2018-10-24 05:26:53 -04:00
|
|
|
validRegions, err := listEC2Regions(ec2conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var invalidRegions []string
|
|
|
|
for _, region := range regions {
|
2019-01-16 14:02:13 -05:00
|
|
|
if region == "" {
|
|
|
|
continue
|
|
|
|
}
|
2018-10-24 05:26:53 -04:00
|
|
|
found := false
|
|
|
|
for _, validRegion := range validRegions {
|
|
|
|
if region == validRegion {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
invalidRegions = append(invalidRegions, region)
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-17 14:03:31 -04:00
|
|
|
|
2018-10-24 05:26:53 -04:00
|
|
|
if len(invalidRegions) > 0 {
|
2018-11-26 05:33:20 -05:00
|
|
|
return fmt.Errorf("Invalid region(s): %v, available regions: %v", invalidRegions, validRegions)
|
2018-10-24 05:26:53 -04:00
|
|
|
}
|
|
|
|
return nil
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|