File tree Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Expand file tree Collapse file tree 1 file changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -2,13 +2,17 @@ package scw
22
33import (
44 "encoding/json"
5+ "fmt"
56 "strings"
67
78 "github.com/scaleway/scaleway-sdk-go/internal/errors"
89 "github.com/scaleway/scaleway-sdk-go/logger"
910 "github.com/scaleway/scaleway-sdk-go/validation"
1011)
1112
13+ // localityPartsSeparator is the separator used in Zone and Region
14+ const localityPartsSeparator = "-"
15+
1216// Zone is an availability zone
1317type Zone string
1418
@@ -45,6 +49,17 @@ func (zone *Zone) String() string {
4549 return string (* zone )
4650}
4751
52+ // Region returns the parent Region for the Zone.
53+ // Manipulates the string directly to allow unlisted zones formatted as xx-yyy-z.
54+ func (zone * Zone ) Region () (Region , error ) {
55+ zoneStr := zone .String ()
56+ if ! validation .IsZone (zoneStr ) {
57+ return "" , fmt .Errorf ("invalid zone '%v'" , zoneStr )
58+ }
59+ zoneParts := strings .Split (zoneStr , localityPartsSeparator )
60+ return Region (strings .Join (zoneParts [:2 ], localityPartsSeparator )), nil
61+ }
62+
4863// Region is a geographical location
4964type Region string
5065
You can’t perform that action at this time.
0 commit comments