2015-04-05 17:58:48 -04:00
|
|
|
package common
|
|
|
|
|
2018-07-19 02:32:35 -04:00
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
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-09-10 18:43:01 -04:00
|
|
|
func getValidationSession() *ec2.EC2 {
|
2018-07-19 02:32:35 -04:00
|
|
|
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
|
|
|
SharedConfigState: session.SharedConfigEnable,
|
|
|
|
}))
|
|
|
|
|
|
|
|
ec2conn := ec2.New(sess)
|
2018-09-10 18:43:01 -04:00
|
|
|
return ec2conn
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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.
|
2018-10-16 17:15:55 -04:00
|
|
|
func ValidateRegion(region string, ec2conn ec2iface.EC2API) (bool, error) {
|
|
|
|
regions, err := listEC2Regions(ec2conn)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, valid := range regions {
|
2015-04-05 17:58:48 -04:00
|
|
|
if region == valid {
|
2018-10-16 17:15:55 -04:00
|
|
|
return true, nil
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 17:15:55 -04:00
|
|
|
return false, nil
|
2015-04-05 17:58:48 -04:00
|
|
|
}
|