forked from scaleway/scaleway-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocality.go
155 lines (137 loc) · 3.45 KB
/
locality.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package scw
import (
"encoding/json"
"github.com/scaleway/scaleway-sdk-go/logger"
)
// Zone is an availability zone
type Zone string
const (
// ZoneFrPar1 represents the fr-par-1 zone
ZoneFrPar1 = Zone("fr-par-1")
// ZoneFrPar2 represents the fr-par-2 zone
ZoneFrPar2 = Zone("fr-par-2")
// ZoneNlAms1 represents the nl-ams-1 zone
ZoneNlAms1 = Zone("nl-ams-1")
)
var (
// AllZones is an array that list all zones
AllZones = []Zone{
ZoneFrPar1,
ZoneFrPar2,
ZoneNlAms1,
}
)
// Exists checks whether a zone exists
func (zone *Zone) Exists() bool {
for _, z := range AllZones {
if z == *zone {
return true
}
}
return false
}
// Region is a geographical location
type Region string
const (
// RegionFrPar represents the fr-par region
RegionFrPar = Region("fr-par")
// RegionNlAms represents the nl-ams region
RegionNlAms = Region("nl-ams")
)
var (
// AllRegions is an array that list all regions
AllRegions = []Region{
RegionFrPar,
RegionNlAms,
}
)
// Exists checks whether a region exists
func (region *Region) Exists() bool {
for _, r := range AllRegions {
if r == *region {
return true
}
}
return false
}
// GetZones is a function that returns the zones for the specified region
func (region Region) GetZones() []Zone {
switch region {
case RegionFrPar:
return []Zone{ZoneFrPar1, ZoneFrPar2}
case RegionNlAms:
return []Zone{ZoneNlAms1}
default:
return []Zone{}
}
}
// ParseZone parse a string value into a Zone object
func ParseZone(zone string) (Zone, error) {
switch zone {
case "par1":
// would be triggered by API market place
// logger.Warningf("par1 is a deprecated name for zone, use fr-par-1 instead")
return ZoneFrPar1, nil
case "ams1":
// would be triggered by API market place
// logger.Warningf("ams1 is a deprecated name for zone, use nl-ams-1 instead")
return ZoneNlAms1, nil
default:
newZone := Zone(zone)
if !newZone.Exists() {
logger.Warningf("%s is an unknown zone", newZone)
}
return newZone, nil
}
}
// UnmarshalJSON implements the Unmarshaler interface for a Zone.
// this to call ParseZone on the string input and return the correct Zone object.
func (zone *Zone) UnmarshalJSON(input []byte) error {
// parse input value as string
var stringValue string
err := json.Unmarshal(input, &stringValue)
if err != nil {
return err
}
// parse string as Zone
*zone, err = ParseZone(stringValue)
if err != nil {
return err
}
return nil
}
// ParseRegion parse a string value into a Zone object
func ParseRegion(region string) (Region, error) {
switch region {
case "par1":
// would be triggered by API market place
// logger.Warningf("par1 is a deprecated name for region, use fr-par instead")
return RegionFrPar, nil
case "ams1":
// would be triggered by API market place
// logger.Warningf("ams1 is a deprecated name for region, use nl-ams instead")
return RegionNlAms, nil
default:
newRegion := Region(region)
if !newRegion.Exists() {
logger.Warningf("%s is an unknown region", newRegion)
}
return newRegion, nil
}
}
// UnmarshalJSON implements the Unmarshaler interface for a Region.
// this to call ParseRegion on the string input and return the correct Region object.
func (region *Region) UnmarshalJSON(input []byte) error {
// parse input value as string
var stringValue string
err := json.Unmarshal(input, &stringValue)
if err != nil {
return err
}
// parse string as Region
*region, err = ParseRegion(stringValue)
if err != nil {
return err
}
return nil
}