Skip to content

Commit

Permalink
fix(vm): fix incorrect disk interface ref when reading VM info from P…
Browse files Browse the repository at this point in the history
…VE (#365)

Fix a minor bug in `vmGetDiskDeviceObjects(...)` that was discovered during investigation of #360.
  • Loading branch information
bpg authored Jun 6, 2023
1 parent a546a82 commit de3935d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 3 additions & 2 deletions proxmoxtf/resource/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
16 changes: 10 additions & 6 deletions proxmoxtf/resource/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
Expand Down

0 comments on commit de3935d

Please sign in to comment.