-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinode.go
More file actions
189 lines (167 loc) · 4.8 KB
/
Copy pathlinode.go
File metadata and controls
189 lines (167 loc) · 4.8 KB
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
package capstan
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
)
type LinodeProvider struct {
spec *ProviderSpec
http *httpClient
}
func NewLinode(token string) *LinodeProvider {
return &LinodeProvider{
spec: Spec(Linode),
http: newHTTPClient("linode", token),
}
}
func (l *LinodeProvider) Name() ProviderName { return Linode }
func (l *LinodeProvider) Regions(ctx context.Context) ([]Region, error) {
var resp struct {
Data []struct {
ID string `json:"id"`
Label string `json:"label"`
Country string `json:"country"`
Status string `json:"status"`
} `json:"data"`
}
if err := l.http.GET(ctx, l.spec.BaseURL,"/regions?page_size=100", &resp); err != nil {
return nil, err
}
var regions []Region
for _, r := range resp.Data {
if r.Status != "ok" {
continue
}
regions = append(regions, Region{ID: r.ID, Name: r.Label, Country: r.Country})
}
return regions, nil
}
func (l *LinodeProvider) Plans(ctx context.Context, region string) ([]Plan, error) {
var resp struct {
Data []struct {
ID string `json:"id"`
Label string `json:"label"`
VCPUs int `json:"vcpus"`
Memory int `json:"memory"`
Disk int `json:"disk"`
Price struct {
Monthly float64 `json:"monthly"`
} `json:"price"`
} `json:"data"`
}
if err := l.http.GET(ctx, l.spec.BaseURL,"/linode/types?page_size=100", &resp); err != nil {
return nil, err
}
plans := make([]Plan, len(resp.Data))
for i, t := range resp.Data {
plans[i] = Plan{
ID: t.ID,
Name: t.Label,
CPUs: t.VCPUs,
MemoryMB: t.Memory,
DiskGB: t.Disk / 1024,
MonthlyCents: l.spec.EstimateMonthlyCost(t.ID),
PriceCurrency: l.spec.PriceCurrency,
APIMonthlyCents: dollarsToCents(t.Price.Monthly),
}
}
return plans, nil
}
func (l *LinodeProvider) Create(ctx context.Context, opts CreateOpts) (*Server, error) {
rootPass, err := randomRootPass()
if err != nil {
return nil, fmt.Errorf("capstan: linode generate root_pass: %w", err)
}
body := map[string]any{
"label": opts.Name,
"type": opts.Plan,
"region": opts.Region,
"image": l.spec.ResolveImage(opts.Image),
"root_pass": rootPass,
}
if opts.UserData != "" {
body["metadata"] = map[string]any{
"user_data": base64.StdEncoding.EncodeToString([]byte(opts.UserData)),
}
}
var s linodeInstance
if err := l.http.POST(ctx, l.spec.BaseURL,"/linode/instances", body, &s); err != nil {
return nil, err
}
return l.toServer(s), nil
}
func (l *LinodeProvider) Get(ctx context.Context, id string) (*Server, error) {
var s linodeInstance
if err := l.http.GET(ctx, l.spec.BaseURL,"/linode/instances/"+id, &s); err != nil {
return nil, err
}
return l.toServer(s), nil
}
func (l *LinodeProvider) Destroy(ctx context.Context, id string) error {
return l.http.DELETE(ctx, l.spec.BaseURL,"/linode/instances/"+id)
}
func (l *LinodeProvider) EstimateMonthlyCost(plan string) int {
return l.spec.EstimateMonthlyCost(plan)
}
// List, PowerOn/Off/Restart, WaitForAction will be implemented for
// Linode as a follow-up. Hetzner is the reference implementation; the
// interface is stable and additive once it lands.
func (l *LinodeProvider) List(ctx context.Context, opts ListOpts) ([]Server, error) {
return nil, ErrNotImplemented
}
func (l *LinodeProvider) PowerOn(ctx context.Context, id string) (*Action, error) {
return nil, ErrNotImplemented
}
func (l *LinodeProvider) PowerOff(ctx context.Context, id string) (*Action, error) {
return nil, ErrNotImplemented
}
func (l *LinodeProvider) Restart(ctx context.Context, id string) (*Action, error) {
return nil, ErrNotImplemented
}
func (l *LinodeProvider) WaitForAction(ctx context.Context, actionID string) (*Action, error) {
return nil, ErrNotImplemented
}
type linodeInstance struct {
ID int `json:"id"`
Label string `json:"label"`
Status string `json:"status"`
Type string `json:"type"`
Region string `json:"region"`
IPv4 []string `json:"ipv4"`
IPv6 string `json:"ipv6"`
Created string `json:"created"`
}
func (l *LinodeProvider) toServer(s linodeInstance) *Server {
srv := &Server{
ID: fmt.Sprintf("%d", s.ID),
Name: s.Label,
Status: l.spec.MapStatus(s.Status),
Plan: s.Type,
Region: s.Region,
CreatedAt: s.Created,
}
if len(s.IPv4) > 0 {
srv.PublicIPv4 = s.IPv4[0]
}
if s.IPv6 != "" {
// Strip CIDR prefix if present (e.g. "2001:db8::1/64" -> "2001:db8::1")
ipv6 := s.IPv6
for i, c := range ipv6 {
if c == '/' {
ipv6 = ipv6[:i]
break
}
}
srv.PublicIPv6 = ipv6
}
return srv
}
// randomRootPass generates a cryptographically random 32-byte base64url string.
func randomRootPass() (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(b), nil
}