-
Notifications
You must be signed in to change notification settings - Fork 454
/
Copy pathvirtual_machine_helper.go
1066 lines (954 loc) · 34.5 KB
/
virtual_machine_helper.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package virtualmachine
import (
"context"
"errors"
"fmt"
"log"
"net"
"strconv"
"strings"
"time"
"github.com/vmware/govmomi/vapi/library"
"github.com/vmware/govmomi/vapi/vcenter"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/folder"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/provider"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/structure"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/vappcontainer"
"github.com/hashicorp/terraform-provider-vsphere/vsphere/internal/helper/viapi"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/view"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
var errGuestShutdownTimeout = errors.New("the VM did not power off within the specified amount of time")
// vmUUIDSearchIndexVersion denotes the minimum version we use the SearchIndex
// VM UUID search for. All versions lower than this use ContainerView to find
// the VM.
var vmUUIDSearchIndexVersion = viapi.VSphereVersion{
Major: 6,
Minor: 5,
}
// UUIDNotFoundError is an error type that is returned when a
// virtual machine could not be found by UUID.
type UUIDNotFoundError struct {
s string
}
// VCenterDeploy containss everything required to create a VM from a content
// library item, and reduces the number of arguments passed between functions.
type VCenterDeploy struct {
VCenterManager *vcenter.Manager
VMName string
Annotation string
FolderID string
DatastoreID string
ResourcePoolID string
HostSystemID string
StoragePolicyID string
DiskType string
NetworkMap []vcenter.NetworkMapping
VAppProperties []vcenter.Property
LibraryItem *library.Item
}
// Error implements error for UUIDNotFoundError.
func (e *UUIDNotFoundError) Error() string {
return e.s
}
// newUUIDNotFoundError returns a new UUIDNotFoundError with the
// text populated.
func newUUIDNotFoundError(s string) *UUIDNotFoundError {
return &UUIDNotFoundError{
s: s,
}
}
// IsUUIDNotFoundError returns true if the error is a UUIDNotFoundError.
func IsUUIDNotFoundError(err error) bool {
_, ok := err.(*UUIDNotFoundError)
return ok
}
func List(client *govmomi.Client) ([]*object.VirtualMachine, error) {
return vmsByPath(client, "/*")
}
func vmsByPath(client *govmomi.Client, path string) ([]*object.VirtualMachine, error) {
ctx := context.TODO()
var vms []*object.VirtualMachine
finder := find.NewFinder(client.Client, false)
es, err := finder.ManagedObjectListChildren(ctx, path+"/*", "vm", "folder", "pool")
if err != nil {
return nil, err
}
for _, id := range es {
switch {
case id.Object.Reference().Type == "VirtualMachine":
vm, err := FromMOID(client, id.Object.Reference().Value)
if err != nil {
return nil, err
}
vms = append(vms, vm)
case id.Object.Reference().Type == "Folder" || id.Object.Reference().Type == "ResourcePool":
newRPs, err := vmsByPath(client, id.Path)
if err != nil {
return nil, err
}
vms = append(vms, newRPs...)
default:
continue
}
}
return vms, nil
}
// FromUUID locates a virtualMachine by its UUID.
func FromUUID(client *govmomi.Client, uuid string) (*object.VirtualMachine, error) {
log.Printf("[DEBUG] Locating virtual machine with UUID %q", uuid)
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
var result object.Reference
var err error
version := viapi.ParseVersionFromClient(client)
expected := vmUUIDSearchIndexVersion
expected.Product = version.Product
if version.Older(expected) {
result, err = virtualMachineFromContainerView(ctx, client, uuid)
} else {
result, err = virtualMachineFromSearchIndex(ctx, client, uuid)
}
if err != nil {
return nil, err
}
// We need to filter our object through finder to ensure that the
// InventoryPath field is populated, or else functions that depend on this
// being present will fail.
finder := find.NewFinder(client.Client, false)
vm, err := finder.ObjectReference(ctx, result.Reference())
if err != nil {
return nil, err
}
// Should be safe to return here. If our reference returned here and is not a
// VM, then we have bigger problems and to be honest we should be panicking
// anyway.
log.Printf("[DEBUG] VM %q found for UUID %q", vm.(*object.VirtualMachine).InventoryPath, uuid)
return vm.(*object.VirtualMachine), nil
}
// virtualMachineFromSearchIndex gets the virtual machine reference via the
// SearchIndex MO and is the method used to fetch UUIDs on newer versions of
// vSphere.
func virtualMachineFromSearchIndex(ctx context.Context, client *govmomi.Client, uuid string) (object.Reference, error) {
log.Printf("[DEBUG] Using SearchIndex to look up UUID %q", uuid)
search := object.NewSearchIndex(client.Client)
result, err := search.FindByUuid(ctx, nil, uuid, true, structure.BoolPtr(false))
if err != nil {
return nil, err
}
if result == nil {
return nil, newUUIDNotFoundError(fmt.Sprintf("virtual machine with UUID %q not found", uuid))
}
return result, nil
}
// virtualMachineFromContainerView is a compatibility method that is
// used when the version of vSphere is too old to support using SearchIndex's
// FindByUuid method correctly. This is mainly to facilitate the ability to use
// FromUUID to find both templates in addition to virtual machines, which
// historically was not supported by FindByUuid.
func virtualMachineFromContainerView(ctx context.Context, client *govmomi.Client, uuid string) (object.Reference, error) {
log.Printf("[DEBUG] Using ContainerView to look up UUID %q", uuid)
m := view.NewManager(client.Client)
v, err := m.CreateContainerView(ctx, client.ServiceContent.RootFolder, []string{"VirtualMachine"}, true)
if err != nil {
return nil, err
}
defer func() {
if err = v.Destroy(ctx); err != nil {
log.Printf("[DEBUG] virtualMachineFromContainerView: Unexpected error destroying container view: %s", err)
}
}()
var vms, results []mo.VirtualMachine
err = v.Retrieve(ctx, []string{"VirtualMachine"}, []string{"config.uuid"}, &results)
if err != nil {
return nil, err
}
for _, result := range results {
if result.Config == nil {
continue
}
if result.Config.Uuid == uuid {
vms = append(vms, result)
}
}
switch {
case len(vms) == 0:
return nil, newUUIDNotFoundError(fmt.Sprintf("virtual machine with UUID %q not found", uuid))
case len(vms) > 1:
return nil, fmt.Errorf("multiple virtual machines with UUID %q found", uuid)
}
return object.NewReference(client.Client, vms[0].Self), nil
}
// FromMOID locates a virtualMachine by its managed
// object reference ID.
func FromMOID(client *govmomi.Client, id string) (*object.VirtualMachine, error) {
finder := find.NewFinder(client.Client, false)
ref := types.ManagedObjectReference{
Type: "VirtualMachine",
Value: id,
}
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
vm, err := finder.ObjectReference(ctx, ref)
if err != nil {
return nil, err
}
// Should be safe to return here. If our reference returned here and is not a
// VM, then we have bigger problems and to be honest we should be panicking
// anyway.
return vm.(*object.VirtualMachine), nil
}
// FromPath returns a VirtualMachine via its supplied path.
func FromPath(client *govmomi.Client, path string, dc *object.Datacenter) (*object.VirtualMachine, error) {
finder := find.NewFinder(client.Client, false)
if dc != nil {
finder.SetDatacenter(dc)
}
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
return finder.VirtualMachine(ctx, path)
}
// Properties is a convenience method that wraps fetching the
// VirtualMachine MO from its higher-level object.
func Properties(vm *object.VirtualMachine) (*mo.VirtualMachine, error) {
log.Printf("[DEBUG] Fetching properties for VM %q", vm.InventoryPath)
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
var props mo.VirtualMachine
if err := vm.Properties(ctx, vm.Reference(), nil, &props); err != nil {
return nil, err
}
return &props, nil
}
// ConfigOptions is a convenience method that wraps fetching the VirtualMachine ConfigOptions
// as returned by QueryConfigOption.
func ConfigOptions(vm *object.VirtualMachine) (*types.VirtualMachineConfigOption, error) {
// First grab the properties so that we can sneak the EnvironmentBrowser out of it
props, err := Properties(vm)
if err != nil {
return nil, err
}
// Make a context so we can timeout according to the provider configuration
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
// Build a request for the config option, and then query for configuration options
log.Printf("[DEBUG] Fetching configuration options for VM %q", vm.InventoryPath)
request := types.QueryConfigOption{This: props.EnvironmentBrowser}
response, err := methods.QueryConfigOption(ctx, vm.Client(), &request)
if err != nil {
return nil, err
}
return response.Returnval, nil
}
// WaitForGuestIP waits for a virtual machine to have an IP address.
//
// The timeout is specified in minutes. If zero or a negative value is passed,
// the waiter returns without error immediately.
func WaitForGuestIP(client *govmomi.Client, vm *object.VirtualMachine, timeout int, ignoredGuestIPs []interface{}) error {
if timeout < 1 {
log.Printf("[DEBUG] Skipping IP waiter for VM %q", vm.InventoryPath)
return nil
}
log.Printf(
"[DEBUG] Waiting for an available IP address on VM %q (timeout = %dm)",
vm.InventoryPath,
timeout,
)
p := client.PropertyCollector()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer cancel()
err := property.Wait(ctx, p, vm.Reference(), []string{"guest.ipAddress"}, func(pc []types.PropertyChange) bool {
for _, c := range pc {
if c.Op != types.PropertyChangeOpAssign {
continue
}
if c.Val == nil {
continue
}
ip := net.ParseIP(c.Val.(string))
if skipIPAddrForWaiter(ip, ignoredGuestIPs) {
continue
}
return true
}
return false
})
if err != nil {
// Provide a friendly error message if we timed out waiting for a routable IP.
if ctx.Err() == context.DeadlineExceeded {
return errors.New("timeout waiting for an available IP address")
}
return err
}
log.Printf("[DEBUG] IP address is now available for VM %q", vm.InventoryPath)
return nil
}
// WaitForGuestNet waits for a virtual machine to have routable network
// access. This is denoted as a gateway, and at least one IP address that can
// reach that gateway. This function supports both IPv4 and IPv6, and returns
// the moment either stack is routable - it doesn't wait for both.
//
// The timeout is specified in minutes. If zero or a negative value is passed,
// the waiter returns without error immediately.
func WaitForGuestNet(client *govmomi.Client, vm *object.VirtualMachine, routable bool, timeout int, ignoredGuestIPs []interface{}) error {
if timeout < 1 {
log.Printf("[DEBUG] Skipping network waiter for VM %q", vm.InventoryPath)
return nil
}
log.Printf(
"[DEBUG] Waiting for an available IP address on VM %q (routable= %t, timeout = %dm)",
vm.InventoryPath,
routable,
timeout,
)
var v4gw, v6gw net.IP
p := client.PropertyCollector()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer cancel()
err := property.Wait(ctx, p, vm.Reference(), []string{"guest.net", "guest.ipStack"}, func(pc []types.PropertyChange) bool {
for _, c := range pc {
if c.Op != types.PropertyChangeOpAssign {
continue
}
switch v := c.Val.(type) {
case types.ArrayOfGuestStackInfo:
for _, s := range v.GuestStackInfo {
if s.IpRouteConfig != nil {
for _, r := range s.IpRouteConfig.IpRoute {
switch r.Network {
case "0.0.0.0":
v4gw = net.ParseIP(r.Gateway.IpAddress)
case "::":
v6gw = net.ParseIP(r.Gateway.IpAddress)
}
}
}
}
case types.ArrayOfGuestNicInfo:
for _, n := range v.GuestNicInfo {
if n.IpConfig != nil {
for _, addr := range n.IpConfig.IpAddress {
ip := net.ParseIP(addr.IpAddress)
if skipIPAddrForWaiter(ip, ignoredGuestIPs) {
continue
}
if !routable {
// We are done. The rest of this block concerns itself with
// checking for a routable address, but the waiter has been
// flagged to not wait for one.
return true
}
var mask net.IPMask
if ip.To4() != nil {
mask = net.CIDRMask(int(addr.PrefixLength), 32)
} else {
mask = net.CIDRMask(int(addr.PrefixLength), 128)
}
if ip.Mask(mask).Equal(v4gw.Mask(mask)) || ip.Mask(mask).Equal(v6gw.Mask(mask)) {
return true
}
}
}
}
}
}
return false
})
if err != nil {
// Provide a friendly error message if we timed out waiting for a routable IP.
if ctx.Err() == context.DeadlineExceeded {
return errors.New("timeout waiting for an available IP address")
}
return err
}
log.Printf("[DEBUG] IP address(es) is/are now available for VM %q", vm.InventoryPath)
return nil
}
func skipIPAddrForWaiter(ip net.IP, ignoredGuestIPs []interface{}) bool {
switch {
case ip.IsLinkLocalMulticast():
fallthrough
case ip.IsLinkLocalUnicast():
fallthrough
case ip.IsLoopback():
fallthrough
case ip.IsMulticast():
return true
default:
// ignoredGuestIPs pre-validated by Schema!
for _, ignoredGuestIP := range ignoredGuestIPs {
if strings.Contains(ignoredGuestIP.(string), "/") {
_, ignoredIPNet, _ := net.ParseCIDR(ignoredGuestIP.(string))
if ignoredIPNet.Contains(ip) {
return true
}
} else if net.ParseIP(ignoredGuestIP.(string)).Equal(ip) {
return true
}
}
}
return false
}
func blockUntilReadyForMethod(ctx context.Context, method string, vm *object.VirtualMachine) error {
log.Printf("[DEBUG] blockUntilReadyForMethod: Going to block until %q is no longer in the Disabled Methods list for vm %s", method, vm.Reference().Value)
for {
vprops, err := Properties(vm)
if err != nil {
return fmt.Errorf("cannot fetch properties of created virtual machine: %s", err)
}
stillPending := false
for _, methodName := range vprops.DisabledMethod {
if methodName == method {
stillPending = true
break
}
}
if !stillPending {
log.Printf("[DEBUG] blockUntilReadyForMethod: %q no longer disabled for vm %s", method, vm.Reference().Value)
break
}
select {
case <-time.After(5 * time.Second):
log.Printf("[DEBUG] blockUntilReadyForMethod: %q still disabled for vm %s, about to check again", method, vm.Reference().Value)
case <-ctx.Done():
return fmt.Errorf("blockUntilReadyForMethod: timed out while waiting for %q to become available for vm %s", method, vm.Reference().Value)
}
}
return nil
}
// Create wraps the creation of a virtual machine and the subsequent waiting of
// the task. A higher-level virtual machine object is returned.
func Create(c *govmomi.Client, f *object.Folder, s types.VirtualMachineConfigSpec, p *object.ResourcePool,
h *object.HostSystem, timeout time.Duration) (*object.VirtualMachine, error) {
log.Printf("[DEBUG] Creating virtual machine %q", fmt.Sprintf("%s/%s", f.InventoryPath, s.Name))
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
var task *object.Task
// Check to see if the resource pool is a vApp
vc, err := vappcontainer.FromID(c, p.Reference().Value)
if err != nil {
if !viapi.IsManagedObjectNotFoundError(err) {
return nil, err
}
task, err = f.CreateVM(ctx, s, p, h)
} else {
task, err = vc.CreateChildVM(ctx, s, h)
}
if err != nil {
return nil, err
}
tctx, tcancel := context.WithTimeout(context.Background(), timeout)
defer tcancel()
result, err := task.WaitForResultEx(tctx, nil)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Virtual machine %q: creation complete (MOID: %q)", fmt.Sprintf("%s/%s", f.InventoryPath, s.Name), result.Result.(types.ManagedObjectReference).Value)
return FromMOID(c, result.Result.(types.ManagedObjectReference).Value)
}
// Clone wraps the creation of a virtual machine and the subsequent waiting of
// the task. A higher-level virtual machine object is returned.
func Clone(c *govmomi.Client, src *object.VirtualMachine, f *object.Folder, name string, spec types.VirtualMachineCloneSpec, timeout int) (*object.VirtualMachine, error) {
log.Printf("[DEBUG] Cloning virtual machine %q", fmt.Sprintf("%s/%s", f.InventoryPath, name))
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer cancel()
task, err := src.Clone(ctx, f, name, spec)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
err = errors.New("timeout waiting for clone to complete")
}
return nil, err
}
result, err := task.WaitForResultEx(ctx, nil)
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
err = errors.New("timeout waiting for clone to complete")
}
return nil, err
}
log.Printf("[DEBUG] Virtual machine %q: clone complete (MOID: %q)", fmt.Sprintf("%s/%s", f.InventoryPath, name), result.Result.(types.ManagedObjectReference).Value)
return FromMOID(c, result.Result.(types.ManagedObjectReference).Value)
}
// Deploy clones a virtual machine from a content library item.
func Deploy(deployData *VCenterDeploy) (*types.ManagedObjectReference, error) {
log.Printf("[DEBUG] virtualmachine.Deploy: Deploying VM from Content Library item.")
// Get OVF mappings for NICs
switch deployData.LibraryItem.Type {
case library.ItemTypeOVF:
return deployData.deployOvf()
case library.ItemTypeVMTX:
return deployData.deployVmtx()
default:
return nil, fmt.Errorf("unsupported library item type: %s", deployData.LibraryItem.Type)
}
}
func (deployData *VCenterDeploy) deployVmtx() (*types.ManagedObjectReference, error) {
storage := &vcenter.DiskStorage{
Datastore: deployData.DatastoreID,
StoragePolicy: &vcenter.StoragePolicy{
Policy: deployData.StoragePolicyID,
Type: "USE_SOURCE_POLICY",
},
}
if deployData.StoragePolicyID != "" {
storage.StoragePolicy.Type = "USE_SPECIFIED_POLICY"
}
deploy := vcenter.DeployTemplate{
Name: deployData.VMName,
Description: deployData.Annotation,
DiskStorage: storage,
VMHomeStorage: storage,
Placement: &vcenter.Placement{
ResourcePool: deployData.ResourcePoolID,
Host: deployData.HostSystemID,
Folder: deployData.FolderID,
},
}
ctx := context.TODO()
return deployData.VCenterManager.DeployTemplateLibraryItem(ctx, deployData.LibraryItem.ID, deploy)
}
func (deployData *VCenterDeploy) deployOvf() (*types.ManagedObjectReference, error) {
deploy := vcenter.Deploy{
DeploymentSpec: vcenter.DeploymentSpec{
Name: deployData.VMName,
DefaultDatastoreID: deployData.DatastoreID,
AcceptAllEULA: true,
Annotation: deployData.Annotation,
AdditionalParams: []vcenter.AdditionalParams{
{
Class: vcenter.ClassDeploymentOptionParams,
Type: vcenter.TypeDeploymentOptionParams,
},
{
Class: vcenter.ClassPropertyParams,
Type: vcenter.TypePropertyParams,
Properties: deployData.VAppProperties,
},
},
NetworkMappings: deployData.NetworkMap,
StorageProvisioning: deployData.DiskType,
StorageProfileID: deployData.StoragePolicyID,
},
Target: vcenter.Target{
ResourcePoolID: deployData.ResourcePoolID,
HostID: deployData.HostSystemID,
FolderID: deployData.FolderID,
},
}
ctx := context.TODO()
return deployData.VCenterManager.DeployLibraryItem(ctx, deployData.LibraryItem.ID, deploy)
}
// VAppProperties converts the vApp properties from the configuration and
// converts them into a slice of vcenter.Properties to be used while deploying
// content library items as VMs.
func VAppProperties(propertyMap map[string]interface{}) []vcenter.Property {
properties := []vcenter.Property{}
for key, value := range propertyMap {
vcenterProperty := vcenter.Property{
ID: key,
Label: "",
Value: value.(string),
}
properties = append(properties, vcenterProperty)
}
return properties
}
// DiskType converts standard disk type labels into labels used in content
// library deployment specs.
func DiskType(d *schema.ResourceData) string {
thin := d.Get("disk.0.thin_provisioned").(bool)
eagerlyScrub := d.Get("disk.0.eagerly_scrub").(bool)
switch {
case thin:
return "thin"
case eagerlyScrub:
return "eagerZeroedThick"
default:
return "thick"
}
}
// Customize wraps the customization of a virtual machine and the subsequent
// waiting of the task.
func Customize(vm *object.VirtualMachine, spec types.CustomizationSpec) error {
log.Printf("[DEBUG] Sending customization spec to virtual machine %q", vm.InventoryPath)
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
task, err := vm.Customize(ctx, spec)
if err != nil {
return err
}
tctx, tcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer tcancel()
return task.WaitEx(tctx)
}
// PowerOn wraps powering on a VM and the waiting for the subsequent task.
func PowerOn(vm *object.VirtualMachine, pTimeout time.Duration) error {
vmPath := vm.InventoryPath
log.Printf("[DEBUG] Powering on virtual machine %q", vmPath)
var ctxTimeout time.Duration
if pTimeout > provider.DefaultAPITimeout {
ctxTimeout = pTimeout
} else {
ctxTimeout = provider.DefaultAPITimeout
}
ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
defer cancel()
err := blockUntilReadyForMethod(ctx, "PowerOnVM_Task", vm)
if err != nil {
return err
}
// This is the controversial part. Although we take every precaution to make sure the VM
// is in a state that can be started we have noticed that vsphere will randomly fail to
// power on the vm with "InvalidState" errors.
//
// We're adding a small loop that will try to power on the VM until we hit a timeout
// or manage to call PowerOnVM_Task successfully.
powerLoop:
for {
select {
case <-time.After(500 * time.Millisecond):
vprops, err := Properties(vm)
if err != nil {
return fmt.Errorf("cannot fetch properties of created virtual machine: %s", err)
}
if vprops.Runtime.PowerState == types.VirtualMachinePowerStatePoweredOff {
log.Printf("[DEBUG] VM %q is powered off, attempting to power on.", vmPath)
task, err := vm.PowerOn(ctx)
if err != nil {
log.Printf("[DEBUG] Failed to submit PowerOn task for vm %q. Error: %s", vmPath, err)
return fmt.Errorf("failed to submit poweron task for vm %q: %s", vmPath, err)
}
err = task.WaitEx(ctx)
if err != nil {
if err.Error() == "The operation is not allowed in the current state." {
log.Printf("[DEBUG] vm %q cannot be powered on in the current state", vmPath)
continue powerLoop
} else {
log.Printf("[DEBUG] PowerOn task for vm %q failed. Error: %s", vmPath, err)
return fmt.Errorf("powerOn task for vm %q failed: %s", vmPath, err)
}
}
log.Printf("[DEBUG] PowerOn task for VM %q was successful.", vmPath)
break powerLoop
}
case <-ctx.Done():
return fmt.Errorf("timed out while trying to power on vm %q", vmPath)
}
}
return nil
}
// PowerOff wraps powering off a VM and the waiting for the subsequent task.
func PowerOff(vm *object.VirtualMachine) error {
log.Printf("[DEBUG] Forcing power off of virtual machine of %q", vm.InventoryPath)
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
task, err := vm.PowerOff(ctx)
if err != nil {
return err
}
tctx, tcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer tcancel()
return task.WaitEx(tctx)
}
// ShutdownGuest wraps the graceful shutdown of a guest VM, and then waiting an
// appropriate amount of time for the guest power state to go to powered off.
// If the VM does not power off in the shutdown period specified by timeout (in
// minutes), an error is returned.
//
// The minimum value for timeout is 1 minute - setting to a 0 or negative value
// is not allowed and will just reset the timeout to the minimum.
func ShutdownGuest(client *govmomi.Client, vm *object.VirtualMachine, timeout int) error {
log.Printf("[DEBUG] Attempting guest shutdown of virtual machine %q", vm.InventoryPath)
sctx, scancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer scancel()
if err := vm.ShutdownGuest(sctx); err != nil {
return err
}
// We now wait on VM power state to be powerOff, via a property collector that waits on power state.
p := client.PropertyCollector()
if timeout < 1 {
timeout = 1
}
pctx, pcancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer pcancel()
err := property.Wait(pctx, p, vm.Reference(), []string{"runtime.powerState"}, func(pc []types.PropertyChange) bool {
for _, c := range pc {
if c.Op != types.PropertyChangeOpAssign {
continue
}
if v, ok := c.Val.(types.VirtualMachinePowerState); ok {
if v == types.VirtualMachinePowerStatePoweredOff {
return true
}
}
}
return false
})
if err != nil {
// Provide a friendly error message if we timed out waiting for a shutdown.
if pctx.Err() == context.DeadlineExceeded {
return errGuestShutdownTimeout
}
return err
}
return nil
}
// GracefulPowerOff is a meta-operation that handles powering down of virtual
// machines. A graceful shutdown is attempted first if possible (VMware Tools
// is installed, and the guest state is not suspended), and then, if allowed, a
// power-off is forced if that fails.
func GracefulPowerOff(client *govmomi.Client, vm *object.VirtualMachine, timeout int, force bool) error {
vprops, err := Properties(vm)
if err != nil {
return err
}
// First we attempt a guest shutdown if we have VMware Tools and if the VM is
// actually powered on (we don't expect that a graceful shutdown would
// complete on a suspended VM, so there's really no point in trying).
if vprops.Runtime.PowerState == types.VirtualMachinePowerStatePoweredOn && vprops.Guest != nil && vprops.Guest.ToolsRunningStatus == string(types.VirtualMachineToolsRunningStatusGuestToolsRunning) {
if err := ShutdownGuest(client, vm, timeout); err != nil {
if err == errGuestShutdownTimeout && !force {
return err
}
} else {
return nil
}
}
// If the guest shutdown failed (and we were allowed to proceed), or
// conditions did not satisfy the criteria for a graceful shutdown, do a full
// power-off of the VM.
return PowerOff(vm)
}
// MoveToFolder moves a virtual machine to the specified folder.
func MoveToFolder(client *govmomi.Client, vm *object.VirtualMachine, relative string) error {
log.Printf("[DEBUG] Moving virtual %q to VM path %q", vm.InventoryPath, relative)
f, err := folder.VirtualMachineFolderFromObject(client, vm, relative)
if err != nil {
return err
}
return folder.MoveObjectTo(vm.Reference(), f)
}
// Reconfigure wraps the Reconfigure task and the subsequent waiting for
// the task to complete.
func Reconfigure(vm *object.VirtualMachine, spec types.VirtualMachineConfigSpec, timeout time.Duration) error {
log.Printf("[DEBUG] Reconfiguring virtual machine %q", vm.InventoryPath)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
task, err := vm.Reconfigure(ctx, spec)
if err != nil {
return err
}
tctx, tcancel := context.WithTimeout(context.Background(), timeout)
defer tcancel()
return task.WaitEx(tctx)
}
// Relocate wraps the Relocate task and the subsequent waiting for the task to
// complete.
func Relocate(vm *object.VirtualMachine, spec types.VirtualMachineRelocateSpec, timeout int) error {
log.Printf("[DEBUG] Beginning migration of virtual machine %q (timeout %d)", vm.InventoryPath, timeout)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*time.Duration(timeout))
defer cancel()
task, err := vm.Relocate(ctx, spec, "")
if err != nil {
return err
}
if err := task.WaitEx(ctx); err != nil {
// Provide a friendly error message if we timed out waiting for the migration.
if ctx.Err() == context.DeadlineExceeded {
return errors.New("timeout waiting for migration to complete")
}
}
return nil
}
// Destroy wraps the Destroy task and the subsequent waiting for the task to
// complete.
func Destroy(vm *object.VirtualMachine) error {
log.Printf("[DEBUG] Deleting virtual machine %q", vm.InventoryPath)
ctx, cancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer cancel()
task, err := vm.Destroy(ctx)
if err != nil {
return err
}
tctx, tcancel := context.WithTimeout(context.Background(), provider.DefaultAPITimeout)
defer tcancel()
return task.WaitEx(tctx)
}
// MOIDForUUIDResult is a struct that holds a virtual machine UUID -> MOID
// association, designed to be used as a helper for mass returning the results
// of translating multiple UUIDs to managed object IDs for various virtual
// machine operations.
type MOIDForUUIDResult struct {
// The UUID of a virtual machine.
UUID string
// The matching managed object reference ID for the virtual machine at the ID
// referenced by UUID.
MOID string
}
// MOIDForUUIDResults is a slice that holds multiple MOIDForUUIDResult structs.
type MOIDForUUIDResults []MOIDForUUIDResult
// MOIDForUUID returns the managed object reference ID for a specific virtual
// machine UUID and returns a MOIDForUUIDResult with the appropriate
// association.
func MOIDForUUID(client *govmomi.Client, uuid string) (MOIDForUUIDResult, error) {
vm, err := FromUUID(client, uuid)
if err != nil {
return MOIDForUUIDResult{}, err
}
return MOIDForUUIDResult{
UUID: uuid,
MOID: vm.Reference().Value,
}, nil
}
// UUIDForMOID returns the managed object reference ID for a specific virtual
// machine MOID and returns a MOIDForUUIDResult with the appropriate
// association.
func UUIDForMOID(client *govmomi.Client, moid string) (MOIDForUUIDResult, error) {
vm, err := FromMOID(client, moid)
if err != nil {
return MOIDForUUIDResult{}, err
}
props, err := Properties(vm)
if err != nil {
return MOIDForUUIDResult{}, err
}
return MOIDForUUIDResult{
UUID: props.Config.Uuid,
MOID: vm.Reference().Value,
}, nil
}
// MOIDsForUUIDs returns a MOIDForUUIDResults for a list of UUIDs. If one UUID
// cannot be found, an error is returned. There are no partial results
// returned.
func MOIDsForUUIDs(client *govmomi.Client, uuids []string) (MOIDForUUIDResults, error) {
var results MOIDForUUIDResults
for _, uuid := range uuids {
result, err := MOIDForUUID(client, uuid)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}
// UUIDsForMOIDs returns a MOIDForUUIDResults for a list of MOIDs. If one MOID
// cannot be found, an error is returned. There are no partial results
// returned.
func UUIDsForMOIDs(client *govmomi.Client, moids []string) (MOIDForUUIDResults, error) {
var results MOIDForUUIDResults
for _, uuid := range moids {
result, err := UUIDForMOID(client, uuid)
if err != nil {
return nil, err
}
results = append(results, result)
}
return results, nil
}
// UUIDsForManagedObjectReferences returns a MOIDForUUIDResults for a list of
// ManagedObjectReferences. If one cannot be found, an error is returned. There
// are no partial results returned.
func UUIDsForManagedObjectReferences(client *govmomi.Client, refs []types.ManagedObjectReference) (MOIDForUUIDResults, error) {
var moids []string
for _, ref := range refs {
moids = append(moids, ref.Value)
}
return UUIDsForMOIDs(client, moids)
}
// MOIDs returns all MOIDs in a MOIDForUUIDResults.
func (r MOIDForUUIDResults) MOIDs() []string {
var moids []string
for _, result := range r {
moids = append(moids, result.MOID)
}
return moids
}
// ManagedObjectReferences returns all MOIDs in a MOIDForUUIDResults, as
// ManagedObjectReferences as type VirtualMachine.
func (r MOIDForUUIDResults) ManagedObjectReferences() []types.ManagedObjectReference {
var refs []types.ManagedObjectReference
for _, result := range r {
refs = append(refs, types.ManagedObjectReference{
Type: "VirtualMachine",
Value: result.MOID,
})
}
return refs
}
// UUIDs returns all UUIDs in a MOIDForUUIDResults.
func (r MOIDForUUIDResults) UUIDs() []string {
var uuids []string
for _, result := range r {
uuids = append(uuids, result.UUID)
}
return uuids
}
// GetHardwareVersionID gets the hardware version string from integer
func GetHardwareVersionID(vint int) string {