forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
126 lines (103 loc) · 3.7 KB
/
types.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
package linodego
import (
"context"
"fmt"
"net/url"
"github.com/go-resty/resty/v2"
)
// LinodeType represents a linode type object
type LinodeType struct {
ID string `json:"id"`
Disk int `json:"disk"`
Class LinodeTypeClass `json:"class"` // enum: nanode, standard, highmem, dedicated, gpu
Price *LinodePrice `json:"price"`
Label string `json:"label"`
Addons *LinodeAddons `json:"addons"`
RegionPrices []LinodeRegionPrice `json:"region_prices"`
NetworkOut int `json:"network_out"`
Memory int `json:"memory"`
Transfer int `json:"transfer"`
VCPUs int `json:"vcpus"`
GPUs int `json:"gpus"`
Successor string `json:"successor"`
}
// LinodePrice represents a linode type price object
type LinodePrice struct {
Hourly float32 `json:"hourly"`
Monthly float32 `json:"monthly"`
}
// LinodeBackupsAddon represents a linode backups addon object
type LinodeBackupsAddon struct {
Price *LinodePrice `json:"price"`
RegionPrices []LinodeRegionPrice `json:"region_prices"`
}
// LinodeAddons represent the linode addons object
type LinodeAddons struct {
Backups *LinodeBackupsAddon `json:"backups"`
}
// LinodeRegionPrice represents an individual type or addon
// price exception for a region.
type LinodeRegionPrice struct {
ID string `json:"id"`
Hourly float32 `json:"hourly"`
Monthly float32 `json:"monthly"`
}
// LinodeTypeClass constants start with Class and include Linode API Instance Type Classes
type LinodeTypeClass string
// LinodeTypeClass contants are the Instance Type Classes that an Instance Type can be assigned
const (
ClassNanode LinodeTypeClass = "nanode"
ClassStandard LinodeTypeClass = "standard"
ClassHighmem LinodeTypeClass = "highmem"
ClassDedicated LinodeTypeClass = "dedicated"
ClassGPU LinodeTypeClass = "gpu"
)
// LinodeTypesPagedResponse represents a linode types API response for listing
type LinodeTypesPagedResponse struct {
*PageOptions
Data []LinodeType `json:"data"`
}
func (*LinodeTypesPagedResponse) endpoint(_ ...any) string {
return "linode/types"
}
func (resp *LinodeTypesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LinodeTypesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LinodeTypesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListTypes lists linode types. This endpoint is cached by default.
func (c *Client) ListTypes(ctx context.Context, opts *ListOptions) ([]LinodeType, error) {
response := LinodeTypesPagedResponse{}
endpoint, err := generateListCacheURL(response.endpoint(), opts)
if err != nil {
return nil, err
}
if result := c.getCachedResponse(endpoint); result != nil {
return result.([]LinodeType), nil
}
err = c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
c.addCachedResponse(endpoint, response.Data, &cacheExpiryTime)
return response.Data, nil
}
// GetType gets the type with the provided ID. This endpoint is cached by default.
func (c *Client) GetType(ctx context.Context, typeID string) (*LinodeType, error) {
e := fmt.Sprintf("linode/types/%s", url.PathEscape(typeID))
if result := c.getCachedResponse(e); result != nil {
result := result.(LinodeType)
return &result, nil
}
req := c.R(ctx).SetResult(LinodeType{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
c.addCachedResponse(e, r.Result(), &cacheExpiryTime)
return r.Result().(*LinodeType), nil
}