Skip to content

Commit

Permalink
fix: linter error in ambush
Browse files Browse the repository at this point in the history
  * This commit fixes a linter error that somehow doesn't manifest
    unless some other, unrelated changes trigger it (see bpg#501 and
    bpg#505).

  * In addition it fixes a similar issue that had so far gone undetected
    by the linter.

  * Refactored the code in question into a function, since it was mostly
    duplicated.

  * Simplified a pair of conditionals that had the same code in both
    branches.
  • Loading branch information
tseeker committed Aug 18, 2023
1 parent e02c52b commit 37286f6
Showing 1 changed file with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions proxmoxtf/resource/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3433,6 +3433,27 @@ func vmRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Dia
return vmReadCustom(ctx, d, m, vmID, vmConfig, vmStatus)
}

// orderedListFromMap generates a list from a map's values. The values are sorted based on the map's keys.
func orderedListFromMap(inputMap map[string]interface{}) []interface{} {
itemCount := len(inputMap)
keyList := make([]string, itemCount)
i := 0

for key := range inputMap {
keyList[i] = key
i++
}

sort.Strings(keyList)

orderedList := make([]interface{}, itemCount)
for i, k := range keyList {
orderedList[i] = inputMap[k]
}

return orderedList
}

func vmReadCustom(
ctx context.Context,
d *schema.ResourceData,
Expand Down Expand Up @@ -3705,7 +3726,6 @@ func vmReadCustom(
currentDiskList := d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{})
diskMap := map[string]interface{}{}
diskObjects := getDiskInfo(vmConfig, d)
var orderedDiskList []interface{}

for di, dd := range diskObjects {
disk := map[string]interface{}{}
Expand Down Expand Up @@ -3807,24 +3827,8 @@ func vmReadCustom(
diskMap[di] = disk
}

var keyList []string

for key := range diskMap {
keyList = append(keyList, key)
}

sort.Strings(keyList)

for _, k := range keyList {
orderedDiskList = append(orderedDiskList, diskMap[k])
}

if len(clone) > 0 {
if len(currentDiskList) > 0 {
err := d.Set(mkResourceVirtualEnvironmentVMDisk, orderedDiskList)
diags = append(diags, diag.FromErr(err)...)
}
} else if len(currentDiskList) > 0 {
if len(currentDiskList) > 0 {
orderedDiskList := orderedListFromMap(diskMap)
err := d.Set(mkResourceVirtualEnvironmentVMDisk, orderedDiskList)
diags = append(diags, diag.FromErr(err)...)
}
Expand Down Expand Up @@ -3887,7 +3891,6 @@ func vmReadCustom(

currentPCIList := d.Get(mkResourceVirtualEnvironmentVMHostPCI).([]interface{})
pciMap := map[string]interface{}{}
var orderedPCIList []interface{}

pciDevices := getPCIInfo(vmConfig, d)
for pi, pp := range pciDevices {
Expand Down Expand Up @@ -3933,23 +3936,9 @@ func vmReadCustom(
pciMap[pi] = pci
}

keyList = []string{}
for key := range pciMap {
keyList = append(keyList, key)
}
sort.Strings(keyList)

for _, k := range keyList {
orderedPCIList = append(orderedPCIList, pciMap[k])
}

if len(clone) > 0 {
if len(currentPCIList) > 0 {
err := d.Set(mkResourceVirtualEnvironmentVMHostPCI, orderedPCIList)
diags = append(diags, diag.FromErr(err)...)
}
} else if len(currentPCIList) > 0 {
if len(currentPCIList) > 0 {
// todo: reordering of devices by PVE may cause an issue here
orderedPCIList := orderedListFromMap(pciMap)
err := d.Set(mkResourceVirtualEnvironmentVMHostPCI, orderedPCIList)
diags = append(diags, diag.FromErr(err)...)
}
Expand Down

0 comments on commit 37286f6

Please sign in to comment.