-
Notifications
You must be signed in to change notification settings - Fork 83
/
regions.go
65 lines (56 loc) · 1.71 KB
/
regions.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
package linodego
import (
"context"
"fmt"
"github.com/go-resty/resty/v2"
)
// Region represents a linode region object
type Region struct {
ID string `json:"id"`
Country string `json:"country"`
Capabilities []string `json:"capabilities"`
Status string `json:"status"`
Resolvers RegionResolvers `json:"resolvers"`
}
// RegionResolvers contains the DNS resolvers of a region
type RegionResolvers struct {
IPv4 string `json:"ipv4"`
IPv6 string `json:"ipv6"`
}
// RegionsPagedResponse represents a linode API response for listing
type RegionsPagedResponse struct {
*PageOptions
Data []Region `json:"data"`
}
// endpoint gets the endpoint URL for Region
func (RegionsPagedResponse) endpoint(_ ...any) string {
return "regions"
}
func (resp *RegionsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(RegionsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*RegionsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListRegions lists Regions
func (c *Client) ListRegions(ctx context.Context, opts *ListOptions) ([]Region, error) {
response := RegionsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetRegion gets the template with the provided ID
func (c *Client) GetRegion(ctx context.Context, regionID string) (*Region, error) {
e := fmt.Sprintf("regions/%s", regionID)
req := c.R(ctx).SetResult(&Region{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Region), nil
}