From 0b6d93c438e9e723b11b9586ede6e6b0a4defb81 Mon Sep 17 00:00:00 2001 From: OidaTiftla Date: Sun, 11 Aug 2024 04:30:09 +0200 Subject: [PATCH 1/2] feat: implement ability to clone Qemu VM to different storage Allows cloning a Qemu VM to a different storage location without changing the original configuration. This is already documented in the README.md but was not implemented yet. --- proxmox/config_qemu.go | 10 +++++++--- test/api/Qemu/qemu_clone_test.go | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index 4816a1b7..60ec8dff 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -40,6 +40,7 @@ type ConfigQemu struct { Description *string `json:"description,omitempty"` Disks *QemuStorages `json:"disks,omitempty"` EFIDisk QemuDevice `json:"efidisk,omitempty"` // TODO should be a struct + Storage string `json:"storage,omitempty"` // for clone FullClone *int `json:"fullclone,omitempty"` // TODO should probably be a bool HaGroup string `json:"hagroup,omitempty"` HaState string `json:"hastate,omitempty"` // TODO should be custom type with enum @@ -849,13 +850,16 @@ storage:xxx */ func (config ConfigQemu) CloneVm(sourceVmr *VmRef, vmr *VmRef, client *Client) (err error) { vmr.SetVmType("qemu") - var storage string + storage := config.Storage fullClone := "1" if config.FullClone != nil { fullClone = strconv.Itoa(*config.FullClone) } - if disk0Storage, ok := config.QemuDisks[0]["storage"].(string); ok && len(disk0Storage) > 0 { - storage = disk0Storage + // if storage is not set, use the storage of the first disk + if storage == "" && len(config.QemuDisks) > 0 { + if disk0Storage, ok := config.QemuDisks[0]["storage"].(string); ok && len(disk0Storage) > 0 { + storage = disk0Storage + } } params := map[string]interface{}{ "newid": vmr.vmId, diff --git a/test/api/Qemu/qemu_clone_test.go b/test/api/Qemu/qemu_clone_test.go index 5088de8d..655eca26 100644 --- a/test/api/Qemu/qemu_clone_test.go +++ b/test/api/Qemu/qemu_clone_test.go @@ -35,6 +35,27 @@ func Test_Clone_Qemu_VM(t *testing.T) { } +func Test_Clone_Qemu_VM_To_Different_Storage(t *testing.T) { + Test := api_test.Test{} + _ = Test.CreateTest() + config := _create_vm_spec(false) + + config.Create(_create_vmref(), Test.GetClient()) + + cloneConfig := _create_vm_spec(false) + + fullClone := 1 + + cloneConfig.Name = "test-qemu02" + cloneConfig.FullClone = &fullClone + cloneConfig.Storage = "other-storage" + + err := cloneConfig.CloneVm(_create_vmref(), _create_clone_vmref(), Test.GetClient()) + + require.NoError(t, err) + +} + func Test_Qemu_VM_Is_Cloned(t *testing.T) { Test := api_test.Test{} _ = Test.CreateTest() From 4a3279d403de2563d19b710a78c9921feb02de9a Mon Sep 17 00:00:00 2001 From: OidaTiftla Date: Mon, 19 Aug 2024 21:54:35 +0000 Subject: [PATCH 2/2] docs: integrate feedback from @Tinyblargon Co-authored-by: Okean <76069640+Tinyblargon@users.noreply.github.com> --- proxmox/config_qemu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxmox/config_qemu.go b/proxmox/config_qemu.go index 60ec8dff..8f7db098 100644 --- a/proxmox/config_qemu.go +++ b/proxmox/config_qemu.go @@ -40,7 +40,7 @@ type ConfigQemu struct { Description *string `json:"description,omitempty"` Disks *QemuStorages `json:"disks,omitempty"` EFIDisk QemuDevice `json:"efidisk,omitempty"` // TODO should be a struct - Storage string `json:"storage,omitempty"` // for clone + Storage string `json:"storage,omitempty"` // this value is only used when doing a full clone and is never returned FullClone *int `json:"fullclone,omitempty"` // TODO should probably be a bool HaGroup string `json:"hagroup,omitempty"` HaState string `json:"hastate,omitempty"` // TODO should be custom type with enum