Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement ability to clone Qemu VM to different storage #352

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions proxmox/config_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"` // 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
Expand Down Expand Up @@ -849,13 +850,16 @@ storage:xxx
*/
func (config ConfigQemu) CloneVm(sourceVmr *VmRef, vmr *VmRef, client *Client) (err error) {
OidaTiftla marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand Down
21 changes: 21 additions & 0 deletions test/api/Qemu/qemu_clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ func Test_Clone_Qemu_VM(t *testing.T) {

}

func Test_Clone_Qemu_VM_To_Different_Storage(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently none of the API test are really functional but this tells us what situation we should have coverage for.

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()
Expand Down
Loading