diff --git a/scw/locality.go b/scw/locality.go index a71ca1f6..1dc7b9e4 100644 --- a/scw/locality.go +++ b/scw/locality.go @@ -2,10 +2,18 @@ package scw import ( "encoding/json" + "regexp" + "strings" + "github.com/scaleway/scaleway-sdk-go/internal/errors" "github.com/scaleway/scaleway-sdk-go/logger" ) +var ( + regionRegex = regexp.MustCompile("^[a-z]{2}-[a-z]{3}$") + zoneRegex = regexp.MustCompile("^[a-z]{2}-[a-z]{3}-[1-9]$") +) + // Zone is an availability zone type Zone string @@ -77,7 +85,7 @@ func (region Region) GetZones() []Zone { } } -// ParseZone parse a string value into a Zone object +// ParseZone parses a string value into a Zone and returns an error if it has a bad format. func ParseZone(zone string) (Zone, error) { switch zone { case "par1": @@ -89,6 +97,14 @@ func ParseZone(zone string) (Zone, error) { // logger.Warningf("ams1 is a deprecated name for zone, use nl-ams-1 instead") return ZoneNlAms1, nil default: + if !zoneRegex.Match([]byte(zone)) { + zones := []string(nil) + for _, z := range AllZones { + zones = append(zones, string(z)) + } + return "", errors.New("wrong zone format, available zones are: %s", strings.Join(zones, ", ")) + } + newZone := Zone(zone) if !newZone.Exists() { logger.Warningf("%s is an unknown zone", newZone) @@ -116,7 +132,7 @@ func (zone *Zone) UnmarshalJSON(input []byte) error { return nil } -// ParseRegion parse a string value into a Zone object +// ParseRegion parses a string value into a Region and returns an error if it has a bad format. func ParseRegion(region string) (Region, error) { switch region { case "par1": @@ -128,6 +144,14 @@ func ParseRegion(region string) (Region, error) { // logger.Warningf("ams1 is a deprecated name for region, use nl-ams instead") return RegionNlAms, nil default: + if !regionRegex.Match([]byte(region)) { + regions := []string(nil) + for _, r := range AllRegions { + regions = append(regions, string(r)) + } + return "", errors.New("wrong region format, available regions are: %s", strings.Join(regions, ", ")) + } + newRegion := Region(region) if !newRegion.Exists() { logger.Warningf("%s is an unknown region", newRegion) diff --git a/scw/locality_test.go b/scw/locality_test.go index 2ae5fe48..9784b559 100644 --- a/scw/locality_test.go +++ b/scw/locality_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "testing" + "github.com/scaleway/scaleway-sdk-go/internal/errors" "github.com/scaleway/scaleway-sdk-go/internal/testhelpers" ) @@ -11,6 +12,7 @@ func TestParseZone(t *testing.T) { tests := []struct { input string + err error expected Zone }{ { @@ -25,12 +27,33 @@ func TestParseZone(t *testing.T) { input: "ams1", expected: ZoneNlAms1, }, + { + input: "xx-xxx-1", + expected: "xx-xxx-1", + }, + { + input: "fr-par", + expected: "", + err: errors.New("wrong zone format, available zones are: fr-par-1, fr-par-2, nl-ams-1"), + }, + { + input: "fr-par-n", + expected: "", + err: errors.New("wrong zone format, available zones are: fr-par-1, fr-par-2, nl-ams-1"), + }, + { + input: "fr-par-0", + expected: "", + err: errors.New("wrong zone format, available zones are: fr-par-1, fr-par-2, nl-ams-1"), + }, } for _, test := range tests { - z, err := ParseZone(test.input) - testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, test.expected, z) + t.Run(test.input, func(t *testing.T) { + z, err := ParseZone(test.input) + testhelpers.Equals(t, test.err, err) + testhelpers.Equals(t, test.expected, z) + }) } } @@ -67,6 +90,7 @@ func TestParseRegion(t *testing.T) { tests := []struct { input string + err error expected Region }{ { @@ -81,12 +105,28 @@ func TestParseRegion(t *testing.T) { input: "ams1", expected: RegionNlAms, }, + { + input: "xx-xxx", + expected: "xx-xxx", + }, + { + input: "fr-par-1", + expected: "", + err: errors.New("wrong region format, available regions are: fr-par, nl-ams"), + }, + { + input: "fr-pa1", + expected: "", + err: errors.New("wrong region format, available regions are: fr-par, nl-ams"), + }, } for _, test := range tests { - r, err := ParseRegion(test.input) - testhelpers.AssertNoError(t, err) - testhelpers.Equals(t, test.expected, r) + t.Run(test.input, func(t *testing.T) { + r, err := ParseRegion(test.input) + testhelpers.Equals(t, test.err, err) + testhelpers.Equals(t, test.expected, r) + }) } }