2019-01-28 13:29:26 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2020-09-30 11:37:09 -04:00
|
|
|
"context"
|
2019-01-28 13:29:26 -05:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
"github.com/antihax/optional"
|
|
|
|
"github.com/outscale/osc-sdk-go/osc"
|
2019-01-28 13:29:26 -05:00
|
|
|
)
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
func listOSCRegions(oscconn *osc.RegionApiService) ([]string, error) {
|
2019-01-28 13:29:26 -05:00
|
|
|
var regions []string
|
2020-09-30 11:37:09 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-30 11:37:09 -04:00
|
|
|
resultRegions := resp
|
2019-01-28 13:29:26 -05:00
|
|
|
|
|
|
|
for _, region := range resultRegions.Regions {
|
|
|
|
regions = append(regions, region.RegionName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return regions, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 11:46:36 -04:00
|
|
|
// 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 {
|
2020-09-30 11:37:09 -04:00
|
|
|
oscconn := c.NewOSCClient()
|
2019-01-28 13:29:26 -05:00
|
|
|
|
2020-09-30 11:37:09 -04: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
|
|
|
|
}
|