From 3d199b3dc4ab39ef68826d4e1bfc31ded8180aa3 Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Mon, 24 Jun 2024 08:35:36 +0200 Subject: [PATCH] fix: `Description` can't be cleared --- proxmox/config_qemu.go | 13 +++++++++---- proxmox/config_qemu_test.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index 9dc8943d..8160e0ab 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -37,7 +37,7 @@ type ConfigQemu struct { Boot string `json:"boot,omitempty"` // TODO should be an array of custom enums BootDisk string `json:"bootdisk,omitempty"` // TODO discuss deprecation? Only returned as it's deprecated in the proxmox api CloudInit *CloudInit `json:"cloudinit,omitempty"` - Description string `json:"description,omitempty"` + Description *string `json:"description,omitempty"` Disks *QemuStorages `json:"disks,omitempty"` EFIDisk QemuDevice `json:"efidisk,omitempty"` // TODO should be a struct FullClone *int `json:"fullclone,omitempty"` // TODO should probably be a bool @@ -189,8 +189,12 @@ func (config ConfigQemu) mapToAPI(currentConfig ConfigQemu, version Version) (re if config.QemuCpu != "" { params["cpu"] = config.QemuCpu } - if config.Description != "" { - params["description"] = config.Description + if config.Description != nil { + if *config.Description != "" { + params["description"] = *config.Description + } else if currentConfig.Description != nil { + itemsToDelete += ",description" + } } if config.Hookscript != "" { params["hookscript"] = config.Hookscript @@ -356,7 +360,8 @@ func (ConfigQemu) mapToStruct(vmr *VmRef, params map[string]interface{}) (*Confi config.Bios = params["bios"].(string) } if _, isSet := params["description"]; isSet { - config.Description = strings.TrimSpace(params["description"].(string)) + tmp := params["description"].(string) + config.Description = &tmp } //Can be network,disk,cpu,memory,usb if _, isSet := params["hotplug"]; isSet { diff --git a/proxmox/config_qemu_test.go b/proxmox/config_qemu_test.go index d8f53323..983c09b4 100644 --- a/proxmox/config_qemu_test.go +++ b/proxmox/config_qemu_test.go @@ -616,6 +616,21 @@ func Test_ConfigQemu_mapToAPI(t *testing.T) { config: &ConfigQemu{CloudInit: &CloudInit{Username: util.Pointer("")}}, currentConfig: ConfigQemu{CloudInit: &CloudInit{Username: util.Pointer("admin")}}, output: map[string]interface{}{"delete": "ciuser"}}}}, + {category: `Description`, + create: []test{ + {name: `Description empty`, + config: &ConfigQemu{Description: util.Pointer("")}, + output: map[string]interface{}{}}}, + createUpdate: []test{ + {name: `Description set`, + config: &ConfigQemu{Description: util.Pointer("test description")}, + currentConfig: ConfigQemu{Description: util.Pointer("old description")}, + output: map[string]interface{}{"description": "test description"}}}, + update: []test{ + {name: `Description empty`, + config: &ConfigQemu{Description: util.Pointer("")}, + currentConfig: ConfigQemu{Description: util.Pointer("old description")}, + output: map[string]interface{}{"delete": "description"}}}}, {category: `Disks.Ide`, update: []test{ {name: `Disk.Ide.Disk_X DELETE`, @@ -3388,6 +3403,10 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { {name: `Username empty`, input: map[string]interface{}{"ciuser": string(" ")}, output: &ConfigQemu{}}}}, + {category: `Description`, + tests: []test{ + {input: map[string]interface{}{"description": string("test description")}, + output: &ConfigQemu{Description: util.Pointer("test description")}}}}, {category: `Disks Ide CdRom`, tests: []test{ {name: `none`,