From 4f915988a6c43fe14697b5ff68ca1109351f9bad Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 12 Sep 2023 11:38:40 -0400 Subject: [PATCH] builder: don't use setParamIfDefined on bools The `setDeviceParamIfDefined' function only works on strings, and checks that the value is not empty before adding the parameter to the request. In the builder code, in several places, there are some instances of booleans being converted to strings using `strconv.FormatBool', and then calling `setDeviceParamIfDefined' with this value, meaning that the value always gets set in the request. This commit removes those instances in favour of a manual boolean check, which can result in adding the parameter to the request only if it is set to true. --- builder/proxmox/common/step_start_vm.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/builder/proxmox/common/step_start_vm.go b/builder/proxmox/common/step_start_vm.go index de5a2daa..267f40b6 100644 --- a/builder/proxmox/common/step_start_vm.go +++ b/builder/proxmox/common/step_start_vm.go @@ -261,7 +261,10 @@ func generateProxmoxNetworkAdapters(nics []NICConfig) proxmox.QemuDevices { setDeviceParamIfDefined(devs[idx], "macaddr", nics[idx].MACAddress) setDeviceParamIfDefined(devs[idx], "bridge", nics[idx].Bridge) setDeviceParamIfDefined(devs[idx], "tag", nics[idx].VLANTag) - setDeviceParamIfDefined(devs[idx], "firewall", strconv.FormatBool(nics[idx].Firewall)) + + if nics[idx].Firewall { + devs[idx]["firewall"] = nics[idx].Firewall + } if nics[idx].MTU > 0 { devs[idx]["mtu"] = nics[idx].MTU @@ -281,16 +284,18 @@ func generateProxmoxDisks(disks []diskConfig) proxmox.QemuDevices { setDeviceParamIfDefined(devs[idx], "storage", disks[idx].StoragePool) setDeviceParamIfDefined(devs[idx], "cache", disks[idx].CacheMode) setDeviceParamIfDefined(devs[idx], "format", disks[idx].DiskFormat) - if devs[idx]["type"] == "scsi" || devs[idx]["type"] == "virtio" { - setDeviceParamIfDefined(devs[idx], "iothread", strconv.FormatBool(disks[idx].IOThread)) + + if devs[idx]["type"] == "scsi" || devs[idx]["type"] == "virtio" && + disks[idx].IOThread { + devs[idx]["iothread"] = "true" } - // Proxmox API expects the value of discard to be either "on" or "ignore" - setDeviceParamIfDefined(devs[idx], "discard", func() string { - if disks[idx].Discard { - return "on" - } - return "ignore" - }()) + + if disks[idx].Discard { + devs[idx]["discard"] = "on" + } else { + devs[idx]["discard"] = "ignore" + } + // Add SSD flag only if true if disks[idx].SSD { devs[idx]["ssd"] = "1"