-
Notifications
You must be signed in to change notification settings - Fork 8
/
location.go
309 lines (269 loc) · 10.3 KB
/
location.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package megaport
import (
"context"
"encoding/json"
"io"
"net/http"
"github.com/lithammer/fuzzysearch/fuzzy"
)
// LocationService is an interface for interfacing with the Location endpoints of the Megaport API.
type LocationService interface {
// ListLocations returns a list of all locations in the Megaport Locations API.
ListLocations(ctx context.Context) ([]*Location, error)
// GetLocationByID returns a location by its ID in the Megaport Locations API.
GetLocationByID(ctx context.Context, locationID int) (*Location, error)
// GetLocationByName returns a location by its name in the Megaport Locations API.
GetLocationByName(ctx context.Context, locationName string) (*Location, error)
// GetLocationByNameFuzzy returns a location by its name in the Megaport Locations API using fuzzy search.
GetLocationByNameFuzzy(ctx context.Context, search string) ([]*Location, error)
// ListCountries returns a list of all countries in the Megaport Network Regions API.
ListCountries(ctx context.Context) ([]*Country, error)
// ListMarketCodes returns a list of all market codes in the Megaport Network Regions API.
ListMarketCodes(ctx context.Context) ([]string, error)
// IsValidMarketCode checks if a market code is valid in the Megaport Network Regions API.
IsValidMarketCode(ctx context.Context, marketCode string) (bool, error)
// FilterLocationsByMarketCode filters locations by market code in the Megaport Locations API.
FilterLocationsByMarketCode(ctx context.Context, marketCode string, locations []*Location) ([]*Location, error)
// FilterLocationsByMcrAvailability filters locations by MCR availability in the Megaport Locations API.
FilterLocationsByMcrAvailability(ctx context.Context, mcrAvailable bool, locations []*Location) []*Location
}
// LocationServiceOp handles communication with Location methods of the Megaport API.
type LocationServiceOp struct {
Client *Client
}
// NewLocationService creates a new instance of the Location Service.
func NewLocationService(c *Client) *LocationServiceOp {
return &LocationServiceOp{
Client: c,
}
}
// Location represents a location in the Megaport API.
type Location struct {
Name string `json:"name"`
Country string `json:"country"`
LiveDate *Time `json:"liveDate"`
SiteCode string `json:"siteCode"`
NetworkRegion string `json:"networkRegion"`
Address map[string]string `json:"address"`
Campus string `json:"campus"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Products *LocationProducts `json:"products"`
Market string `json:"market"`
Metro string `json:"metro"`
VRouterAvailable bool `json:"vRouterAvailable"`
ID int `json:"id"`
Status string `json:"status"`
}
// LocationProducts represent the products available at a location in the Megaport API.
type LocationProducts struct {
MCR bool `json:"mcr"`
MCRVersion int `json:"mcrVersion"`
Megaport []int `json:"megaport"`
MVE []LocationMVE `json:"mve"`
MCR1 []int `json:"mcr1"`
MCR2 []int `json:"mcr2"`
}
// LocationMVE represents the MVE product available at a location in the Megaport API.
type LocationMVE struct {
Sizes []string `json:"sizes"`
Details []LocationMVEDetails `json:"details"`
MaxCPUCount int `json:"maxCpuCount"`
Version string `json:"version"`
Product string `json:"product"`
Vendor string `json:"vendor"`
VendorDescription string `json:"vendorDescription"`
ID int `json:"id"`
ReleaseImage bool `json:"releaseImage"`
}
// LocationMVEDetails represents the details of the MVE product available at a location in the Megaport API.
type LocationMVEDetails struct {
Size string `json:"size"`
Label string `json:"label"`
CPUCoreCount int `json:"cpuCoreCount"`
RamGB int `json:"ramGB"`
BandwidthMbps int `json:"bandwidthMbps"`
}
// Country represents a country in the Megaport Locations API.
type Country struct {
Code string `json:"code"`
Name string `json:"name"`
Prefix string `json:"prefix"`
SiteCount int `json:"siteCount"`
}
// LocationsResponse represents the response from the Megaport Locations API.
type LocationResponse struct {
Message string `json:"message"`
Terms string `json:"terms"`
Data []*Location `json:"data"`
}
// CountryResponse represents the response from the Megaport Network Regions API.
type CountryResponse struct {
Message string `json:"message"`
Terms string `json:"terms"`
Data []*CountryInnerResponse `json:"data"`
}
// CountriesInnerResponse represents the inner response from the Megaport Network Regions API.
type CountryInnerResponse struct {
Countries []*Country `json:"countries"`
NetworkRegion string `json:"networkRegion"`
}
// ProductLocationDetails represents the location details of a product.
type ProductLocationDetails struct {
Name string `json:"name"`
City string `json:"city"`
Metro string `json:"metro"`
Country string `json:"country"`
}
// ListLocations returns a list of all locations in the Megaport Locations API.
func (svc *LocationServiceOp) ListLocations(ctx context.Context) ([]*Location, error) {
path := "/v2/locations"
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, resErr := svc.Client.Do(ctx, clientReq, nil)
if resErr != nil {
return nil, resErr
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
locationResponse := &LocationResponse{}
unmarshalErr := json.Unmarshal(body, locationResponse)
if unmarshalErr != nil {
return nil, unmarshalErr
}
return locationResponse.Data, nil
}
// GetLocationByID returns a location by its ID in the Megaport Locations API.
func (svc *LocationServiceOp) GetLocationByID(ctx context.Context, locationID int) (*Location, error) {
allLocations, locErr := svc.ListLocations(ctx)
if locErr != nil {
return nil, locErr
}
for _, location := range allLocations {
if location.ID == locationID {
return location, nil
}
}
return nil, ErrLocationNotFound
}
// GetLocationByName returns a location by its name in the Megaport Locations API.
func (svc *LocationServiceOp) GetLocationByName(ctx context.Context, locationName string) (*Location, error) {
allLocations, locErr := svc.ListLocations(ctx)
if locErr != nil {
return nil, locErr
}
for _, location := range allLocations {
if location.Name == locationName {
return location, nil
}
}
return nil, ErrLocationNotFound
}
// GetLocationByNameFuzzy returns a location by its name in the Megaport Locations API using fuzzy search.
func (svc *LocationServiceOp) GetLocationByNameFuzzy(ctx context.Context, search string) ([]*Location, error) {
locations, err := svc.ListLocations(ctx)
if err != nil {
return nil, err
}
var matchedLocations []*Location
for i := 0; i < len(locations); i++ {
if fuzzy.Match(search, locations[i].Name) {
matchedLocations = append(matchedLocations, locations[i])
}
}
if len(matchedLocations) > 0 {
return matchedLocations, nil
} else {
return matchedLocations, ErrNoMatchingLocations
}
}
// ListCountries returns a list of all countries in the Megaport Network Regions API.
func (svc *LocationServiceOp) ListCountries(ctx context.Context) ([]*Country, error) {
path := "/v2/networkRegions"
url := svc.Client.BaseURL.JoinPath(path).String()
clientReq, err := svc.Client.NewRequest(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
response, resErr := svc.Client.Do(ctx, clientReq, nil)
if resErr != nil {
return nil, resErr
}
defer response.Body.Close()
body, fileErr := io.ReadAll(response.Body)
if fileErr != nil {
return nil, fileErr
}
countryResponse := CountryResponse{}
unmarshalErr := json.Unmarshal(body, &countryResponse)
if unmarshalErr != nil {
return nil, unmarshalErr
}
allCountries := make([]*Country, 0)
for i := 0; i < len(countryResponse.Data); i++ {
if countryResponse.Data[i].NetworkRegion == "MP1" {
allCountries = countryResponse.Data[i].Countries
}
}
return allCountries, nil
}
// ListMarketCodes returns a list of all market codes in the Megaport Network Regions API.
func (svc *LocationServiceOp) ListMarketCodes(ctx context.Context) ([]string, error) {
countries, countriesErr := svc.ListCountries(ctx)
if countriesErr != nil {
return nil, countriesErr
}
var marketCodes []string
for i := 0; i < len(countries); i++ {
marketCodes = append(marketCodes, countries[i].Prefix)
}
return marketCodes, nil
}
// IsValidMarketCode checks if a market code is valid in the Megaport Network Regions API.
func (svc *LocationServiceOp) IsValidMarketCode(ctx context.Context, marketCode string) (bool, error) {
found := false
marketCodes, err := svc.ListMarketCodes(ctx)
if err != nil {
return found, err
}
for i := 0; i < len(marketCodes); i++ {
if marketCodes[i] == marketCode {
found = true
}
}
return found, nil
}
// FilterLocationsByMarketCode filters locations by market code in the Megaport Locations API.
func (svc *LocationServiceOp) FilterLocationsByMarketCode(ctx context.Context, marketCode string, locations []*Location) ([]*Location, error) {
existingLocations := locations
toReturn := []*Location{}
isValid, err := svc.IsValidMarketCode(ctx, marketCode)
if err != nil {
return nil, err
}
if isValid {
for _, loc := range existingLocations {
if loc.Market == marketCode {
toReturn = append(toReturn, loc)
}
}
}
return toReturn, nil
}
// FilterLocationsByMcrAvailability filters locations by MCR availability in the Megaport Locations API.
func (svc *LocationServiceOp) FilterLocationsByMcrAvailability(ctx context.Context, mcrAvailable bool, locations []*Location) []*Location {
existingLocations := locations
toReturn := []*Location{}
for _, location := range existingLocations {
if location.Products.MCR == mcrAvailable {
toReturn = append(toReturn, location)
}
}
return toReturn
}