-
Notifications
You must be signed in to change notification settings - Fork 1
/
ignite.go
471 lines (434 loc) · 14.1 KB
/
ignite.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package hop
import (
"context"
"errors"
"net/url"
"go.hop.io/sdk/types"
)
// AddDomain is used to add a domain to the gateway. The parameter gatewayId is the ID of the gateway to add the domain to,
// and domain is the full name of the domain.
func (c ClientCategoryIgniteGateways) AddDomain(ctx context.Context, gatewayId string, domain string, opts ...ClientOption) error {
return c.c.do(ctx, ClientArgs{
Method: "POST",
Path: "/ignite/gateways/" + url.PathEscape(gatewayId) + "/domains",
Body: map[string]any{"domain": domain},
}, opts)
}
// GetDomain is used to get a domain by its ID.
func (c ClientCategoryIgniteGateways) GetDomain(
ctx context.Context, domainId string, opts ...ClientOption,
) (*types.Domain, error) {
var d types.Domain
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/domains/" + url.PathEscape(domainId),
ResultKey: "domain",
Result: &d,
}, opts)
if err != nil {
return nil, err
}
return &d, nil
}
// DeleteDomain is used to delete a domain by its ID.
func (c ClientCategoryIgniteGateways) DeleteDomain(ctx context.Context, domainId string, opts ...ClientOption) error {
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/ignite/domains/" + url.PathEscape(domainId),
}, opts)
}
// Get is used to get a gateway by its ID.
func (c ClientCategoryIgniteGateways) Get(ctx context.Context, id string, opts ...ClientOption) (*types.Gateway, error) {
var gw types.Gateway
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/gateways/" + url.PathEscape(id),
ResultKey: "gateway",
Result: &gw,
}, opts)
if err != nil {
return nil, err
}
return &gw, nil
}
// Delete is used to delete a gateway by its ID.
func (c ClientCategoryIgniteGateways) Delete(ctx context.Context, id string, opts ...ClientOption) error {
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/ignite/gateways/" + url.PathEscape(id),
}, opts)
}
// Update is used to update a gateway by its ID.
func (c ClientCategoryIgniteGateways) Update(
ctx context.Context, id string, updateOpts types.IgniteGatewayUpdateOpts, opts ...ClientOption,
) (*types.Gateway, error) {
var gw types.Gateway
err := c.c.do(ctx, ClientArgs{
Method: "PATCH",
Path: "/ignite/gateways/" + url.PathEscape(id),
ResultKey: "gateway",
Body: updateOpts,
Result: &gw,
}, opts)
if err != nil {
return nil, err
}
return &gw, nil
}
// Create is used to create a deployment.
func (c ClientCategoryIgniteDeployments) Create(
ctx context.Context, deployment *types.DeploymentConfig, opts ...ClientOption,
) (*types.Deployment, error) {
if c.c.getProjectId(opts) == "" {
if c.c.getTokenType() != "ptk" {
return nil, types.InvalidToken("project ID must be specified when using bearer authentication to make deployments")
}
} else {
if c.c.getTokenType() != "bearer" && c.c.getTokenType() != "pat" {
return nil, types.InvalidToken("project ID must not be specified if it is implied")
}
}
ramSize, err := deployment.Resources.RAM.Bytes()
if err != nil {
return nil, err
}
if 6e+6 >= ramSize {
return nil, errors.New("ram must be at least 6MB")
}
var d types.Deployment
err = c.c.do(ctx, ClientArgs{
Method: "POST",
Path: "/ignite/deployments",
ResultKey: "deployment",
Body: deployment,
Result: &d,
Ignore404: false,
}, opts)
if err != nil {
return nil, err
}
return &d, nil
}
// Get is used to get a deployment by its ID.
func (c ClientCategoryIgniteDeployments) Get(ctx context.Context, id string, opts ...ClientOption) (*types.Deployment, error) {
var d types.Deployment
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/" + url.PathEscape(id),
ResultKey: "deployment",
Result: &d,
}, opts)
if err != nil {
return nil, err
}
return &d, nil
}
// GetByName is used to get a deployment by its name.
func (c ClientCategoryIgniteDeployments) GetByName(ctx context.Context, name string, opts ...ClientOption) (*types.Deployment, error) {
var d types.Deployment
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/search",
Query: map[string]string{"name": name},
ResultKey: "deployment",
Result: &d,
}, opts)
if err != nil {
return nil, err
}
return &d, nil
}
// Update is used to update a deployment by its ID.
func (c ClientCategoryIgniteDeployments) Update(
ctx context.Context, id string, updateOpts types.IgniteDeploymentUpdateOpts, opts ...ClientOption,
) (*types.Deployment, error) {
var d types.Deployment
err := c.c.do(ctx, ClientArgs{
Method: "PATCH",
Path: "/ignite/deployments/" + url.PathEscape(id),
ResultKey: "deployment",
Body: updateOpts,
Result: &d,
}, opts)
if err != nil {
return nil, err
}
return &d, nil
}
// Patch is used to patch a deployment by its ID.
//
// Deprecated: use Update instead.
func (c ClientCategoryIgniteDeployments) Patch(
ctx context.Context, id string, patchOpts types.IgniteDeploymentPatchOpts, opts ...ClientOption,
) (*types.Deployment, error) {
return c.Update(ctx, id, patchOpts, opts...)
}
// GetContainers is used to get the containers of a deployment.
func (c ClientCategoryIgniteDeployments) GetContainers(ctx context.Context, id string, opts ...ClientOption) ([]*types.Container, error) {
var containers []*types.Container
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/" + url.PathEscape(id) + "/containers",
ResultKey: "containers",
Result: &containers,
}, opts)
if err != nil {
return nil, err
}
return containers, nil
}
// GetAll is used to get all deployments.
func (c ClientCategoryIgniteDeployments) GetAll(ctx context.Context, opts ...ClientOption) ([]*types.Deployment, error) {
var deployments []*types.Deployment
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments",
ResultKey: "deployments",
Result: &deployments,
}, opts)
if err != nil {
return nil, err
}
return deployments, nil
}
// Delete is used to delete a deployment by its ID.
func (c ClientCategoryIgniteDeployments) Delete(ctx context.Context, id string, opts ...ClientOption) error {
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/ignite/deployments/" + url.PathEscape(id),
}, opts)
}
// GetAllGateways is used to get all gateways attached to a deployment.
func (c ClientCategoryIgniteDeployments) GetAllGateways(ctx context.Context, id string, opts ...ClientOption) ([]*types.Gateway, error) {
var gateways []*types.Gateway
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/" + url.PathEscape(id) + "/gateways",
ResultKey: "gateways",
Result: &gateways,
}, opts)
if err != nil {
return nil, err
}
return gateways, nil
}
// CreateGateway is used to create a gateway attached to a deployment.
func (c ClientCategoryIgniteDeployments) CreateGateway(
ctx context.Context, opts types.GatewayCreationOptions, clientOpts ...ClientOption,
) (*types.Gateway, error) {
if opts.ProjectID != "" { //nolint:staticcheck // Support deprecated field.
clientOpts = append(clientOpts, WithProjectID(opts.ProjectID)) //nolint:staticcheck // Support deprecated field.
}
var gw types.Gateway
err := c.c.do(ctx, ClientArgs{
Method: "POST",
Path: "/ignite/deployments/" + url.PathEscape(opts.DeploymentID) + "/gateways",
Body: opts,
ResultKey: "gateway",
Result: &gw,
}, clientOpts)
if err != nil {
return nil, err
}
return &gw, nil
}
// Delete is used to delete a container by its ID.
func (c ClientCategoryIgniteContainers) Delete(ctx context.Context, id string, opts ...ClientOption) error {
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/ignite/containers/" + url.PathEscape(id),
}, opts)
}
// DeleteAndRecreate is used to delete a container by its ID and then recreate it. This is another function to avoid a
// breaking change.
func (c ClientCategoryIgniteContainers) DeleteAndRecreate(ctx context.Context, id string, opts ...ClientOption) error {
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/ignite/containers/" + url.PathEscape(id),
Query: map[string]string{"recreate": "true"},
}, opts)
}
// GetLogs is used to get a paginator for the logs of a container.
func (c ClientCategoryIgniteContainers) GetLogs(id string, limit int, ascOrder bool) *Paginator[*types.ContainerLog] {
orderBy := "desc"
if ascOrder {
orderBy = "asc"
}
return &Paginator[*types.ContainerLog]{
c: c.c,
total: -1,
offsetStrat: true,
limit: limit,
path: "/ignite/containers/" + url.PathEscape(id) + "/logs",
resultKey: "logs",
sortBy: "timestamp",
orderBy: orderBy,
}
}
// Used to update the container state.
func (c ClientCategoryIgniteContainers) updateContainerState(
ctx context.Context, id string, state types.ContainerState, opts []ClientOption,
) error {
return c.c.do(ctx, ClientArgs{
Method: "PUT",
Path: "/ignite/containers/" + url.PathEscape(id) + "/state",
Body: map[string]types.ContainerState{"preferred_state": state},
}, opts)
}
// Stop is used to stop a container by its ID.
func (c ClientCategoryIgniteContainers) Stop(ctx context.Context, id string, opts ...ClientOption) error {
return c.updateContainerState(ctx, id, types.ContainerStateStopped, opts)
}
// Start is used to start a container by its ID.
func (c ClientCategoryIgniteContainers) Start(ctx context.Context, id string, opts ...ClientOption) error {
return c.updateContainerState(ctx, id, types.ContainerStateRunning, opts)
}
// Create is used to create a container.
func (c ClientCategoryIgniteContainers) Create(ctx context.Context, deploymentId string, opts ...ClientOption) (*types.Container, error) {
var a []*types.Container
err := c.c.do(ctx, ClientArgs{
Method: "POST",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/containers",
ResultKey: "containers",
Result: &a,
}, opts)
if err != nil {
return nil, err
}
return a[0], nil
}
// Scale is used to scale the container count of a deployment.
func (c ClientCategoryIgniteDeployments) Scale(
ctx context.Context, deploymentId string, containerCount uint, opts ...ClientOption,
) ([]*types.Container, error) {
var a []*types.Container
err := c.c.do(ctx, ClientArgs{
Method: "PATCH",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/scale",
Body: map[string]uint{"scale": containerCount},
ResultKey: "containers",
Result: &a,
}, opts)
if err != nil {
return nil, err
}
return a, nil
}
// NewHealthCheck is used to set a health check on a deployment. Returns the health check ID.
func (c ClientCategoryIgniteDeployments) NewHealthCheck(
ctx context.Context, createOpts types.HealthCheckCreateOpts, opts ...ClientOption,
) (*types.HealthCheck, error) {
// Get the deployment ID.
deploymentId := createOpts.DeploymentID
createOpts.DeploymentID = ""
// Set the defaults.
if createOpts.Protocol == "" {
createOpts.Protocol = types.HealthCheckProtocolHTTP
}
if createOpts.Path == "" {
createOpts.Path = "/"
}
if createOpts.Port == 0 {
createOpts.Port = 8080
}
if createOpts.InitialDelay == 0 {
createOpts.InitialDelay = types.SecondsFromInt(5)
}
if createOpts.Interval == 0 {
createOpts.Interval = types.SecondsFromInt(60)
}
if createOpts.Timeout == 0 {
createOpts.Timeout = types.MillisecondsFromInt(50)
}
if createOpts.MaxRetries == 0 {
createOpts.MaxRetries = 3
}
// Do the HTTP request.
var res types.HealthCheck
err := c.c.do(ctx, ClientArgs{
Method: "POST",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/health-checks",
Body: createOpts,
ResultKey: "health_check",
Result: &res,
}, opts)
if err != nil {
return nil, err
}
return &res, nil
}
// GetHealthChecks is used to get the health checks attached to a deployment ID.
func (c ClientCategoryIgniteDeployments) GetHealthChecks(
ctx context.Context, deploymentId string, opts ...ClientOption,
) ([]*types.HealthCheck, error) {
var res []*types.HealthCheck
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/health-checks",
ResultKey: "health_checks",
Result: &res,
}, opts)
if err != nil {
return nil, err
}
return res, nil
}
// DeleteHealthCheck is used to delete a health check by its ID.
func (c ClientCategoryIgniteDeployments) DeleteHealthCheck(
ctx context.Context, deploymentId, healthCheckId string, opts ...ClientOption,
) error {
return c.c.do(ctx, ClientArgs{
Method: "DELETE",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/health-checks/" +
url.PathEscape(healthCheckId),
}, opts)
}
// UpdateHealthCheck is used to update a health check.
func (c ClientCategoryIgniteDeployments) UpdateHealthCheck(
ctx context.Context, updateOpts types.HealthCheckUpdateOpts, opts ...ClientOption,
) (*types.HealthCheck, error) {
var res types.HealthCheck
err := c.c.do(ctx, ClientArgs{
Method: "PATCH",
Path: "/ignite/deployments/" + url.PathEscape(updateOpts.DeploymentID) + "/health-checks/" +
url.PathEscape(updateOpts.HealthCheckID),
Body: updateOpts,
ResultKey: "health_check",
Result: &res,
}, opts)
if err != nil {
return nil, err
}
return &res, nil
}
// HealthCheckStates is used to get the state of health checks for a deployment.
func (c ClientCategoryIgniteDeployments) HealthCheckStates(
ctx context.Context, deploymentId string, opts ...ClientOption,
) ([]*types.HealthCheckState, error) {
var res []*types.HealthCheckState
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/health-check-state",
ResultKey: "health_check_states",
Result: &res,
}, opts)
if err != nil {
return nil, err
}
return res, nil
}
// GetStorageStats is used to get stats about storage.
func (c ClientCategoryIgniteDeployments) GetStorageStats(
ctx context.Context, deploymentId string, opts ...ClientOption,
) (types.DeploymentStorageInfo, error) {
var res types.DeploymentStorageInfo
err := c.c.do(ctx, ClientArgs{
Method: "GET",
Path: "/ignite/deployments/" + url.PathEscape(deploymentId) + "/storage",
Result: &res,
}, opts)
if err != nil {
return types.DeploymentStorageInfo{}, err
}
return res, nil
}