From de3935d462cd074ae8f1bfa2ead655efec8256b7 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Tue, 6 Jun 2023 07:45:03 -0400 Subject: [PATCH] fix(vm): fix incorrect disk interface ref when reading VM info from PVE (#365) Fix a minor bug in `vmGetDiskDeviceObjects(...)` that was discovered during investigation of #360. --- proxmoxtf/resource/utils.go | 5 +++-- proxmoxtf/resource/vm.go | 16 ++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/proxmoxtf/resource/utils.go b/proxmoxtf/resource/utils.go index 8cb97308..bbb997f2 100644 --- a/proxmoxtf/resource/utils.go +++ b/proxmoxtf/resource/utils.go @@ -516,8 +516,9 @@ func getDiskInfo(resp *vms.GetResponseData, d *schema.ResourceData) map[string]* v.FileID = &fileID } } - - v.Interface = &k + // defensive copy of the loop variable + iface := k + v.Interface = &iface } } diff --git a/proxmoxtf/resource/vm.go b/proxmoxtf/resource/vm.go index c7731f03..5f64d6b5 100644 --- a/proxmoxtf/resource/vm.go +++ b/proxmoxtf/resource/vm.go @@ -1679,9 +1679,9 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d return diag.FromErr(e) } - allDiskInfo := getDiskInfo(vmConfig, d) + allDiskInfo := getDiskInfo(vmConfig, d) // from the cloned VM - diskDeviceObjects, e := vmGetDiskDeviceObjects(d, nil) + diskDeviceObjects, e := vmGetDiskDeviceObjects(d, nil) // from the resource config if e != nil { return diag.FromErr(e) } @@ -1691,29 +1691,33 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d diskInterface := diskBlock[mkResourceVirtualEnvironmentVMDiskInterface].(string) dataStoreID := diskBlock[mkResourceVirtualEnvironmentVMDiskDatastoreID].(string) diskSize := diskBlock[mkResourceVirtualEnvironmentVMDiskSize].(int) + prefix := diskDigitPrefix(diskInterface) currentDiskInfo := allDiskInfo[diskInterface] + configuredDiskInfo := diskDeviceObjects[prefix][diskInterface] if currentDiskInfo == nil { diskUpdateBody := &vms.UpdateRequestBody{} - prefix := diskDigitPrefix(diskInterface) switch prefix { case "virtio": if diskUpdateBody.VirtualIODevices == nil { diskUpdateBody.VirtualIODevices = vms.CustomStorageDevices{} } - diskUpdateBody.VirtualIODevices[diskInterface] = diskDeviceObjects[prefix][diskInterface] + + diskUpdateBody.VirtualIODevices[diskInterface] = configuredDiskInfo case "sata": if diskUpdateBody.SATADevices == nil { diskUpdateBody.SATADevices = vms.CustomStorageDevices{} } - diskUpdateBody.SATADevices[diskInterface] = diskDeviceObjects[prefix][diskInterface] + + diskUpdateBody.SATADevices[diskInterface] = configuredDiskInfo case "scsi": if diskUpdateBody.SCSIDevices == nil { diskUpdateBody.SCSIDevices = vms.CustomStorageDevices{} } - diskUpdateBody.SCSIDevices[diskInterface] = diskDeviceObjects[prefix][diskInterface] + + diskUpdateBody.SCSIDevices[diskInterface] = configuredDiskInfo } e = vmAPI.UpdateVM(ctx, diskUpdateBody)