forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernels.go
109 lines (87 loc) · 2.72 KB
/
kernels.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
package linodego
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)
// LinodeKernel represents a Linode Instance kernel object
type LinodeKernel struct {
ID string `json:"id"`
Label string `json:"label"`
Version string `json:"version"`
Architecture string `json:"architecture"`
Deprecated bool `json:"deprecated"`
KVM bool `json:"kvm"`
XEN bool `json:"xen"`
PVOPS bool `json:"pvops"`
Built *time.Time `json:"-"`
}
// LinodeKernelsPagedResponse represents a Linode kernels API response for listing
type LinodeKernelsPagedResponse struct {
*PageOptions
Data []LinodeKernel `json:"data"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (i *LinodeKernel) UnmarshalJSON(b []byte) error {
type Mask LinodeKernel
p := struct {
*Mask
Built *parseabletime.ParseableTime `json:"built"`
}{
Mask: (*Mask)(i),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
i.Built = (*time.Time)(p.Built)
return nil
}
func (LinodeKernelsPagedResponse) endpoint(_ ...any) string {
return "linode/kernels"
}
func (resp *LinodeKernelsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(LinodeKernelsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*LinodeKernelsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListKernels lists linode kernels. This endpoint is cached by default.
func (c *Client) ListKernels(ctx context.Context, opts *ListOptions) ([]LinodeKernel, error) {
response := LinodeKernelsPagedResponse{}
endpoint, err := generateListCacheURL(response.endpoint(), opts)
if err != nil {
return nil, err
}
if result := c.getCachedResponse(endpoint); result != nil {
return result.([]LinodeKernel), nil
}
err = c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
c.addCachedResponse(endpoint, response.Data, nil)
return response.Data, nil
}
// GetKernel gets the kernel with the provided ID. This endpoint is cached by default.
func (c *Client) GetKernel(ctx context.Context, kernelID string) (*LinodeKernel, error) {
kernelID = url.PathEscape(kernelID)
e := fmt.Sprintf("linode/kernels/%s", kernelID)
if result := c.getCachedResponse(e); result != nil {
result := result.(LinodeKernel)
return &result, nil
}
req := c.R(ctx).SetResult(&LinodeKernel{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
c.addCachedResponse(e, r.Result(), nil)
return r.Result().(*LinodeKernel), nil
}