-
Notifications
You must be signed in to change notification settings - Fork 20
/
disk.go
850 lines (746 loc) · 27.6 KB
/
disk.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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
package ovirtclient
import (
"io"
"strings"
"sync"
ovirtsdk4 "github.com/ovirt/go-ovirt"
)
//go:generate go run scripts/rest/rest.go -i "Disk" -n "disk" -T DiskID
// DiskID is the identifier for disks.
type DiskID string
// DiskClient is the client interface part that deals with disks.
type DiskClient interface {
// StartImageUpload uploads an image file into a disk. The actual upload takes place in the
// background and can be tracked using the returned UploadImageProgress object.
//
// Parameters are as follows:
//
// - alias: this is the name used for the uploaded image.
// - storageDomainID: this is the UUID of the storage domain that the image should be uploaded to.
// - sparse: use sparse provisioning
// - size: this is the file size of the image. This must match the bytes read.
// - reader: this is the source of the image data.
// - retries: a set of optional retry options.
//
// You can wait for the upload to complete using the Done() method:
//
// progress, err := cli.StartImageUpload(...)
// if err != nil {
// //...
// }
// <-progress.Done()
//
// After the upload is complete you can check the Err() method if it completed successfully:
//
// if err := progress.Err(); err != nil {
// //...
// }
//
// Deprecated: Use StartUploadToNewDisk instead.
StartImageUpload(
alias string,
storageDomainID StorageDomainID,
sparse bool,
size uint64,
reader io.ReadSeekCloser,
retries ...RetryStrategy,
) (UploadImageProgress, error)
// StartUploadToNewDisk uploads an image file into a disk. The actual upload takes place in the
// background and can be tracked using the returned UploadImageProgress object. If the process fails a removal
// of the created disk is attempted.
//
// Parameters are as follows:
//
// - storageDomainID: this is the UUID of the storage domain that the image should be uploaded to.
// - format: format of the created disk. This does not necessarily have to be identical to the format of the image
// being uploaded as the oVirt engine converts images on upload.
// - size: file size of the uploaded image on the disk.
// - reader: this is the source of the image data. It is a reader that must support seek and close operations.
// - retries: a set of optional retry options.
//
// You can wait for the upload to complete using the Done() method:
//
// progress, err := cli.StartUploadToNewDisk(...)
// if err != nil {
// //...
// }
// <-progress.Done()
//
// After the upload is complete you can check the Err() method if it completed successfully:
//
// if err := progress.Err(); err != nil {
// //...
// }
StartUploadToNewDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
reader io.ReadSeekCloser,
retries ...RetryStrategy,
) (UploadImageProgress, error)
// UploadImage is identical to StartImageUpload, but waits until the upload is complete. It returns the disk ID
// as a result, or the error if one happened.
//
// Deprecated: Use UploadToNewDisk instead.
UploadImage(
alias string,
storageDomainID StorageDomainID,
sparse bool,
size uint64,
reader io.ReadSeekCloser,
retries ...RetryStrategy,
) (UploadImageResult, error)
// UploadToNewDisk is identical to StartUploadToNewDisk, but waits until the upload is complete. It
// returns the disk ID as a result, or the error if one happened.
UploadToNewDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
reader io.ReadSeekCloser,
retries ...RetryStrategy,
) (UploadImageResult, error)
// StartUploadToDisk uploads a disk image to an existing disk. The actual upload takes place in the background
// and can be tracked using the returned UploadImageProgress object. Parameters are as follows:
//
// - diskID: ID of the disk to upload to.
// - reader This is the source of the image data.
// - retries: A set of optional retry options.
//
// You can wait for the upload to complete using the Done() method:
//
// progress, err := cli.StartUploadToDisk(...)
// if err != nil {
// //...
// }
// <-progress.Done()
//
// After the upload is complete you can check the Err() method if it completed successfully:
//
// if err := progress.Err(); err != nil {
// //...
// }
StartUploadToDisk(
diskID DiskID,
size uint64,
reader io.ReadSeekCloser,
retries ...RetryStrategy,
) (UploadImageProgress, error)
// UploadToDisk runs StartUploadDisk and then waits for the upload to complete. It returns an error if the upload
// failed despite retries.
//
// Parameters are as follows:
//
// - diskID: ID of the disk to upload to.
// - size: size of the file on disk.
// - reader: this is the source of the image data. The format is automatically determined from the file being
// uploaded. The reader must support seeking and close.
// - retries: a set of optional retry options.
UploadToDisk(
diskID DiskID,
size uint64,
reader io.ReadSeekCloser,
retries ...RetryStrategy,
) error
// StartImageDownload starts the download of the image file of a specific disk.
// The caller can then wait for the initialization using the Initialized() call:
//
// <-download.Initialized()
//
// Alternatively, the downloader can use the Read() function to wait for the download to become available
// and then read immediately.
//
// The caller MUST close the returned reader, otherwise the disk will remain locked in the oVirt engine.
//
// Deprecated: please use StartDownloadDisk instead.
StartImageDownload(
diskID DiskID,
format ImageFormat,
retries ...RetryStrategy,
) (ImageDownload, error)
// StartDownloadDisk starts the download of the image file of a specific disk.
// The caller can then wait for the initialization using the Initialized() call:
//
// <-download.Initialized()
//
// Alternatively, the downloader can use the Read() function to wait for the download to become available
// and then read immediately.
//
// The caller MUST close the returned reader, otherwise the disk will remain locked in the oVirt engine.
StartDownloadDisk(
diskID DiskID,
format ImageFormat,
retries ...RetryStrategy,
) (ImageDownload, error)
// DownloadImage runs StartDownloadDisk, then waits for the download to be ready before returning the reader.
// The caller MUST close the ImageDownloadReader in order to properly unlock the disk in the oVirt engine.
//
// Deprecated: please use DownloadDisk instead.
DownloadImage(
diskID DiskID,
format ImageFormat,
retries ...RetryStrategy,
) (ImageDownloadReader, error)
// DownloadDisk runs StartDownloadDisk, then waits for the download to be ready before returning the reader.
// The caller MUST close the ImageDownloadReader in order to properly unlock the disk in the oVirt engine.
DownloadDisk(
diskID DiskID,
format ImageFormat,
retries ...RetryStrategy,
) (ImageDownloadReader, error)
// StartCreateDisk starts creating an empty disk with the specified parameters and returns a DiskCreation object,
// which can be queried for completion. Optional parameters can be created using CreateDiskParams().
StartCreateDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
retries ...RetryStrategy,
) (DiskCreation, error)
// CreateDisk is a shorthand for calling StartCreateDisk, and then waiting for the disk creation to complete.
// Optional parameters can be created using CreateDiskParams().
//
// Caution! The CreateDisk method may return both an error and a disk that has been created, but has not reached
// the ready state. Since the disk is almost certainly in a locked state, this may mean that there is a disk left
// behind.
CreateDisk(
storageDomainID StorageDomainID,
format ImageFormat,
size uint64,
params CreateDiskOptionalParameters,
retries ...RetryStrategy,
) (Disk, error)
// StartUpdateDisk sends the disk update request to the oVirt API and returns a DiskUpdate
// object, which can be used to wait for the update to complete. Use UpdateDiskParams to
// obtain a builder for the parameters structure.
StartUpdateDisk(
id DiskID,
params UpdateDiskParameters,
retries ...RetryStrategy,
) (DiskUpdate, error)
// UpdateDisk updates the specified disk with the specified parameters. Use UpdateDiskParams to
// obtain a builder for the parameters structure.
UpdateDisk(
id DiskID,
params UpdateDiskParameters,
retries ...RetryStrategy,
) (Disk, error)
// ListDisks lists all disks.
ListDisks(retries ...RetryStrategy) ([]Disk, error)
// GetDisk fetches a disk with a specific ID from the oVirt Engine.
GetDisk(diskID DiskID, retries ...RetryStrategy) (Disk, error)
// ListDisksByAlias fetches a disks with a specific name from the oVirt Engine.
ListDisksByAlias(alias string, retries ...RetryStrategy) ([]Disk, error)
// RemoveDisk removes a disk with a specific ID.
RemoveDisk(diskID DiskID, retries ...RetryStrategy) error
// WaitForDiskOK waits for a disk to be in OK status
WaitForDiskOK(diskID DiskID, retries ...RetryStrategy) (Disk, error)
}
// UpdateDiskParams creates a builder for the params for updating a disk.
func UpdateDiskParams() BuildableUpdateDiskParameters {
return &updateDiskParams{}
}
// UpdateDiskParameters describes the possible parameters for updating a disk.
type UpdateDiskParameters interface {
// Alias returns the disk alias to set. It can return nil to leave the alias unchanged.
Alias() *string
// ProvisionedSize returns the disk provisioned size to set.
// It can return nil to leave the provisioned size unchanged.
ProvisionedSize() *uint64
}
// BuildableUpdateDiskParameters is a buildable version of UpdateDiskParameters.
type BuildableUpdateDiskParameters interface {
UpdateDiskParameters
// WithAlias changes the params structure to set the alias to the specified value. It returns an error
// if the alias is invalid.
WithAlias(alias string) (BuildableUpdateDiskParameters, error)
// MustWithAlias is identical to WithAlias, but panics instead of returning an error.
MustWithAlias(alias string) BuildableUpdateDiskParameters
// WithProvisionedSize changes the params structure to set the provisioned size to the specified value.
// It returns an error if the provisioned size is invalid.
WithProvisionedSize(size uint64) (BuildableUpdateDiskParameters, error)
// MustWithProvisionedSize is identical to WithProvisionedSize, but panics instead of returning an error.
MustWithProvisionedSize(size uint64) BuildableUpdateDiskParameters
}
type updateDiskParams struct {
alias *string
provisionedSize *uint64
}
func (u *updateDiskParams) Alias() *string {
return u.alias
}
func (u *updateDiskParams) WithAlias(alias string) (BuildableUpdateDiskParameters, error) {
u.alias = &alias
return u, nil
}
func (u *updateDiskParams) MustWithAlias(alias string) BuildableUpdateDiskParameters {
builder, err := u.WithAlias(alias)
if err != nil {
panic(err)
}
return builder
}
func (u *updateDiskParams) ProvisionedSize() *uint64 {
return u.provisionedSize
}
func (u *updateDiskParams) WithProvisionedSize(size uint64) (BuildableUpdateDiskParameters, error) {
err := validateDiskSize(size)
if err != nil {
return u, err
}
u.provisionedSize = &size
return u, nil
}
func (u *updateDiskParams) MustWithProvisionedSize(size uint64) BuildableUpdateDiskParameters {
builder, err := u.WithProvisionedSize(size)
if err != nil {
panic(err)
}
return builder
}
// CreateDiskOptionalParameters is a structure that serves to hold the optional parameters for DiskClient.CreateDisk.
type CreateDiskOptionalParameters interface {
// Alias is a secondary name for the disk.
Alias() string
// Sparse indicates that the disk should be sparse-provisioned.If it returns nil, the default will be used.
Sparse() *bool
// InitialSize is the initially reserved disk space when creating the disk.
InitialSize() *uint64
}
// BuildableCreateDiskParameters is a buildable version of CreateDiskOptionalParameters.
type BuildableCreateDiskParameters interface {
CreateDiskOptionalParameters
// WithAlias sets the alias of the disk.
WithAlias(alias string) (BuildableCreateDiskParameters, error)
// MustWithAlias is the same as WithAlias, but panics instead of returning an error.
MustWithAlias(alias string) BuildableCreateDiskParameters
// WithSparse sets sparse provisioning for the disk.
WithSparse(sparse bool) (BuildableCreateDiskParameters, error)
// MustWithSparse is the same as WithSparse, but panics instead of returning an error.
MustWithSparse(sparse bool) BuildableCreateDiskParameters
// WithInitialSize sets initial size for the disk.
WithInitialSize(size uint64) (BuildableCreateDiskParameters, error)
// MustWithInitialSize is the same as WithInitialSize, but panics instead of returning an error.
MustWithInitialSize(size uint64) BuildableCreateDiskParameters
}
// CreateDiskParams creates a buildable set of CreateDiskOptionalParameters for use with
// Client.CreateDisk.
func CreateDiskParams() BuildableCreateDiskParameters {
return &createDiskParams{}
}
type createDiskParams struct {
alias string
sparse *bool
initialSize *uint64
}
func (c *createDiskParams) Alias() string {
return c.alias
}
func (c *createDiskParams) WithAlias(alias string) (BuildableCreateDiskParameters, error) {
c.alias = alias
return c, nil
}
func (c *createDiskParams) MustWithAlias(alias string) BuildableCreateDiskParameters {
builder, err := c.WithAlias(alias)
if err != nil {
panic(err)
}
return builder
}
func (c *createDiskParams) Sparse() *bool {
return c.sparse
}
func (c *createDiskParams) WithSparse(sparse bool) (BuildableCreateDiskParameters, error) {
c.sparse = &sparse
return c, nil
}
func (c *createDiskParams) MustWithSparse(sparse bool) BuildableCreateDiskParameters {
builder, err := c.WithSparse(sparse)
if err != nil {
panic(err)
}
return builder
}
func (c *createDiskParams) InitialSize() *uint64 {
return c.initialSize
}
func (c *createDiskParams) WithInitialSize(size uint64) (BuildableCreateDiskParameters, error) {
c.initialSize = &size
return c, nil
}
func (c *createDiskParams) MustWithInitialSize(size uint64) BuildableCreateDiskParameters {
builder, err := c.WithInitialSize(size)
if err != nil {
panic(err)
}
return builder
}
// DiskCreation is a process object that lets you query the status of the disk creation.
type DiskCreation interface {
// Disk returns the disk that has been created, even if it is not yet ready.
Disk() Disk
// Wait waits until the disk creation is complete and returns when it is done. It returns the created disk and
// an error if one happened.
Wait(retries ...RetryStrategy) (Disk, error)
}
// DiskUpdate is an object to monitor the progress of an update.
type DiskUpdate interface {
// Disk returns the disk as it was during the last update call.
Disk() Disk
// Wait waits until the disk update is complete and returns when it is done. It returns the created disk and
// an error if one happened.
Wait(retries ...RetryStrategy) (Disk, error)
}
// ImageDownloadReader is a special reader for reading image downloads. On the first Read call
// it waits until the image download is ready and then returns the desired bytes. It also
// tracks how many bytes are read for an async display of a progress bar.
type ImageDownloadReader interface {
io.Reader
// Read reads the specified bytes from the disk image. This call will block if
// the image is not yet ready for download.
Read(p []byte) (n int, err error)
// Close closes the image download and unlocks the disk.
Close() error
// BytesRead returns the number of bytes read so far. This can be used to
// provide a progress bar.
BytesRead() uint64
// Size returns the size of the disk image in bytes. This is ONLY available after the initialization is complete and
// MAY return 0 before.
Size() uint64
}
// ImageDownload represents an image download in progress. The caller MUST
// close the image download when it is finished otherwise the disk will not be unlocked.
type ImageDownload interface {
ImageDownloadReader
// Err returns the error that happened during initializing the download, or the last error reading from the
// image server.
Err() error
// Initialized returns a channel that will be closed when the initialization is complete. This can be either
// in an errored state (check Err()) or when the image is ready.
Initialized() <-chan struct{}
}
// UploadImageResult represents the completed image upload.
type UploadImageResult interface {
// Disk returns the disk that has been created as the result of the image upload.
Disk() Disk
}
// DiskData is the core of a Disk, only exposing data functions, but not the client functions.
// This can be used for cases where not a full Disk is required, but only the data functionality.
type DiskData interface {
// ID is the unique ID for this disk.
ID() DiskID
// Alias is the name for this disk set by the user.
Alias() string
// ProvisionedSize is the size visible to the virtual machine.
ProvisionedSize() uint64
// TotalSize is the size of the image file.
// This value can be zero in some cases, for example when the disk upload wasn't properly finalized.
TotalSize() uint64
// Format is the format of the image.
Format() ImageFormat
// StorageDomainIDs returns a list of storage domains this disk is present on. This will typically be a single
// disk, but may have multiple disk when the disk has been copied over to other storage domains. The disk is always
// present on at least one disk, so this list will never be empty.
StorageDomainIDs() []StorageDomainID
// Status returns the status the disk is in.
Status() DiskStatus
// Sparse indicates sparse provisioning on the disk.
Sparse() bool
}
// Disk is a disk in oVirt.
type Disk interface {
DiskData
// StartDownload starts the download of the image file the current disk.
// The caller can then wait for the initialization using the Initialized() call:
//
// <-download.Initialized()
//
// Alternatively, the downloader can use the Read() function to wait for the download to become available
// and then read immediately.
//
// The caller MUST close the returned reader, otherwise the disk will remain locked in the oVirt engine.
// The passed context is observed only for the initialization phase.
StartDownload(
format ImageFormat,
retries ...RetryStrategy,
) (ImageDownload, error)
// Download runs StartDownload, then waits for the download to be ready before returning the reader.
// The caller MUST close the ImageDownloadReader in order to properly unlock the disk in the oVirt engine.
Download(
format ImageFormat,
retries ...RetryStrategy,
) (ImageDownloadReader, error)
// Remove removes the current disk in the oVirt engine.
Remove(retries ...RetryStrategy) error
// AttachToVM attaches a disk to this VM.
AttachToVM(
vmID VMID,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
retries ...RetryStrategy,
) (DiskAttachment, error)
// StartUpdate starts an update to the disk. The returned DiskUpdate can be used to wait
// for the update to complete. Use UpdateDiskParams() to obtain a buildable structure.
StartUpdate(
params UpdateDiskParameters,
retries ...RetryStrategy,
) (DiskUpdate, error)
// Update updates the current disk with the specified parameters.
// Use UpdateDiskParams() to obtain a buildable structure.
Update(
params UpdateDiskParameters,
retries ...RetryStrategy,
) (Disk, error)
// StorageDomains will fetch and return the storage domains associated with this disk.
StorageDomains(retries ...RetryStrategy) ([]StorageDomain, error)
// WaitForOK waits for the disk status to return to OK.
WaitForOK(retries ...RetryStrategy) (Disk, error)
}
// DiskStatus shows the status of a disk. Certain operations lock a disk, which is important because the disk can then
// not be changed.
type DiskStatus string
const (
// DiskStatusOK represents a disk status that operations can be performed on.
DiskStatusOK DiskStatus = "ok"
// DiskStatusLocked represents a disk status where no operations can be performed on the disk.
DiskStatusLocked DiskStatus = "locked"
// DiskStatusIllegal indicates that the disk cannot be accessed by the virtual machine, and the user needs
// to take action to resolve the issue.
DiskStatusIllegal DiskStatus = "illegal"
)
// DiskStatusList is a list of DiskStatus values.
type DiskStatusList []DiskStatus
// DiskStatusValues returns all possible values for DiskStatus.
func DiskStatusValues() DiskStatusList {
return []DiskStatus{
DiskStatusOK,
DiskStatusLocked,
DiskStatusIllegal,
}
}
// Strings returns a list of strings.
func (l DiskStatusList) Strings() []string {
result := make([]string, len(l))
for i, status := range l {
result[i] = string(status)
}
return result
}
// UploadImageProgress is a tracker for the upload progress happening in the background.
type UploadImageProgress interface {
// Disk returns the disk created as part of the upload process once the upload is complete. Before the upload
// is complete it will return nil.
Disk() Disk
// UploadedBytes returns the number of bytes already uploaded.
//
// Caution! This number may decrease or reset to 0 if the upload has to be retried.
UploadedBytes() uint64
// TotalBytes returns the total number of bytes to be uploaded.
TotalBytes() uint64
// Err returns the error of the upload once the upload is complete or errored.
Err() error
// Done returns a channel that will be closed when the upload is complete.
Done() <-chan struct{}
}
// ImageFormat is a constant for representing the format that images can be in. This is relevant
// for both image uploads and image downloads, as the oVirt engine has the capability of converting
// between these formats.
//
// Note: the mocking facility cannot convert between the formats due to the complexity of the QCOW2
// format. It is recommended to write tests only using the raw format as comparing QCOW2 files
// is complex.
type ImageFormat string
// Validate returns an error if the image format doesn't have a valid value.
func (f ImageFormat) Validate() error {
for _, format := range ImageFormatValues() {
if format == f {
return nil
}
}
return newError(
EBadArgument,
"invalid image format: %s must be one of: %s",
f,
strings.Join(ImageFormatValues().Strings(), ", "),
)
}
const (
// ImageFormatCow is an image conforming to the QCOW2 image format. This image format can use
// compression, supports snapshots, and other features.
// See https://github.com/qemu/qemu/blob/master/docs/interop/qcow2.txt for details.
ImageFormatCow ImageFormat = "cow"
// ImageFormatRaw is not actually a format, it only contains the raw bytes on the block device.
ImageFormatRaw ImageFormat = "raw"
)
// ImageFormatList is a list of ImageFormat values.
type ImageFormatList []ImageFormat
// ImageFormatValues returns all possible ImageFormat values.
func ImageFormatValues() ImageFormatList {
return []ImageFormat{
ImageFormatCow,
ImageFormatRaw,
}
}
// Strings creates a string list of the values.
func (l ImageFormatList) Strings() []string {
result := make([]string, len(l))
for i, status := range l {
result[i] = string(status)
}
return result
}
func convertSDKDisk(sdkDisk *ovirtsdk4.Disk, client Client) (Disk, error) {
id, ok := sdkDisk.Id()
if !ok {
return nil, newError(EFieldMissing, "disk does not contain an ID")
}
var storageDomainIDs []StorageDomainID
if sdkStorageDomain, ok := sdkDisk.StorageDomain(); ok {
storageDomainID, _ := sdkStorageDomain.Id()
storageDomainIDs = append(storageDomainIDs, StorageDomainID(storageDomainID))
}
if sdkStorageDomains, ok := sdkDisk.StorageDomains(); ok {
for _, sd := range sdkStorageDomains.Slice() {
storageDomainID, _ := sd.Id()
storageDomainIDs = append(storageDomainIDs, StorageDomainID(storageDomainID))
}
}
if len(storageDomainIDs) == 0 {
return nil, newError(EFieldMissing, "failed to find a valid storage domain for disk %s", id)
}
alias, ok := sdkDisk.Alias()
if !ok {
return nil, newError(EFieldMissing, "disk %s does not contain an alias", id)
}
provisionedSize, ok := sdkDisk.ProvisionedSize()
if !ok {
return nil, newError(EFieldMissing, "disk %s does not contain a provisioned size", id)
}
totalSize, ok := sdkDisk.TotalSize()
if !ok {
return nil, newError(EFieldMissing, "disk %s does not contain a total size", id)
}
format, ok := sdkDisk.Format()
if !ok {
return nil, newError(EFieldMissing, "disk %s has no format field", id)
}
status, ok := sdkDisk.Status()
if !ok {
return nil, newError(EFieldMissing, "disk %s has no status field", id)
}
sparse, ok := sdkDisk.Sparse()
if !ok {
return nil, newError(EFieldMissing, "disk %s has no sparse field", id)
}
return &disk{
client: client,
id: DiskID(id),
alias: alias,
provisionedSize: uint64(provisionedSize),
totalSize: uint64(totalSize),
format: ImageFormat(format),
storageDomainIDs: storageDomainIDs,
status: DiskStatus(status),
sparse: sparse,
}, nil
}
type disk struct {
client Client
id DiskID
alias string
provisionedSize uint64
format ImageFormat
storageDomainIDs []StorageDomainID
status DiskStatus
totalSize uint64
sparse bool
}
func (d *disk) WaitForOK(retries ...RetryStrategy) (Disk, error) {
return d.client.WaitForDiskOK(d.id, retries...)
}
func (d *disk) StorageDomainIDs() []StorageDomainID {
return d.storageDomainIDs
}
func (d *disk) StorageDomains(retries ...RetryStrategy) ([]StorageDomain, error) {
storageDomains := make([]StorageDomain, len(d.storageDomainIDs))
for i, id := range d.storageDomainIDs {
storageDomain, err := d.client.GetStorageDomain(id, retries...)
if err != nil {
return nil, err
}
storageDomains[i] = storageDomain
}
return storageDomains, nil
}
func (d *disk) Update(params UpdateDiskParameters, retries ...RetryStrategy) (Disk, error) {
return d.client.UpdateDisk(d.id, params, retries...)
}
func (d *disk) StartUpdate(params UpdateDiskParameters, retries ...RetryStrategy) (DiskUpdate, error) {
return d.client.StartUpdateDisk(d.id, params, retries...)
}
func (d *disk) Sparse() bool {
return d.sparse
}
func (d *disk) AttachToVM(
vmID VMID,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
retries ...RetryStrategy,
) (DiskAttachment, error) {
return d.client.CreateDiskAttachment(vmID, d.id, diskInterface, params, retries...)
}
func (d *disk) Remove(retries ...RetryStrategy) error {
return d.client.RemoveDisk(d.id, retries...)
}
func (d *disk) TotalSize() uint64 {
return d.totalSize
}
func (d disk) Status() DiskStatus {
return d.status
}
func (d disk) ID() DiskID {
return d.id
}
func (d disk) Alias() string {
return d.alias
}
func (d disk) ProvisionedSize() uint64 {
return d.provisionedSize
}
func (d disk) Format() ImageFormat {
return d.format
}
func (d disk) StartDownload(format ImageFormat, retries ...RetryStrategy) (ImageDownload, error) {
return d.client.StartDownloadDisk(d.id, format, retries...)
}
func (d disk) Download(format ImageFormat, retries ...RetryStrategy) (ImageDownloadReader, error) {
return d.client.DownloadDisk(d.id, format, retries...)
}
type diskWait struct {
client *oVirtClient
disk Disk
correlationID string
lock *sync.Mutex
}
func (d *diskWait) Disk() Disk {
d.lock.Lock()
defer d.lock.Unlock()
return d.disk
}
func (d *diskWait) Wait(retries ...RetryStrategy) (Disk, error) {
retries = defaultRetries(retries, defaultLongTimeouts(d.client))
if err := d.client.waitForJobFinished(d.correlationID, retries); err != nil {
return d.disk, err
}
d.lock.Lock()
diskID := d.disk.ID()
d.lock.Unlock()
disk, err := d.client.GetDisk(diskID, retries...)
d.lock.Lock()
defer d.lock.Unlock()
if disk != nil {
d.disk = disk
}
return disk, err
}