congo.go
364 lines1
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package congo
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"congo.gg/pkg/platform"
)
type backend struct {
baseURL string
apiKey string
client *http.Client
}
// Compile-time interface checks
var _ platform.Backend = (*backend)(nil)
var _ platform.SSHKeyProvider = (*backend)(nil)
var _ platform.VPCProvider = (*backend)(nil)
var _ platform.LoadBalancerProvider = (*backend)(nil)
var _ platform.TagProvider = (*backend)(nil)
// Option configures the Congo platform provider.
type Option func(*backend)
// WithBaseURL sets the congo-host service URL.
func WithBaseURL(url string) Option {
return func(b *backend) { b.baseURL = url }
}
// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(c *http.Client) Option {
return func(b *backend) { b.client = c }
}
// New creates a Congo platform provider that delegates to host.congo.gg.
func New(apiKey string, opts ...Option) (*platform.Platform, error) {
if apiKey == "" {
return nil, fmt.Errorf("api key required")
}
b := &backend{
baseURL: "https://host.congo.gg",
apiKey: apiKey,
client: &http.Client{Timeout: 6 * time.Minute},
}
for _, opt := range opts {
opt(b)
}
return &platform.Platform{Backend: b}, nil
}
// do executes an HTTP request against the congo-host API.
func (b *backend) do(method, path string, body any) (*http.Response, error) {
var reqBody io.Reader
if body != nil {
data, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal request: %w", err)
}
reqBody = bytes.NewReader(data)
}
req, err := http.NewRequest(method, b.baseURL+path, reqBody)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+b.apiKey)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
return b.client.Do(req)
}
// mapError converts an HTTP error response to a platform error.
func (b *backend) mapError(resp *http.Response) error {
var e struct {
Error string `json:"error"`
Code string `json:"code"`
}
json.NewDecoder(io.LimitReader(resp.Body, 1<<20)).Decode(&e)
switch e.Code {
case "not_found":
return platform.ErrNotFound
case "timeout":
return platform.ErrTimeout
case "unsupported_region":
return fmt.Errorf("%w: %s", platform.ErrUnsupportedRegion, e.Error)
case "unsupported_size":
return fmt.Errorf("%w: %s", platform.ErrUnsupportedSize, e.Error)
case "unsupported":
return platform.ErrUnsupported
default:
return fmt.Errorf("congo host: %s", e.Error)
}
}
// decode reads a JSON response into v, handling errors.
func (b *backend) decode(resp *http.Response, v any) error {
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return b.mapError(resp)
}
return json.NewDecoder(resp.Body).Decode(v)
}
// Backend interface
func (b *backend) CreateServer(opts platform.ServerOptions) (*platform.Server, error) {
resp, err := b.do("POST", "/api/servers", map[string]any{
"name": opts.Name,
"size": string(opts.Size),
"region": string(opts.Region),
"image": opts.Image,
"ssh_key": opts.SSHKey,
"tags": opts.Tags,
"backups": opts.Backups,
"vpc_id": opts.VpcID,
})
if err != nil {
return nil, err
}
var s struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
PrivateIP string `json:"private_ip"`
Size string `json:"size"`
Region string `json:"region"`
Status string `json:"status"`
}
if err := b.decode(resp, &s); err != nil {
return nil, err
}
return &platform.Server{
ID: s.ID, Name: s.Name, IP: s.IP, PrivateIP: s.PrivateIP,
Size: s.Size, Region: s.Region, Status: s.Status,
}, nil
}
func (b *backend) GetServer(name string) (*platform.Server, error) {
resp, err := b.do("GET", "/api/servers/"+name, nil)
if err != nil {
return nil, err
}
var s struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
PrivateIP string `json:"private_ip"`
Size string `json:"size"`
Region string `json:"region"`
Status string `json:"status"`
}
if err := b.decode(resp, &s); err != nil {
return nil, err
}
return &platform.Server{
ID: s.ID, Name: s.Name, IP: s.IP, PrivateIP: s.PrivateIP,
Size: s.Size, Region: s.Region, Status: s.Status,
}, nil
}
func (b *backend) DeleteServer(id string) error {
resp, err := b.do("DELETE", "/api/servers/"+id, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return b.mapError(resp)
}
return nil
}
func (b *backend) CreateVolume(name string, sizeGB int, region platform.Region) (*platform.Volume, error) {
resp, err := b.do("POST", "/api/volumes", map[string]any{
"name": name,
"size_gb": sizeGB,
"region": string(region),
})
if err != nil {
return nil, err
}
var v struct {
ID string `json:"id"`
Name string `json:"name"`
Size int `json:"size"`
Region string `json:"region"`
}
if err := b.decode(resp, &v); err != nil {
return nil, err
}
return &platform.Volume{ID: v.ID, Name: v.Name, Size: v.Size, Region: v.Region}, nil
}
func (b *backend) GetVolume(name string) (*platform.Volume, error) {
resp, err := b.do("GET", "/api/volumes/"+name, nil)
if err != nil {
return nil, err
}
var v struct {
ID string `json:"id"`
Name string `json:"name"`
Size int `json:"size"`
Region string `json:"region"`
ServerID string `json:"server_id"`
}
if err := b.decode(resp, &v); err != nil {
return nil, err
}
return &platform.Volume{ID: v.ID, Name: v.Name, Size: v.Size, Region: v.Region, ServerID: v.ServerID}, nil
}
func (b *backend) AttachVolume(volumeID, serverID string) error {
resp, err := b.do("POST", "/api/volumes/"+volumeID+"/attach", map[string]string{
"server_id": serverID,
})
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return b.mapError(resp)
}
return nil
}
func (b *backend) DetachVolume(volumeID string) error {
resp, err := b.do("POST", "/api/volumes/"+volumeID+"/detach", nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return b.mapError(resp)
}
return nil
}
// SSHKeyProvider
func (b *backend) GetSSHKeyFingerprint(publicKey string) (string, error) {
resp, err := b.do("POST", "/api/ssh-keys", map[string]string{
"public_key": publicKey,
})
if err != nil {
return "", err
}
var result struct {
Fingerprint string `json:"fingerprint"`
}
if err := b.decode(resp, &result); err != nil {
return "", err
}
return result.Fingerprint, nil
}
// VPCProvider
func (b *backend) CreateVPC(name, region, description string) (string, error) {
resp, err := b.do("POST", "/api/vpcs", map[string]string{
"name": name, "region": region, "description": description,
})
if err != nil {
return "", err
}
var result struct {
ID string `json:"id"`
}
if err := b.decode(resp, &result); err != nil {
return "", err
}
return result.ID, nil
}
func (b *backend) GetVPC(name string) (string, error) {
resp, err := b.do("GET", "/api/vpcs/"+name, nil)
if err != nil {
return "", err
}
var result struct {
ID string `json:"id"`
}
if err := b.decode(resp, &result); err != nil {
return "", err
}
return result.ID, nil
}
// LoadBalancerProvider
func (b *backend) CreateLoadBalancer(cfg platform.LoadBalancerConfig) (*platform.LoadBalancer, error) {
resp, err := b.do("POST", "/api/load-balancers", map[string]any{
"name": cfg.Name,
"region": string(cfg.Region),
"vpc_id": cfg.VpcID,
"server_ids": cfg.ServerIDs,
"tags": cfg.Tags,
"port": cfg.Port,
"target_port": cfg.TargetPort,
"health_path": cfg.HealthPath,
})
if err != nil {
return nil, err
}
var lb struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
Status string `json:"status"`
}
if err := b.decode(resp, &lb); err != nil {
return nil, err
}
return &platform.LoadBalancer{ID: lb.ID, Name: lb.Name, IP: lb.IP, Status: lb.Status}, nil
}
func (b *backend) GetLoadBalancer(name string) (*platform.LoadBalancer, error) {
resp, err := b.do("GET", "/api/load-balancers/"+name, nil)
if err != nil {
return nil, err
}
var lb struct {
ID string `json:"id"`
Name string `json:"name"`
IP string `json:"ip"`
Status string `json:"status"`
}
if err := b.decode(resp, &lb); err != nil {
return nil, err
}
return &platform.LoadBalancer{ID: lb.ID, Name: lb.Name, IP: lb.IP, Status: lb.Status}, nil
}
func (b *backend) DeleteLoadBalancer(id string) error {
resp, err := b.do("DELETE", "/api/load-balancers/"+id, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return b.mapError(resp)
}
return nil
}
// TagProvider
func (b *backend) TagServer(serverID string, tag string) error {
resp, err := b.do("POST", "/api/servers/"+serverID+"/tag", map[string]string{
"tag": tag,
})
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return b.mapError(resp)
}
return nil
}