packer-cn/builder/amazon/common/regions.go

37 lines
767 B
Go
Raw Normal View History

package common
import (
2018-10-17 14:03:31 -04:00
"fmt"
2018-09-10 18:43:01 -04:00
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
)
func listEC2Regions(ec2conn ec2iface.EC2API) ([]string, error) {
2018-09-10 18:43:01 -04: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 16:04:35 -05:00
}
return regions, nil
}
// ValidateRegion returns true if the supplied region is a valid AWS
// region and false if it's not.
2018-10-17 14:03:31 -04:00
func ValidateRegion(region string, ec2conn ec2iface.EC2API) error {
regions, err := listEC2Regions(ec2conn)
if err != nil {
2018-10-17 14:03:31 -04:00
return err
}
for _, valid := range regions {
if region == valid {
2018-10-17 14:03:31 -04:00
return nil
}
}
2018-10-17 14:03:31 -04:00
return fmt.Errorf("Invalid region: %s", region)
}