@@ -753,18 +753,6 @@ func resourceVSphereVirtualMachineUpdate(d *schema.ResourceData, meta interface{
753753 return err
754754 }
755755
756- if d .HasChange ("vtpm" ) {
757-
758- spec .DeviceChange = append (spec .DeviceChange , & types.VirtualDeviceConfigSpec {
759- Operation : types .VirtualDeviceConfigSpecOperationAdd ,
760- Device : & types.VirtualTPM {
761- VirtualDevice : types.VirtualDevice {
762- Key : - 1 ,
763- },
764- },
765- })
766- }
767-
768756 // Only carry out the reconfigure if we actually have a change to process.
769757 cv := virtualmachine .GetHardwareVersionNumber (vprops .Config .Version )
770758 tv := d .Get ("hardware_version" ).(int )
@@ -1403,17 +1391,6 @@ func resourceVSphereVirtualMachineCreateBareStandard(
14031391 VmPathName : fmt .Sprintf ("[%s]" , ds .Name ()),
14041392 }
14051393
1406- if vtpms , ok := d .GetOk ("vtpm" ); ok && len (vtpms .([]interface {})) > 0 {
1407- spec .DeviceChange = append (spec .DeviceChange , & types.VirtualDeviceConfigSpec {
1408- Operation : types .VirtualDeviceConfigSpecOperationAdd ,
1409- Device : & types.VirtualTPM {
1410- VirtualDevice : types.VirtualDevice {
1411- Key : - 1 ,
1412- },
1413- },
1414- })
1415- }
1416-
14171394 timeout := meta .(* Client ).timeout
14181395 vm , err := virtualmachine .Create (client , fo , spec , pool , hs , timeout )
14191396 if err != nil {
@@ -1739,6 +1716,18 @@ func resourceVSphereVirtualMachinePostDeployChanges(d *schema.ResourceData, meta
17391716 )
17401717 }
17411718 cfgSpec .DeviceChange = virtualdevice .AppendDeviceChangeSpec (cfgSpec .DeviceChange , delta ... )
1719+
1720+ // VTPM
1721+ devices , delta , err = virtualdevice .VtpmApplyOperation (d , devices )
1722+ if err != nil {
1723+ return resourceVSphereVirtualMachineRollbackCreate (
1724+ d ,
1725+ meta ,
1726+ vm ,
1727+ fmt .Errorf ("error processing VTPM device changes post-clone: %s" , err ),
1728+ )
1729+ }
1730+ cfgSpec .DeviceChange = virtualdevice .AppendDeviceChangeSpec (cfgSpec .DeviceChange , delta ... )
17421731 log .Printf ("[DEBUG] %s: Final device list: %s" , resourceVSphereVirtualMachineIDString (d ), virtualdevice .DeviceListString (devices ))
17431732 log .Printf ("[DEBUG] %s: Final device change cfgSpec: %s" , resourceVSphereVirtualMachineIDString (d ), virtualdevice .DeviceChangeString (cfgSpec .DeviceChange ))
17441733
@@ -2043,6 +2032,13 @@ func applyVirtualDevices(d *schema.ResourceData, c *govmomi.Client, l object.Vir
20432032 return nil , err
20442033 }
20452034 spec = virtualdevice .AppendDeviceChangeSpec (spec , delta ... )
2035+
2036+ // VTPM
2037+ l , delta , err = virtualdevice .VtpmApplyOperation (d , l )
2038+ if err != nil {
2039+ return nil , err
2040+ }
2041+ spec = virtualdevice .AppendDeviceChangeSpec (spec , delta ... )
20462042 log .Printf ("[DEBUG] %s: Final device list: %s" , resourceVSphereVirtualMachineIDString (d ), virtualdevice .DeviceListString (l ))
20472043 log .Printf ("[DEBUG] %s: Final device change spec: %s" , resourceVSphereVirtualMachineIDString (d ), virtualdevice .DeviceChangeString (spec ))
20482044 return spec , nil
0 commit comments