Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): introduce format validation in locality parsing #237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions scw/locality.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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":
Expand All @@ -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)
Expand Down Expand Up @@ -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":
Expand All @@ -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)
Expand Down
52 changes: 46 additions & 6 deletions scw/locality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"encoding/json"
"testing"

"github.com/scaleway/scaleway-sdk-go/internal/errors"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
)

func TestParseZone(t *testing.T) {

tests := []struct {
input string
err error
expected Zone
}{
{
Expand All @@ -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)
})
}

}
Expand Down Expand Up @@ -67,6 +90,7 @@ func TestParseRegion(t *testing.T) {

tests := []struct {
input string
err error
expected Region
}{
{
Expand All @@ -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)
})
}

}