2015-04-05 17:58:48 -04:00
|
|
|
package common
|
|
|
|
|
2018-07-19 02:32:35 -04:00
|
|
|
import (
|
2018-07-20 03:37:39 -04:00
|
|
|
"flag"
|
|
|
|
|
2018-07-19 02:32:35 -04:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
)
|
|
|
|
|
2016-01-12 22:03:21 -05:00
|
|
|
func listEC2Regions() []string {
|
2018-07-19 02:32:35 -04:00
|
|
|
var regions []string
|
|
|
|
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
}))
|
|
|
|
|
|
|
|
ec2conn := ec2.New(sess)
|
|
|
|
resultRegions, _ := ec2conn.DescribeRegions(nil)
|
|
|
|
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
|
|
|
|
|
|
|
return regions
|
2016-01-12 22:03:21 -05:00
|
|
|
}
|
2015-04-05 17:58:48 -04:00
|
|
|
|
2016-01-12 22:03:21 -05:00
|
|
|
// ValidateRegion returns true if the supplied region is a valid AWS
|
|
|
|
// region and false if it's not.
|
|
|
|
func ValidateRegion(region string) bool {
|
2018-07-20 03:37:39 -04:00
|
|
|
|
|
|
|
// To pass tests
|
|
|
|
if v := flag.Lookup("test.v"); v != nil || v.Value.String() == "true" {
|
|
|
|
regions := []string{
|
|
|
|
"us-east-1",
|
|
|
|
"us-east-2",
|
|
|
|
"us-west-1",
|
|
|
|
}
|
|
|
|
for _, valid := range regions {
|
|
|
|
if region == valid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normal run
|
2016-01-12 22:03:21 -05:00
|
|
|
for _, valid := range listEC2Regions() {
|
2015-04-05 17:58:48 -04:00
|
|
|
if region == valid {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|