forked from oVirt/go-ovirt-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_vm.go
1164 lines (1011 loc) · 34.2 KB
/
client_vm.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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ovirtclient
import (
"fmt"
"regexp"
"strconv"
"strings"
"sync"
ovirtsdk "github.com/ovirt/go-ovirt"
)
//go:generate go run scripts/rest.go -i "Vm" -n "vm" -o "VM"
// VMClient includes the methods required to deal with virtual machines.
type VMClient interface {
// CreateVM creates a virtual machine.
CreateVM(
clusterID string,
templateID TemplateID,
name string,
optional OptionalVMParameters,
retries ...RetryStrategy,
) (VM, error)
// GetVM returns a single virtual machine based on an ID.
GetVM(id string, retries ...RetryStrategy) (VM, error)
// UpdateVM updates the virtual machine with the given parameters.
// Use UpdateVMParams to obtain a builder for the params.
UpdateVM(id string, params UpdateVMParameters, retries ...RetryStrategy) (VM, error)
// SetVMOptimizePinningSettings sets the CPU settings to optimized.
AutoOptimizeVMCPUPinningSettings(id string, optimize bool, retries ...RetryStrategy) error
// StartVM triggers a VM start. The actual VM startup will take time and should be waited for via the
// WaitForVMStatus call.
StartVM(id string, retries ...RetryStrategy) error
// StopVM triggers a VM power-off. The actual VM stop will take time and should be waited for via the
// WaitForVMStatus call. The force parameter will cause the shutdown to proceed even if a backup is currently
// running.
StopVM(id string, force bool, retries ...RetryStrategy) error
// ShutdownVM triggers a VM shutdown. The actual VM shutdown will take time and should be waited for via the
// WaitForVMStatus call. The force parameter will cause the shutdown to proceed even if a backup is currently
// running.
ShutdownVM(id string, force bool, retries ...RetryStrategy) error
// WaitForVMStatus waits for the VM to reach the desired status.
WaitForVMStatus(id string, status VMStatus, retries ...RetryStrategy) (VM, error)
// ListVMs returns a list of all virtual machines.
ListVMs(retries ...RetryStrategy) ([]VM, error)
// SearchVMs lists all virtual machines matching a certain criteria specified in params.
SearchVMs(params VMSearchParameters, retries ...RetryStrategy) ([]VM, error)
// RemoveVM removes a virtual machine specified by id.
RemoveVM(id string, retries ...RetryStrategy) error
// AddTagToVM Add tag specified by id to a VM.
AddTagToVM(id string, tagID string, retries ...RetryStrategy) error
}
// VMData is the core of VM providing only data access functions.
type VMData interface {
// ID returns the unique identifier (UUID) of the current virtual machine.
ID() string
// Name is the user-defined name of the virtual machine.
Name() string
// Comment is the comment added to the VM.
Comment() string
// ClusterID returns the cluster this machine belongs to.
ClusterID() string
// TemplateID returns the ID of the base template for this machine.
TemplateID() TemplateID
// Status returns the current status of the VM.
Status() VMStatus
// CPU returns the CPU structure of a VM.
CPU() VMCPU
// TagIDS returns a list of tags for this VM.
TagIDs() []string
// HugePages returns the hugepage settings for the VM, if any.
HugePages() *VMHugePages
// Initialization returns the virtual machine’s initialization configuration.
Initialization() Initialization
}
// VMCPU is the CPU configuration of a VM.
type VMCPU interface {
// Topo is the desired CPU topology for this VM.
Topo() VMCPUTopo
}
type vmCPU struct {
topo *vmCPUTopo
}
func (v vmCPU) Topo() VMCPUTopo {
return v.topo
}
func (v *vmCPU) clone() *vmCPU {
if v == nil {
return nil
}
return &vmCPU{
topo: v.topo.clone(),
}
}
// VMHugePages is the hugepages setting of the VM in bytes.
type VMHugePages uint64
// Validate returns an error if the VM hugepages doesn't have a valid value.
func (h VMHugePages) Validate() error {
for _, hugePages := range VMHugePagesValues() {
if hugePages == h {
return nil
}
}
return newError(
EBadArgument,
"Invalid value for VM huge pages: %d must be one of: %s",
h,
strings.Join(VMHugePagesValues().Strings(), ", "),
)
}
const (
// VMHugePages2M represents the small value of supported huge pages setting which is 2048 Kib.
VMHugePages2M VMHugePages = 2048
// VMHugePages1G represents the large value of supported huge pages setting which is 1048576 Kib.
VMHugePages1G VMHugePages = 1048576
)
// VMHugePagesList is a list of VMHugePages.
type VMHugePagesList []VMHugePages
// Strings creates a string list of the values.
func (l VMHugePagesList) Strings() []string {
result := make([]string, len(l))
for i, hugepage := range l {
result[i] = fmt.Sprint(hugepage)
}
return result
}
// VMHugePagesValues returns all possible VMHugepages values.
func VMHugePagesValues() VMHugePagesList {
return []VMHugePages{
VMHugePages2M,
VMHugePages1G,
}
}
// Initialization defines to the virtual machine’s initialization configuration.
type Initialization interface {
CustomScript() string
HostName() string
}
// BuildableInitialization is a buildable version of Initialization.
type BuildableInitialization interface {
Initialization
WithCustomScript(customScript string) BuildableInitialization
WithHostname(hostname string) BuildableInitialization
}
// initialization defines to the virtual machine’s initialization configuration.
// customScript - Cloud-init script which will be executed on Virtual Machine when deployed.
// hostname - Hostname to be set to Virtual Machine when deployed.
type initialization struct {
customScript string
hostname string
}
// NewInitialization creates a new Initialization from the specified parameters.
func NewInitialization(customScript, hostname string) Initialization {
return &initialization{
customScript: customScript,
hostname: hostname,
}
}
func (i *initialization) CustomScript() string {
return i.customScript
}
func (i *initialization) HostName() string {
return i.hostname
}
func (i *initialization) WithCustomScript(customScript string) BuildableInitialization {
i.customScript = customScript
return i
}
func (i *initialization) WithHostname(hostname string) BuildableInitialization {
i.hostname = hostname
return i
}
// convertSDKInitialization converts the initialization of a VM. We keep the error return in case we need it later
// as errors may happen as we extend this function and we don't want to touch other functions.
func convertSDKInitialization(sdkObject *ovirtsdk.Vm) (*initialization, error) { //nolint:unparam
initializationSDK, ok := sdkObject.Initialization()
if !ok {
// This happens for some, but not all API calls if the initialization is not set.
return &initialization{}, nil
}
init := initialization{}
customScript, ok := initializationSDK.CustomScript()
if ok {
init.customScript = customScript
}
hostname, ok := initializationSDK.HostName()
if ok {
init.hostname = hostname
}
return &init, nil
}
// VM is the implementation of the virtual machine in oVirt.
type VM interface {
VMData
// Update updates the virtual machine with the given parameters. Use UpdateVMParams to
// get a builder for the parameters.
Update(params UpdateVMParameters, retries ...RetryStrategy) (VM, error)
// Remove removes the current VM. This involves an API call and may be slow.
Remove(retries ...RetryStrategy) error
// Start will cause a VM to start. The actual start process takes some time and should be checked via WaitForStatus.
Start(retries ...RetryStrategy) error
// Stop will cause the VM to power-off. The force parameter will cause the VM to stop even if a backup is currently
// running.
Stop(force bool, retries ...RetryStrategy) error
// Shutdown will cause the VM to shut down. The force parameter will cause the VM to shut down even if a backup
// is currently running.
Shutdown(force bool, retries ...RetryStrategy) error
// WaitForStatus will wait until the VM reaches the desired status. If the status is not reached within the
// specified amount of retries, an error will be returned. If the VM enters the desired state, an updated VM
// object will be returned.
WaitForStatus(status VMStatus, retries ...RetryStrategy) (VM, error)
// CreateNIC creates a network interface on the current VM. This involves an API call and may be slow.
CreateNIC(name string, vnicProfileID string, params OptionalNICParameters, retries ...RetryStrategy) (NIC, error)
// GetNIC fetches a NIC with a specific ID on the current VM. This involves an API call and may be slow.
GetNIC(id string, retries ...RetryStrategy) (NIC, error)
// ListNICs fetches a list of network interfaces attached to this VM. This involves an API call and may be slow.
ListNICs(retries ...RetryStrategy) ([]NIC, error)
// AttachDisk attaches a disk to this VM.
AttachDisk(
diskID string,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
retries ...RetryStrategy,
) (DiskAttachment, error)
// GetDiskAttachment returns a specific disk attachment for the current VM by ID.
GetDiskAttachment(diskAttachmentID string, retries ...RetryStrategy) (DiskAttachment, error)
// ListDiskAttachments lists all disk attachments for the current VM.
ListDiskAttachments(retries ...RetryStrategy) ([]DiskAttachment, error)
// DetachDisk removes a specific disk attachment by the disk attachment ID.
DetachDisk(
diskAttachmentID string,
retries ...RetryStrategy,
) error
// Tags list all tags for the current VM
Tags(retries ...RetryStrategy) ([]Tag, error)
}
// VMSearchParameters declares the parameters that can be passed to a VM search. Each parameter
// is declared as a pointer, where a nil value will mean that parameter will not be searched for.
// All parameters are used together as an AND filter.
type VMSearchParameters interface {
// Name will match the name of the virtual machine exactly.
Name() *string
// Tag will match the tag of the virtual machine.
Tag() *string
// Statuses will return a list of acceptable statuses for this VM search.
Statuses() *VMStatusList
// NotStatuses will return a list of not acceptable statuses for this VM search.
NotStatuses() *VMStatusList
}
// BuildableVMSearchParameters is a buildable version of VMSearchParameters.
type BuildableVMSearchParameters interface {
VMSearchParameters
// WithName sets the name to search for.
WithName(name string) BuildableVMSearchParameters
// WithTag sets the tag to search for.
WithTag(name string) BuildableVMSearchParameters
// WithStatus adds a single status to the filter.
WithStatus(status VMStatus) BuildableVMSearchParameters
// WithNotStatus excludes a VM status from the search.
WithNotStatus(status VMStatus) BuildableVMSearchParameters
// WithStatuses will return the statuses the returned VMs should be in.
WithStatuses(list VMStatusList) BuildableVMSearchParameters
// WithNotStatuses will return the statuses the returned VMs should not be in.
WithNotStatuses(list VMStatusList) BuildableVMSearchParameters
}
// VMSearchParams creates a buildable set of search parameters for easier use.
func VMSearchParams() BuildableVMSearchParameters {
return &vmSearchParams{
lock: &sync.Mutex{},
}
}
type vmSearchParams struct {
lock *sync.Mutex
name *string
tag *string
statuses *VMStatusList
notStatuses *VMStatusList
}
func (v *vmSearchParams) WithStatus(status VMStatus) BuildableVMSearchParameters {
v.lock.Lock()
defer v.lock.Unlock()
newStatuses := append(*v.statuses, status)
v.statuses = &newStatuses
return v
}
func (v *vmSearchParams) WithNotStatus(status VMStatus) BuildableVMSearchParameters {
v.lock.Lock()
defer v.lock.Unlock()
newNotStatuses := append(*v.notStatuses, status)
v.statuses = &newNotStatuses
return v
}
func (v *vmSearchParams) Tag() *string {
v.lock.Lock()
defer v.lock.Unlock()
return v.tag
}
func (v *vmSearchParams) Name() *string {
v.lock.Lock()
defer v.lock.Unlock()
return v.name
}
func (v *vmSearchParams) Statuses() *VMStatusList {
v.lock.Lock()
defer v.lock.Unlock()
return v.statuses
}
func (v *vmSearchParams) NotStatuses() *VMStatusList {
v.lock.Lock()
defer v.lock.Unlock()
return v.notStatuses
}
func (v *vmSearchParams) WithName(name string) BuildableVMSearchParameters {
v.lock.Lock()
defer v.lock.Unlock()
v.name = &name
return v
}
func (v *vmSearchParams) WithTag(tag string) BuildableVMSearchParameters {
v.lock.Lock()
defer v.lock.Unlock()
v.tag = &tag
return v
}
func (v *vmSearchParams) WithStatuses(list VMStatusList) BuildableVMSearchParameters {
v.lock.Lock()
defer v.lock.Unlock()
newStatuses := list.Copy()
v.statuses = &newStatuses
return v
}
func (v *vmSearchParams) WithNotStatuses(list VMStatusList) BuildableVMSearchParameters {
v.lock.Lock()
defer v.lock.Unlock()
newNotStatuses := list.Copy()
v.notStatuses = &newNotStatuses
return v
}
// OptionalVMParameters are a list of parameters that can be, but must not necessarily be added on VM creation. This
// interface is expected to be extended in the future.
type OptionalVMParameters interface {
// Comment returns the comment for the VM.
Comment() string
// CPU contains the CPU topology, if any.
CPU() VMCPUTopo
// HugePages returns the optional value for the HugePages setting for VMs.
HugePages() *VMHugePages
// Initialization defines the virtual machine’s initialization configuration.
Initialization() Initialization
}
// BuildableVMParameters is a variant of OptionalVMParameters that can be changed using the supplied
// builder functions. This is placed here for future use.
type BuildableVMParameters interface {
OptionalVMParameters
// WithComment adds a common to the VM.
WithComment(comment string) (BuildableVMParameters, error)
// MustWithComment is identical to WithComment, but panics instead of returning an error.
MustWithComment(comment string) BuildableVMParameters
// WithCPU adds a VMCPUTopo to the VM.
WithCPU(cpu VMCPUTopo) (BuildableVMParameters, error)
// MustWithCPU adds a VMCPUTopo and panics if an error happens.
MustWithCPU(cpu VMCPUTopo) BuildableVMParameters
// WithCPUParameters is a simplified function that calls NewVMCPUTopo and adds the CPU topology to
// the VM.
WithCPUParameters(cores, threads, sockets uint) (BuildableVMParameters, error)
// MustWithCPUParameters is a simplified function that calls MustNewVMCPUTopo and adds the CPU topology to
// the VM.
MustWithCPUParameters(cores, threads, sockets uint) BuildableVMParameters
// WithHugePages sets the HugePages setting for the VM.
WithHugePages(hugePages VMHugePages) (BuildableVMParameters, error)
// MustWithHugePages is identical to WithHugePages, but panics instead of returning an error.
MustWithHugePages(hugePages VMHugePages) BuildableVMParameters
// WithInitialization sets the virtual machine’s initialization configuration.
WithInitialization(initialization Initialization) (BuildableVMParameters, error)
// MustWithInitialization is identical to WithInitialization, but panics instead of returning an error.
MustWithInitialization(initialization Initialization) BuildableVMParameters
// MustWithInitializationParameters is a simplified function that calls MustNewInitialization and adds customScript
MustWithInitializationParameters(customScript, hostname string) BuildableVMParameters
}
// UpdateVMParameters returns a set of parameters to change on a VM.
type UpdateVMParameters interface {
// Name returns the name for the VM. Return nil if the name should not be changed.
Name() *string
// Comment returns the comment for the VM. Return nil if the name should not be changed.
Comment() *string
}
// VMCPUTopo contains the CPU topology information about a VM.
type VMCPUTopo interface {
// Cores is the number of CPU cores.
Cores() uint
// Threads is the number of CPU threads in a core.
Threads() uint
// Sockets is the number of sockets.
Sockets() uint
}
// NewVMCPUTopo creates a new VMCPUTopo from the specified parameters.
func NewVMCPUTopo(cores uint, threads uint, sockets uint) (VMCPUTopo, error) {
if cores == 0 {
return nil, newError(EBadArgument, "number of cores must be positive")
}
if threads == 0 {
return nil, newError(EBadArgument, "number of threads must be positive")
}
if sockets == 0 {
return nil, newError(EBadArgument, "number of sockets must be positive")
}
return &vmCPUTopo{
cores: cores,
threads: threads,
sockets: sockets,
}, nil
}
// MustNewVMCPUTopo is equivalent to NewVMCPUTopo, but panics instead of returning an error.
func MustNewVMCPUTopo(cores uint, threads uint, sockets uint) VMCPUTopo {
topo, err := NewVMCPUTopo(cores, threads, sockets)
if err != nil {
panic(err)
}
return topo
}
type vmCPUTopo struct {
cores uint
threads uint
sockets uint
}
func (v *vmCPUTopo) Cores() uint {
return v.cores
}
func (v *vmCPUTopo) Threads() uint {
return v.threads
}
func (v *vmCPUTopo) Sockets() uint {
return v.sockets
}
func (v *vmCPUTopo) clone() *vmCPUTopo {
if v == nil {
return nil
}
return &vmCPUTopo{
cores: v.cores,
threads: v.threads,
sockets: v.sockets,
}
}
// BuildableUpdateVMParameters is a buildable version of UpdateVMParameters.
type BuildableUpdateVMParameters interface {
UpdateVMParameters
// WithName adds an updated name to the request.
WithName(name string) (BuildableUpdateVMParameters, error)
// MustWithName is identical to WithName, but panics instead of returning an error
MustWithName(name string) BuildableUpdateVMParameters
// WithComment adds a comment to the request
WithComment(comment string) (BuildableUpdateVMParameters, error)
// MustWithComment is identical to WithComment, but panics instead of returning an error.
MustWithComment(comment string) BuildableUpdateVMParameters
}
// UpdateVMParams returns a buildable set of update parameters.
func UpdateVMParams() BuildableUpdateVMParameters {
return &updateVMParams{}
}
type updateVMParams struct {
name *string
comment *string
}
func (u *updateVMParams) MustWithName(name string) BuildableUpdateVMParameters {
builder, err := u.WithName(name)
if err != nil {
panic(err)
}
return builder
}
func (u *updateVMParams) MustWithComment(comment string) BuildableUpdateVMParameters {
builder, err := u.WithComment(comment)
if err != nil {
panic(err)
}
return builder
}
func (u *updateVMParams) Name() *string {
return u.name
}
func (u *updateVMParams) Comment() *string {
return u.comment
}
func (u *updateVMParams) WithName(name string) (BuildableUpdateVMParameters, error) {
if err := validateVMName(name); err != nil {
return nil, err
}
u.name = &name
return u, nil
}
func (u *updateVMParams) WithComment(comment string) (BuildableUpdateVMParameters, error) {
u.comment = &comment
return u, nil
}
// CreateVMParams creates a set of BuildableVMParameters that can be used to construct the optional VM parameters.
func CreateVMParams() BuildableVMParameters {
return &vmParams{
lock: &sync.Mutex{},
}
}
type vmParams struct {
lock *sync.Mutex
name string
comment string
cpu VMCPUTopo
hugePages *VMHugePages
initialization Initialization
}
func (v *vmParams) HugePages() *VMHugePages {
return v.hugePages
}
func (v *vmParams) WithHugePages(hugePages VMHugePages) (BuildableVMParameters, error) {
if err := hugePages.Validate(); err != nil {
return v, err
}
v.hugePages = &hugePages
return v, nil
}
func (v *vmParams) MustWithHugePages(hugePages VMHugePages) BuildableVMParameters {
builder, err := v.WithHugePages(hugePages)
if err != nil {
panic(err)
}
return builder
}
func (v *vmParams) Initialization() Initialization {
return v.initialization
}
func (v *vmParams) WithInitialization(initialization Initialization) (BuildableVMParameters, error) {
v.initialization = initialization
return v, nil
}
func (v *vmParams) MustWithInitialization(initialization Initialization) BuildableVMParameters {
builder, err := v.WithInitialization(initialization)
if err != nil {
panic(err)
}
return builder
}
func (v *vmParams) MustWithInitializationParameters(customScript, hostname string) BuildableVMParameters {
init := NewInitialization(customScript, hostname)
return v.MustWithInitialization(init)
}
func (v *vmParams) CPU() VMCPUTopo {
return v.cpu
}
func (v *vmParams) WithCPU(cpu VMCPUTopo) (BuildableVMParameters, error) {
v.cpu = cpu
return v, nil
}
func (v *vmParams) MustWithCPU(cpu VMCPUTopo) BuildableVMParameters {
builder, err := v.WithCPU(cpu)
if err != nil {
panic(err)
}
return builder
}
func (v *vmParams) WithCPUParameters(cores, threads, sockets uint) (BuildableVMParameters, error) {
cpu, err := NewVMCPUTopo(cores, threads, sockets)
if err != nil {
return nil, err
}
return v.WithCPU(cpu)
}
func (v *vmParams) MustWithCPUParameters(cores, threads, sockets uint) BuildableVMParameters {
return v.MustWithCPU(MustNewVMCPUTopo(cores, threads, sockets))
}
func (v *vmParams) MustWithName(name string) BuildableVMParameters {
builder, err := v.WithName(name)
if err != nil {
panic(err)
}
return builder
}
func (v *vmParams) MustWithComment(comment string) BuildableVMParameters {
builder, err := v.WithComment(comment)
if err != nil {
panic(err)
}
return builder
}
func (v *vmParams) WithName(name string) (BuildableVMParameters, error) {
if err := validateVMName(name); err != nil {
return nil, err
}
v.name = name
return v, nil
}
func (v *vmParams) WithComment(comment string) (BuildableVMParameters, error) {
v.comment = comment
return v, nil
}
func (v vmParams) Name() string {
return v.name
}
func (v vmParams) Comment() string {
return v.comment
}
type vm struct {
client Client
id string
name string
comment string
clusterID string
templateID TemplateID
status VMStatus
cpu *vmCPU
tagIDs []string
hugePages *VMHugePages
initialization Initialization
}
func (v *vm) HugePages() *VMHugePages {
return v.hugePages
}
func (v *vm) Start(retries ...RetryStrategy) error {
return v.client.StartVM(v.id, retries...)
}
func (v *vm) Stop(force bool, retries ...RetryStrategy) error {
return v.client.StopVM(v.id, force, retries...)
}
func (v *vm) Shutdown(force bool, retries ...RetryStrategy) error {
return v.client.ShutdownVM(v.id, force, retries...)
}
func (v *vm) WaitForStatus(status VMStatus, retries ...RetryStrategy) (VM, error) {
return v.client.WaitForVMStatus(v.id, status, retries...)
}
func (v *vm) CPU() VMCPU {
return v.cpu
}
func (v *vm) Initialization() Initialization {
return v.initialization
}
// withName returns a copy of the VM with the new name. It does not change the original copy to avoid
// shared state issues.
func (v *vm) withName(name string) *vm {
return &vm{
client: v.client,
id: v.id,
name: name,
comment: v.comment,
clusterID: v.clusterID,
templateID: v.templateID,
status: v.status,
cpu: v.cpu,
}
}
// withComment returns a copy of the VM with the new comment. It does not change the original copy to avoid
// shared state issues.
func (v *vm) withComment(comment string) *vm {
return &vm{
client: v.client,
id: v.id,
name: v.name,
comment: comment,
clusterID: v.clusterID,
templateID: v.templateID,
status: v.status,
cpu: v.cpu,
}
}
func (v *vm) Update(params UpdateVMParameters, retries ...RetryStrategy) (VM, error) {
return v.client.UpdateVM(v.id, params, retries...)
}
func (v *vm) Status() VMStatus {
return v.status
}
func (v *vm) AttachDisk(
diskID string,
diskInterface DiskInterface,
params CreateDiskAttachmentOptionalParams,
retries ...RetryStrategy,
) (DiskAttachment, error) {
return v.client.CreateDiskAttachment(v.id, diskID, diskInterface, params, retries...)
}
func (v *vm) GetDiskAttachment(diskAttachmentID string, retries ...RetryStrategy) (DiskAttachment, error) {
return v.client.GetDiskAttachment(v.id, diskAttachmentID, retries...)
}
func (v *vm) ListDiskAttachments(retries ...RetryStrategy) ([]DiskAttachment, error) {
return v.client.ListDiskAttachments(v.id, retries...)
}
func (v *vm) DetachDisk(diskAttachmentID string, retries ...RetryStrategy) error {
return v.client.RemoveDiskAttachment(v.id, diskAttachmentID, retries...)
}
func (v *vm) Remove(retries ...RetryStrategy) error {
return v.client.RemoveVM(v.id, retries...)
}
func (v *vm) CreateNIC(name string, vnicProfileID string, params OptionalNICParameters, retries ...RetryStrategy) (NIC, error) {
return v.client.CreateNIC(v.id, vnicProfileID, name, params, retries...)
}
func (v *vm) GetNIC(id string, retries ...RetryStrategy) (NIC, error) {
return v.client.GetNIC(v.id, id, retries...)
}
func (v *vm) ListNICs(retries ...RetryStrategy) ([]NIC, error) {
return v.client.ListNICs(v.id, retries...)
}
func (v *vm) Comment() string {
return v.comment
}
func (v *vm) ClusterID() string {
return v.clusterID
}
func (v *vm) TemplateID() TemplateID {
return v.templateID
}
func (v *vm) ID() string {
return v.id
}
func (v *vm) Name() string {
return v.name
}
func (v *vm) TagIDs() []string {
return v.tagIDs
}
func (v *vm) Tags(retries ...RetryStrategy) ([]Tag, error) {
tags := make([]Tag, len(v.tagIDs))
for i, id := range v.tagIDs {
tag, err := v.client.GetTag(id, retries...)
if err != nil {
return nil, err
}
tags[i] = tag
}
return tags, nil
}
func (v *vm) AddTagToVM(tagID string, retries ...RetryStrategy) error {
return v.client.AddTagToVM(v.id, tagID, retries...)
}
var vmNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_\-.]*$`)
func validateVMName(name string) error {
if !vmNameRegexp.MatchString(name) {
return newError(EBadArgument, "invalid VM name: %s", name)
}
return nil
}
func convertSDKVM(sdkObject *ovirtsdk.Vm, client Client) (VM, error) {
vmObject := &vm{
client: client,
}
vmConverters := []func(sdkObject *ovirtsdk.Vm, vm *vm) error{
vmIDConverter,
vmNameConverter,
vmCommentConverter,
vmClusterConverter,
vmStatusConverter,
vmTemplateConverter,
vmCPUConverter,
vmHugePagesConverter,
vmTagsConverter,
vmInitializationConverter,
}
for _, converter := range vmConverters {
if err := converter(sdkObject, vmObject); err != nil {
return nil, err
}
}
return vmObject, nil
}
func vmIDConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
id, ok := sdkObject.Id()
if !ok {
return newError(EFieldMissing, "id field missing from VM object")
}
v.id = id
return nil
}
func vmNameConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
name, ok := sdkObject.Name()
if !ok {
return newError(EFieldMissing, "name field missing from VM object")
}
v.name = name
return nil
}
func vmCommentConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
comment, ok := sdkObject.Comment()
if !ok {
return newError(EFieldMissing, "comment field missing from VM object")
}
v.comment = comment
return nil
}
func vmClusterConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
cluster, ok := sdkObject.Cluster()
if !ok {
return newError(EFieldMissing, "cluster field missing from VM object")
}
clusterID, ok := cluster.Id()
if !ok {
return newError(EFieldMissing, "ID field missing from cluster in VM object")
}
v.clusterID = clusterID
return nil
}
func vmStatusConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
status, ok := sdkObject.Status()
if !ok {
return newFieldNotFound("vm", "status")
}
v.status = VMStatus(status)
return nil
}
func vmTemplateConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
template, ok := sdkObject.Template()
if !ok {
return newFieldNotFound("VM", "template")
}
templateID, ok := template.Id()
if !ok {
return newFieldNotFound("template in VM", "template ID")
}
v.templateID = TemplateID(templateID)
return nil
}
func vmCPUConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
cpu, err := convertSDKVMCPU(sdkObject)
if err != nil {
return err
}
v.cpu = cpu
return nil
}
func vmHugePagesConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
hugePages, err := hugePagesFromSDKVM(sdkObject)
if err != nil {
return err
}
v.hugePages = hugePages
return nil
}
func vmInitializationConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
var vmInitialization *initialization
vmInitialization, err := convertSDKInitialization(sdkObject)
if err != nil {
return err
}
v.initialization = vmInitialization
return nil
}
func vmTagsConverter(sdkObject *ovirtsdk.Vm, v *vm) error {
var tagIDs []string
if sdkTags, ok := sdkObject.Tags(); ok {
for _, tag := range sdkTags.Slice() {
tagID, _ := tag.Id()
tagIDs = append(tagIDs, tagID)
}
}
v.tagIDs = tagIDs
return nil
}
func convertSDKVMCPU(sdkObject *ovirtsdk.Vm) (*vmCPU, error) {
sdkCPU, ok := sdkObject.Cpu()
if !ok {
return nil, newFieldNotFound("VM", "CPU")
}
cpuTopo, ok := sdkCPU.Topology()
if !ok {
return nil, newFieldNotFound("CPU in VM", "CPU topo")
}