Skip to content

feat: get Region from Zone #255

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

Merged
merged 3 commits into from
Dec 2, 2019
Merged
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
15 changes: 15 additions & 0 deletions scw/locality.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package scw

import (
"encoding/json"
"fmt"
"strings"

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

// localityPartsSeparator is the separator used in Zone and Region
const localityPartsSeparator = "-"

// Zone is an availability zone
type Zone string

Expand Down Expand Up @@ -45,6 +49,17 @@ func (zone *Zone) String() string {
return string(*zone)
}

// Region returns the parent Region for the Zone.
// Manipulates the string directly to allow unlisted zones formatted as xx-yyy-z.
func (zone *Zone) Region() (Region, error) {
zoneStr := zone.String()
if !validation.IsZone(zoneStr) {
return "", fmt.Errorf("invalid zone '%v'", zoneStr)
}
zoneParts := strings.Split(zoneStr, localityPartsSeparator)
return Region(strings.Join(zoneParts[:2], localityPartsSeparator)), nil
}

// Region is a geographical location
type Region string

Expand Down