-
Notifications
You must be signed in to change notification settings - Fork 78
/
regions.go
130 lines (108 loc) · 3.98 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
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
package linodego
import (
"context"
"fmt"
"net/url"
"time"
"github.com/go-resty/resty/v2"
)
// This is an enumeration of Capabilities Linode offers that can be referenced
// through the user-facing parts of the application.
// Defined as strings rather than a custom type to avoid breaking change.
// Can be changed in the potential v2 version.
const (
Linodes string = "Linodes"
NodeBalancers string = "NodeBalancers"
BlockStorage string = "Block Storage"
ObjectStorage string = "Object Storage"
ObjectStorageRegions string = "Object Storage Access Key Regions"
LKE string = "Kubernetes"
LkeHaControlPlanes string = "LKE HA Control Planes"
CloudFirewall string = "Cloud Firewall"
GPU string = "GPU Linodes"
Vlans string = "Vlans"
VPCs string = "VPCs"
VPCsExtra string = "VPCs Extra"
MachineImages string = "Machine Images"
BareMetal string = "Bare Metal"
DBAAS string = "Managed Databases"
BlockStorageMigrations string = "Block Storage Migrations"
Metadata string = "Metadata"
PremiumPlans string = "Premium Plans"
EdgePlans string = "Edge Plans"
LKEControlPlaneACL string = "LKE Network Access Control List (IP ACL)"
ACLB string = "Akamai Cloud Load Balancer"
SupportTicketSeverity string = "Support Ticket Severity"
Backups string = "Backups"
PlacementGroup string = "Placement Group"
DiskEncryption string = "Disk Encryption"
)
// Region-related endpoints have a custom expiry time as the
// `status` field may update for database outages.
var cacheExpiryTime = time.Minute
// Region represents a linode region object
type Region struct {
ID string `json:"id"`
Country string `json:"country"`
// A List of enums from the above constants
Capabilities []string `json:"capabilities"`
Status string `json:"status"`
Resolvers RegionResolvers `json:"resolvers"`
Label string `json:"label"`
SiteType string `json:"site_type"`
}
// 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. This endpoint is cached by default.
func (c *Client) ListRegions(ctx context.Context, opts *ListOptions) ([]Region, error) {
response := RegionsPagedResponse{}
endpoint, err := generateListCacheURL(response.endpoint(), opts)
if err != nil {
return nil, err
}
if result := c.getCachedResponse(endpoint); result != nil {
return result.([]Region), nil
}
err = c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)
return response.Data, nil
}
// GetRegion gets the template with the provided ID. This endpoint is cached by default.
func (c *Client) GetRegion(ctx context.Context, regionID string) (*Region, error) {
e := fmt.Sprintf("regions/%s", url.PathEscape(regionID))
if result := c.getCachedResponse(e); result != nil {
result := result.(Region)
return &result, nil
}
req := c.R(ctx).SetResult(&Region{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
c.addCachedResponse(e, r.Result(), &cacheExpiryTime)
return r.Result().(*Region), nil
}