Skip to content

Commit 7972939

Browse files
authored
feat: get Region from Zone (#255)
1 parent b29bed7 commit 7972939

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

scw/locality.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ package scw
22

33
import (
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
1317
type 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
4964
type Region string
5065

0 commit comments

Comments
 (0)