forked from firecracker-microvm/firecracker-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firecracker.go
521 lines (414 loc) · 19.1 KB
/
firecracker.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package firecracker
import (
"context"
"time"
"github.com/go-openapi/strfmt"
"github.com/sirupsen/logrus"
"github.com/firecracker-microvm/firecracker-go-sdk/client"
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models"
ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
)
const (
// env name to make firecracker request timeout configurable
firecrackerRequestTimeoutEnv = "FIRECRACKER_GO_SDK_REQUEST_TIMEOUT_MILLISECONDS"
defaultFirecrackerRequestTimeout = 500
)
// newFirecrackerClient creates a FirecrackerClient
func newFirecrackerClient(socketPath string, logger *logrus.Entry, debug bool) *client.Firecracker {
httpClient := client.NewHTTPClient(strfmt.NewFormats())
transport := NewUnixSocketTransport(socketPath, logger, debug)
httpClient.SetTransport(transport)
return httpClient
}
// ClientOpt is a functional option used to modify the client after construction.
type ClientOpt func(*Client)
// WithOpsClient will return a functional option and replace the operations
// client. This is useful for mock and stub testing.
func WithOpsClient(opsClient ops.ClientIface) ClientOpt {
return func(c *Client) {
c.client.Operations = opsClient
}
}
// Client is a client for interacting with the Firecracker API
type Client struct {
client *client.Firecracker
firecrackerRequestTimeout int
firecrackerInitTimeout int
}
// NewClient creates a Client
func NewClient(socketPath string, logger *logrus.Entry, debug bool, opts ...ClientOpt) *Client {
httpClient := newFirecrackerClient(socketPath, logger, debug)
c := &Client{client: httpClient}
c.firecrackerRequestTimeout = envValueOrDefaultInt(firecrackerRequestTimeoutEnv, defaultFirecrackerRequestTimeout)
c.firecrackerInitTimeout = envValueOrDefaultInt(firecrackerInitTimeoutEnv, defaultFirecrackerInitTimeoutSeconds)
for _, opt := range opts {
opt(c)
}
return c
}
// GetFirecrackerVersionOpt is a functional option to be used for the
// GetFirecrackerVersion API in setting any additional optional fields.
type GetFirecrackerVersionOpt func(*ops.GetFirecrackerVersionParams)
// GetFirecrackerVersion is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) GetFirecrackerVersion(ctx context.Context, opts ...GetFirecrackerVersionOpt) (*ops.GetFirecrackerVersionOK, error) {
params := ops.NewGetFirecrackerVersionParams()
params.SetContext(ctx)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.GetFirecrackerVersion(params)
}
// PutLoggerOpt is a functional option to be used for the PutLogger API in
// setting any additional optional fields.
type PutLoggerOpt func(*ops.PutLoggerParams)
// PutLogger is a wrapper for the swagger generated client to make calling of
// the API easier.
func (f *Client) PutLogger(ctx context.Context, logger *models.Logger, opts ...PutLoggerOpt) (*ops.PutLoggerNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
loggerParams := ops.NewPutLoggerParamsWithContext(timeout)
loggerParams.SetBody(logger)
for _, opt := range opts {
opt(loggerParams)
}
return f.client.Operations.PutLogger(loggerParams)
}
// PutMetricsOpt is a functional option to be used for the PutMetrics API in
// setting any additional optional fields.
type PutMetricsOpt func(*ops.PutMetricsParams)
// PutMetrics is a wrapper for the swagger generated client to make calling of
// the API easier.
func (f *Client) PutMetrics(ctx context.Context, metrics *models.Metrics, opts ...PutMetricsOpt) (*ops.PutMetricsNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
params := ops.NewPutMetricsParamsWithContext(timeout)
params.SetBody(metrics)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PutMetrics(params)
}
// PutMachineConfigurationOpt is a functional option to be used for the
// PutMachineConfiguration API in setting any additional optional fields.
type PutMachineConfigurationOpt func(*ops.PutMachineConfigurationParams)
// PutMachineConfiguration is a wrapper for the swagger generated client to
// make calling of the API easier.
func (f *Client) PutMachineConfiguration(ctx context.Context, cfg *models.MachineConfiguration, opts ...PutMachineConfigurationOpt) (*ops.PutMachineConfigurationNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
mc := ops.NewPutMachineConfigurationParamsWithContext(timeout)
mc.SetBody(cfg)
for _, opt := range opts {
opt(mc)
}
return f.client.Operations.PutMachineConfiguration(mc)
}
// PutGuestBootSourceOpt is a functional option to be used for the
// PutGuestBootSource API in setting any additional optional fields.
type PutGuestBootSourceOpt func(*ops.PutGuestBootSourceParams)
// PutGuestBootSource is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) PutGuestBootSource(ctx context.Context, source *models.BootSource, opts ...PutGuestBootSourceOpt) (*ops.PutGuestBootSourceNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
bootSource := ops.NewPutGuestBootSourceParamsWithContext(timeout)
bootSource.SetBody(source)
for _, opt := range opts {
opt(bootSource)
}
return f.client.Operations.PutGuestBootSource(bootSource)
}
// PutGuestNetworkInterfaceByIDOpt is a functional option to be used for the
// PutGuestNetworkInterfaceByID API in setting any additional optional fields.
type PutGuestNetworkInterfaceByIDOpt func(*ops.PutGuestNetworkInterfaceByIDParams)
// PutGuestNetworkInterfaceByID is a wrapper for the swagger generated client
// to make calling of the API easier.
func (f *Client) PutGuestNetworkInterfaceByID(ctx context.Context, ifaceID string, ifaceCfg *models.NetworkInterface, opts ...PutGuestNetworkInterfaceByIDOpt) (*ops.PutGuestNetworkInterfaceByIDNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
cfg := ops.NewPutGuestNetworkInterfaceByIDParamsWithContext(timeout)
cfg.SetBody(ifaceCfg)
cfg.SetIfaceID(ifaceID)
for _, opt := range opts {
opt(cfg)
}
return f.client.Operations.PutGuestNetworkInterfaceByID(cfg)
}
// PatchGuestNetworkInterfaceByIDOpt is a functional option to be used for the
// PatchGuestNetworkInterfaceByID API in setting any additional optional fields.
type PatchGuestNetworkInterfaceByIDOpt func(*ops.PatchGuestNetworkInterfaceByIDParams)
// PatchGuestNetworkInterfaceByID is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PatchGuestNetworkInterfaceByID(ctx context.Context, ifaceID string, ifaceCfg *models.PartialNetworkInterface, opts ...PatchGuestNetworkInterfaceByIDOpt) (*ops.PatchGuestNetworkInterfaceByIDNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
cfg := ops.NewPatchGuestNetworkInterfaceByIDParamsWithContext(timeout)
cfg.SetBody(ifaceCfg)
cfg.SetIfaceID(ifaceID)
for _, opt := range opts {
opt(cfg)
}
return f.client.Operations.PatchGuestNetworkInterfaceByID(cfg)
}
// PutGuestDriveByIDOpt is a functional option to be used for the
// PutGuestDriveByID API in setting any additional optional fields.
type PutGuestDriveByIDOpt func(*ops.PutGuestDriveByIDParams)
// PutGuestDriveByID is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) PutGuestDriveByID(ctx context.Context, driveID string, drive *models.Drive, opts ...PutGuestDriveByIDOpt) (*ops.PutGuestDriveByIDNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)/2*time.Millisecond)
defer cancel()
params := ops.NewPutGuestDriveByIDParamsWithContext(timeout)
params.SetDriveID(driveID)
params.SetBody(drive)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PutGuestDriveByID(params)
}
// PutGuestVsockOpt is a functional option to be used for the
// PutGuestVsock API in setting any additional optional fields.
type PutGuestVsockOpt func(params *ops.PutGuestVsockParams)
// PutGuestVsock is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) PutGuestVsock(ctx context.Context, vsock *models.Vsock, opts ...PutGuestVsockOpt) (*ops.PutGuestVsockNoContent, error) {
params := ops.NewPutGuestVsockParams()
params.SetContext(ctx)
params.SetBody(vsock)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PutGuestVsock(params)
}
// PatchVMOpt is a functional option to be used for the
// PatchVM API in setting any additional optional fields.
type PatchVMOpt func(*ops.PatchVMParams)
// PatchVM is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) PatchVM(ctx context.Context, vm *models.VM, opts ...PatchVMOpt) (*ops.PatchVMNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
params := ops.NewPatchVMParamsWithContext(timeout)
params.SetBody(vm)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PatchVM(params)
}
// CreateSnapshotOpt is a functional option to be used for the
// CreateSnapshot API in setting any additional optional fields.
type CreateSnapshotOpt func(*ops.CreateSnapshotParams)
// CreateSnapshot is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) CreateSnapshot(ctx context.Context, snapshotParams *models.SnapshotCreateParams, opts ...CreateSnapshotOpt) (*ops.CreateSnapshotNoContent, error) {
params := ops.NewCreateSnapshotParamsWithContext(ctx)
params.SetBody(snapshotParams)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.CreateSnapshot(params)
}
// LoadSnapshotOpt is a functional option to be used for the
// LoadSnapshot API in setting any additional optional fields.
type LoadSnapshotOpt func(*ops.LoadSnapshotParams)
// LoadSnapshot is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) LoadSnapshot(ctx context.Context, snapshotParams *models.SnapshotLoadParams, opts ...LoadSnapshotOpt) (*ops.LoadSnapshotNoContent, error) {
params := ops.NewLoadSnapshotParamsWithContext(ctx)
params.SetBody(snapshotParams)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.LoadSnapshot(params)
}
// CreateSyncActionOpt is a functional option to be used for the
// CreateSyncAction API in setting any additional optional fields.
type CreateSyncActionOpt func(*ops.CreateSyncActionParams)
// CreateSyncAction is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) CreateSyncAction(ctx context.Context, info *models.InstanceActionInfo, opts ...CreateSyncActionOpt) (*ops.CreateSyncActionNoContent, error) {
params := ops.NewCreateSyncActionParams()
params.SetContext(ctx)
params.SetInfo(info)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.CreateSyncAction(params)
}
// PutMmdsOpt is a functional option to be used for the PutMmds API in setting
// any additional optional fields.
type PutMmdsOpt func(*ops.PutMmdsParams)
// PutMmds is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PutMmds(ctx context.Context, metadata interface{}, opts ...PutMmdsOpt) (*ops.PutMmdsNoContent, error) {
params := ops.NewPutMmdsParams()
params.SetContext(ctx)
params.SetBody(metadata)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PutMmds(params)
}
// GetMmdsOpt is a functional option to be used for the GetMmds API in setting
// any additional optional fields.
type GetMmdsOpt func(*ops.GetMmdsParams)
// GetMmds is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) GetMmds(ctx context.Context, opts ...GetMmdsOpt) (*ops.GetMmdsOK, error) {
params := ops.NewGetMmdsParams()
params.SetContext(ctx)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.GetMmds(params)
}
// PatchMmdsOpt is a functional option to be used for the GetMmds API in setting
// any additional optional fields.
type PatchMmdsOpt func(*ops.PatchMmdsParams)
// PatchMmds is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PatchMmds(ctx context.Context, metadata interface{}, opts ...PatchMmdsOpt) (*ops.PatchMmdsNoContent, error) {
params := ops.NewPatchMmdsParams()
params.SetContext(ctx)
params.SetBody(metadata)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PatchMmds(params)
}
// PutMmdsConfig is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PutMmdsConfig(ctx context.Context, config *models.MmdsConfig) (*ops.PutMmdsConfigNoContent, error) {
params := ops.NewPutMmdsConfigParams()
params.SetContext(ctx)
params.SetBody(config)
return f.client.Operations.PutMmdsConfig(params)
}
// GetMachineConfigurationOpt is a functional option to be used for the
// GetMachineConfiguration API in setting any additional optional fields.
type GetMachineConfigurationOpt func(*ops.GetMachineConfigurationParams)
// GetMachineConfiguration is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) GetMachineConfiguration(opts ...GetMachineConfigurationOpt) (*ops.GetMachineConfigurationOK, error) {
p := ops.NewGetMachineConfigurationParams()
p.SetTimeout(time.Duration(f.firecrackerRequestTimeout) * time.Millisecond)
for _, opt := range opts {
opt(p)
}
return f.client.Operations.GetMachineConfiguration(p)
}
// DescribeInstanceOpt is a functional option to be used for the DescribeInstance API
// for any additional optional fields
type DescribeInstanceOpt func(*ops.DescribeInstanceParams)
// GetInstanceInfo is a wrapper for the swagger generated client to make calling of
// the API easier
func (f *Client) GetInstanceInfo(ctx context.Context, opts ...DescribeInstanceOpt) (*ops.DescribeInstanceOK, error) {
params := ops.NewDescribeInstanceParams()
params.SetContext(ctx)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.DescribeInstance(params)
}
// PatchGuestDriveByIDOpt is a functional option to be used for the PutMmds API in setting
// any additional optional fields.
type PatchGuestDriveByIDOpt func(*ops.PatchGuestDriveByIDParams)
// PatchGuestDriveByID is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PatchGuestDriveByID(ctx context.Context, driveID, pathOnHost string, opts ...PatchGuestDriveByIDOpt) (*ops.PatchGuestDriveByIDNoContent, error) {
params := ops.NewPatchGuestDriveByIDParams()
params.SetContext(ctx)
partialDrive := models.PartialDrive{
DriveID: &driveID,
PathOnHost: pathOnHost,
}
params.SetBody(&partialDrive)
params.DriveID = driveID
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PatchGuestDriveByID(params)
}
// PutBalloonOpt is a functional option to be used for the
// PutBalloon API in setting any additional optional fields.
type PutBalloonOpt func(*ops.PutBalloonParams)
// PutBalloonOpt is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) PutBalloon(ctx context.Context, balloon *models.Balloon, opts ...PutBalloonOpt) (*ops.PutBalloonNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
params := ops.NewPutBalloonParamsWithContext(timeout)
params.SetBody(balloon)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PutBalloon(params)
}
// DescribeBalloonConfig is a wrapper for the swagger generated client to make
// calling of the API easier.
func (f *Client) DescribeBalloonConfig(ctx context.Context) (*ops.DescribeBalloonConfigOK, error) {
params := ops.NewDescribeBalloonConfigParams()
params.SetContext(ctx)
params.SetTimeout(time.Duration(f.firecrackerRequestTimeout) * time.Millisecond)
return f.client.Operations.DescribeBalloonConfig(params)
}
// PatchBalloonOpt is a functional option to be used for the PatchBalloon API in setting
// any additional optional fields.
type PatchBalloonOpt func(*ops.PatchBalloonParams)
// PatchBalloon is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PatchBalloon(ctx context.Context, ballonUpdate *models.BalloonUpdate, opts ...PatchBalloonOpt) (*ops.PatchBalloonNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
params := ops.NewPatchBalloonParamsWithContext(timeout)
params.SetBody(ballonUpdate)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PatchBalloon(params)
}
// DescribeBalloonStats is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) DescribeBalloonStats(ctx context.Context) (*ops.DescribeBalloonStatsOK, error) {
params := ops.NewDescribeBalloonStatsParams()
params.SetContext(ctx)
params.SetTimeout(time.Duration(f.firecrackerRequestTimeout) * time.Millisecond)
return f.client.Operations.DescribeBalloonStats(params)
}
// PatchBalloonStatsIntervalOpt is a functional option to be used for the PatchBalloonStatsInterval API in setting
// any additional optional fields.
type PatchBalloonStatsIntervalOpt func(*ops.PatchBalloonStatsIntervalParams)
// PatchBalloonStatsInterval is a wrapper for the swagger generated client to make calling of the
// API easier.
func (f *Client) PatchBalloonStatsInterval(ctx context.Context, balloonStatsUpdate *models.BalloonStatsUpdate, opts ...PatchBalloonStatsIntervalOpt) (*ops.PatchBalloonStatsIntervalNoContent, error) {
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
defer cancel()
params := ops.NewPatchBalloonStatsIntervalParamsWithContext(timeout)
params.SetBody(balloonStatsUpdate)
for _, opt := range opts {
opt(params)
}
return f.client.Operations.PatchBalloonStatsInterval(params)
}
type GetExportVMConfigOpt func(*ops.GetExportVMConfigParams)
func (f *Client) GetExportVMConfig(opts ...GetExportVMConfigOpt) (*ops.GetExportVMConfigOK, error) {
p := ops.NewGetExportVMConfigParams()
p.SetTimeout(time.Duration(f.firecrackerRequestTimeout) * time.Millisecond)
for _, opt := range opts {
opt(p)
}
return f.client.Operations.GetExportVMConfig(p)
}