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

58 lines
1.2 KiB
Go
Raw Normal View History

2019-01-28 13:29:26 -05:00
package common
import (
"context"
2019-01-28 13:29:26 -05:00
"fmt"
"github.com/antihax/optional"
"github.com/outscale/osc-sdk-go/osc"
2019-01-28 13:29:26 -05:00
)
func listOSCRegions(oscconn *osc.RegionApiService) ([]string, error) {
2019-01-28 13:29:26 -05:00
var regions []string
resp, _, err := oscconn.ReadRegions(context.Background(), &osc.ReadRegionsOpts{
ReadRegionsRequest: optional.NewInterface(osc.ReadRegionsRequest{}),
})
if err != nil {
2019-01-28 13:29:26 -05:00
return []string{}, err
}
resultRegions := resp
2019-01-28 13:29:26 -05:00
for _, region := range resultRegions.Regions {
regions = append(regions, region.RegionName)
}
return regions, nil
}
// ValidateRegion returns true if the supplied region is a valid Outscale
2019-01-28 13:29:26 -05:00
// region and false if it's not.
2020-10-13 13:54:25 -04:00
func (c *AccessConfig) ValidateOSCRegion(regions ...string) error {
oscconn := c.NewOSCClient()
2019-01-28 13:29:26 -05:00
validRegions, err := listOSCRegions(oscconn.RegionApi)
2019-01-28 13:29:26 -05:00
if err != nil {
return err
}
var invalidRegions []string
for _, region := range regions {
found := false
for _, validRegion := range validRegions {
if region == validRegion {
found = true
break
}
}
if !found {
invalidRegions = append(invalidRegions, region)
}
}
if len(invalidRegions) > 0 {
return fmt.Errorf("Invalid region(s): %v", invalidRegions)
}
return nil
}