Skip to content

Commit

Permalink
builder: don't use setParamIfDefined on bools
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lbajolet-hashicorp committed Sep 12, 2023
1 parent 0209937 commit 4f91598
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions builder/proxmox/common/step_start_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down

0 comments on commit 4f91598

Please sign in to comment.