Skip to content

Commit db21a08

Browse files
authored
feat: add/remove TPM module when editing and cloning r/virtual_machine (#2542)
Signed-off-by: Stoyan Zhelyazkov <[email protected]>
1 parent d335ff3 commit db21a08

File tree

3 files changed

+336
-23
lines changed

3 files changed

+336
-23
lines changed

vsphere/internal/virtualdevice/virtual_machine_device_subresource.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,3 +1151,54 @@ func PciPassthroughPostCloneOperation(d *schema.ResourceData, c *govmomi.Client,
11511151
}
11521152
return applyConfig.VirtualDevice, applyConfig.Spec, nil
11531153
}
1154+
1155+
func VtpmApplyOperation(d *schema.ResourceData, l object.VirtualDeviceList) (object.VirtualDeviceList, []types.BaseVirtualDeviceConfigSpec, error) {
1156+
vtpmConfigRaw := d.Get("vtpm")
1157+
vtpmConfig := vtpmConfigRaw.([]interface{})
1158+
1159+
// There can only be one TPM module per virtual machine
1160+
// so we only expect 1 element in this slice at the most
1161+
vtpmDevices := l.Select(func(device types.BaseVirtualDevice) bool {
1162+
if _, ok := device.(*types.VirtualTPM); ok {
1163+
return true
1164+
}
1165+
return false
1166+
})
1167+
1168+
var specs []types.BaseVirtualDeviceConfigSpec
1169+
1170+
if len(vtpmDevices) > len(vtpmConfig) {
1171+
// delete device
1172+
spec := &types.VirtualDeviceConfigSpec{
1173+
Operation: types.VirtualDeviceConfigSpecOperationRemove,
1174+
Device: &types.VirtualTPM{
1175+
VirtualDevice: types.VirtualDevice{
1176+
Key: vtpmDevices[0].GetVirtualDevice().Key,
1177+
},
1178+
},
1179+
}
1180+
1181+
specs = append(specs, spec)
1182+
}
1183+
1184+
if len(vtpmConfig) > len(vtpmDevices) {
1185+
// create device
1186+
spec := &types.VirtualDeviceConfigSpec{
1187+
Operation: types.VirtualDeviceConfigSpecOperationAdd,
1188+
Device: &types.VirtualTPM{
1189+
VirtualDevice: types.VirtualDevice{
1190+
Key: -1,
1191+
},
1192+
},
1193+
}
1194+
1195+
specs = append(specs, spec)
1196+
}
1197+
1198+
if len(specs) > 0 {
1199+
_ = d.Set("reboot_required", true)
1200+
}
1201+
1202+
l = applyDeviceChange(l, specs)
1203+
return l, specs, nil
1204+
}

vsphere/resource_vsphere_virtual_machine.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)