mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-02 03:22:59 +00:00
Add multidisk support
This commit is contained in:
parent
7883dfdb7e
commit
3956a31494
@ -236,6 +236,7 @@ func (c *VirtualEnvironmentClient) RebootVMAsync(nodeName string, vmID int, d *V
|
|||||||
// ResizeVMDisk resizes a virtual machine disk.
|
// ResizeVMDisk resizes a virtual machine disk.
|
||||||
func (c *VirtualEnvironmentClient) ResizeVMDisk(nodeName string, vmID int, d *VirtualEnvironmentVMResizeDiskRequestBody) error {
|
func (c *VirtualEnvironmentClient) ResizeVMDisk(nodeName string, vmID int, d *VirtualEnvironmentVMResizeDiskRequestBody) error {
|
||||||
var err error
|
var err error
|
||||||
|
log.Printf("[DEBUG] RESIZE size: %s, disk: %s", d.Size, d.Disk)
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
err = c.DoRequest(hmPUT, fmt.Sprintf("nodes/%s/qemu/%d/resize", url.PathEscape(nodeName), vmID), d, nil)
|
err = c.DoRequest(hmPUT, fmt.Sprintf("nodes/%s/qemu/%d/resize", url.PathEscape(nodeName), vmID), d, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -165,11 +165,12 @@ type CustomStorageDevice struct {
|
|||||||
Format *string `json:"format,omitempty" url:"format,omitempty"`
|
Format *string `json:"format,omitempty" url:"format,omitempty"`
|
||||||
Interface *string
|
Interface *string
|
||||||
ID *string
|
ID *string
|
||||||
FileId *string
|
FileID *string
|
||||||
|
SizeInt *int
|
||||||
}
|
}
|
||||||
|
|
||||||
// CustomStorageDevices handles QEMU SATA device parameters.
|
// CustomStorageDevices handles QEMU SATA device parameters.
|
||||||
type CustomStorageDevices []CustomStorageDevice
|
type CustomStorageDevices map[string]CustomStorageDevice
|
||||||
|
|
||||||
// CustomUSBDevice handles QEMU USB device parameters.
|
// CustomUSBDevice handles QEMU USB device parameters.
|
||||||
type CustomUSBDevice struct {
|
type CustomUSBDevice struct {
|
||||||
@ -248,7 +249,7 @@ type VirtualEnvironmentVMCreateRequestBody struct {
|
|||||||
HookScript *string `json:"hookscript,omitempty" url:"hookscript,omitempty"`
|
HookScript *string `json:"hookscript,omitempty" url:"hookscript,omitempty"`
|
||||||
Hotplug CustomCommaSeparatedList `json:"hotplug,omitempty" url:"hotplug,omitempty,comma"`
|
Hotplug CustomCommaSeparatedList `json:"hotplug,omitempty" url:"hotplug,omitempty,comma"`
|
||||||
Hugepages *string `json:"hugepages,omitempty" url:"hugepages,omitempty"`
|
Hugepages *string `json:"hugepages,omitempty" url:"hugepages,omitempty"`
|
||||||
IDEDevices CustomStorageDevices `json:"ide,omitempty" url:"ide,omitempty"`
|
IDEDevices CustomStorageDevices `json:"ide,omitempty" url:",omitempty"`
|
||||||
KeyboardLayout *string `json:"keyboard,omitempty" url:"keyboard,omitempty"`
|
KeyboardLayout *string `json:"keyboard,omitempty" url:"keyboard,omitempty"`
|
||||||
KVMArguments CustomLineBreakSeparatedList `json:"args,omitempty" url:"args,omitempty,space"`
|
KVMArguments CustomLineBreakSeparatedList `json:"args,omitempty" url:"args,omitempty,space"`
|
||||||
KVMEnabled *CustomBool `json:"kvm,omitempty" url:"kvm,omitempty,int"`
|
KVMEnabled *CustomBool `json:"kvm,omitempty" url:"kvm,omitempty,int"`
|
||||||
@ -1055,7 +1056,7 @@ func (r CustomStorageDevice) EncodeValues(key string, v *url.Values) error {
|
|||||||
func (r CustomStorageDevices) EncodeValues(key string, v *url.Values) error {
|
func (r CustomStorageDevices) EncodeValues(key string, v *url.Values) error {
|
||||||
for i, d := range r {
|
for i, d := range r {
|
||||||
if d.Enabled {
|
if d.Enabled {
|
||||||
d.EncodeValues(fmt.Sprintf("%s%d", key, i), v)
|
d.EncodeValues(i, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ package proxmoxtf
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -499,7 +499,7 @@ func resourceVirtualEnvironmentVM() *schema.Resource {
|
|||||||
Description: "The disk size in gigabytes",
|
Description: "The disk size in gigabytes",
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Default: dvResourceVirtualEnvironmentVMDiskSize,
|
Default: dvResourceVirtualEnvironmentVMDiskSize,
|
||||||
ValidateFunc: validation.IntBetween(1, 8192),
|
ValidateFunc: validation.IntAtLeast(1),
|
||||||
},
|
},
|
||||||
mkResourceVirtualEnvironmentVMDiskSpeed: {
|
mkResourceVirtualEnvironmentVMDiskSpeed: {
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeList,
|
||||||
@ -1132,16 +1132,16 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
|
|||||||
|
|
||||||
if len(cdrom) > 0 || len(initialization) > 0 {
|
if len(cdrom) > 0 || len(initialization) > 0 {
|
||||||
ideDevices = proxmox.CustomStorageDevices{
|
ideDevices = proxmox.CustomStorageDevices{
|
||||||
proxmox.CustomStorageDevice{
|
"ide0": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide1": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide2": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide3": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1159,13 +1159,13 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
|
|||||||
|
|
||||||
cdromMedia := "cdrom"
|
cdromMedia := "cdrom"
|
||||||
|
|
||||||
cdromDevice := proxmox.CustomStorageDevice{
|
updateBody.IDEDevices = proxmox.CustomStorageDevices{
|
||||||
Enabled: cdromEnabled,
|
"ide3": proxmox.CustomStorageDevice{
|
||||||
FileVolume: cdromFileID,
|
Enabled: cdromEnabled,
|
||||||
Media: &cdromMedia,
|
FileVolume: cdromFileID,
|
||||||
|
Media: &cdromMedia,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ideDevices[3] = cdromDevice
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cpu) > 0 {
|
if len(cpu) > 0 {
|
||||||
@ -1211,13 +1211,14 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
|
|||||||
cdromCloudInitFileID := fmt.Sprintf("%s:cloudinit", initializationDatastoreID)
|
cdromCloudInitFileID := fmt.Sprintf("%s:cloudinit", initializationDatastoreID)
|
||||||
cdromCloudInitMedia := "cdrom"
|
cdromCloudInitMedia := "cdrom"
|
||||||
|
|
||||||
cdromCloudInitDevice := proxmox.CustomStorageDevice{
|
updateBody.IDEDevices = proxmox.CustomStorageDevices{
|
||||||
Enabled: cdromCloudInitEnabled,
|
"ide2": proxmox.CustomStorageDevice{
|
||||||
FileVolume: cdromCloudInitFileID,
|
Enabled: cdromCloudInitEnabled,
|
||||||
Media: &cdromCloudInitMedia,
|
FileVolume: cdromCloudInitFileID,
|
||||||
|
Media: &cdromCloudInitMedia,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ideDevices[2] = cdromCloudInitDevice
|
|
||||||
initializationConfig, err := resourceVirtualEnvironmentVMGetCloudInitConfig(d, m)
|
initializationConfig, err := resourceVirtualEnvironmentVMGetCloudInitConfig(d, m)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -1336,6 +1337,12 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
|
|||||||
|
|
||||||
allDiskInfo := getDiskInfo(vmConfig)
|
allDiskInfo := getDiskInfo(vmConfig)
|
||||||
|
|
||||||
|
diskDeviceObjects, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
for i := range disk {
|
for i := range disk {
|
||||||
|
|
||||||
diskBlock := disk[i].(map[string]interface{})
|
diskBlock := disk[i].(map[string]interface{})
|
||||||
@ -1346,7 +1353,32 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
|
|||||||
currentDiskInfo := allDiskInfo[diskInterface]
|
currentDiskInfo := allDiskInfo[diskInterface]
|
||||||
|
|
||||||
if currentDiskInfo == nil {
|
if currentDiskInfo == nil {
|
||||||
return errors.New(fmt.Sprintf("Disk move failed, no disk named %s", diskInterface))
|
diskUpdateBody := &proxmox.VirtualEnvironmentVMUpdateRequestBody{}
|
||||||
|
prefix := diskDigitPrefix(diskInterface)
|
||||||
|
switch prefix {
|
||||||
|
case "virtio":
|
||||||
|
if diskUpdateBody.VirtualIODevices == nil {
|
||||||
|
diskUpdateBody.VirtualIODevices = make(proxmox.CustomStorageDevices)
|
||||||
|
}
|
||||||
|
diskUpdateBody.VirtualIODevices[diskInterface] = diskDeviceObjects[prefix][diskInterface]
|
||||||
|
case "sata":
|
||||||
|
if diskUpdateBody.SATADevices == nil {
|
||||||
|
diskUpdateBody.SATADevices = make(proxmox.CustomStorageDevices)
|
||||||
|
}
|
||||||
|
diskUpdateBody.SATADevices[diskInterface] = diskDeviceObjects[prefix][diskInterface]
|
||||||
|
case "scsi":
|
||||||
|
if diskUpdateBody.SCSIDevices == nil {
|
||||||
|
diskUpdateBody.SCSIDevices = make(proxmox.CustomStorageDevices)
|
||||||
|
}
|
||||||
|
diskUpdateBody.SCSIDevices[diskInterface] = diskDeviceObjects[prefix][diskInterface]
|
||||||
|
}
|
||||||
|
|
||||||
|
err = veClient.UpdateVM(nodeName, vmID, diskUpdateBody)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
compareNumber, err := parseDiskSize(currentDiskInfo.Size)
|
compareNumber, err := parseDiskSize(currentDiskInfo.Size)
|
||||||
@ -1356,7 +1388,7 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if diskSize < compareNumber {
|
if diskSize < compareNumber {
|
||||||
return errors.New(fmt.Sprintf("Disk resize fails requests size (%dG) is lower than current size (%s)", diskSize, *currentDiskInfo.Size))
|
return fmt.Errorf("Disk resize fails requests size (%dG) is lower than current size (%s)", diskSize, *currentDiskInfo.Size)
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteOriginalDisk := proxmox.CustomBool(true)
|
deleteOriginalDisk := proxmox.CustomBool(true)
|
||||||
@ -1450,16 +1482,16 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac
|
|||||||
cpuUnits := cpuBlock[mkResourceVirtualEnvironmentVMCPUUnits].(int)
|
cpuUnits := cpuBlock[mkResourceVirtualEnvironmentVMCPUUnits].(int)
|
||||||
|
|
||||||
description := d.Get(mkResourceVirtualEnvironmentVMDescription).(string)
|
description := d.Get(mkResourceVirtualEnvironmentVMDescription).(string)
|
||||||
diskDeviceObjects, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m)
|
diskDeviceObjects, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
virtioDeviceObjects := getOrderedDiskDeviceList(diskDeviceObjects, "virtio")
|
virtioDeviceObjects := diskDeviceObjects["vitio"]
|
||||||
scsiDeviceObjects := getOrderedDiskDeviceList(diskDeviceObjects, "scsi")
|
scsiDeviceObjects := diskDeviceObjects["scsi"]
|
||||||
//ideDeviceObjects := getOrderedDiskDeviceList(diskDeviceObjects, "ide")
|
//ideDeviceObjects := getOrderedDiskDeviceList(diskDeviceObjects, "ide")
|
||||||
sataDeviceObjects := getOrderedDiskDeviceList(diskDeviceObjects, "sata")
|
sataDeviceObjects := diskDeviceObjects["sata"]
|
||||||
|
|
||||||
initializationConfig, err := resourceVirtualEnvironmentVMGetCloudInitConfig(d, m)
|
initializationConfig, err := resourceVirtualEnvironmentVMGetCloudInitConfig(d, m)
|
||||||
|
|
||||||
@ -1552,18 +1584,12 @@ func resourceVirtualEnvironmentVMCreateCustom(d *schema.ResourceData, m interfac
|
|||||||
|
|
||||||
ideDevice2Media := "cdrom"
|
ideDevice2Media := "cdrom"
|
||||||
ideDevices := proxmox.CustomStorageDevices{
|
ideDevices := proxmox.CustomStorageDevices{
|
||||||
proxmox.CustomStorageDevice{
|
"ide1": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
|
||||||
},
|
|
||||||
proxmox.CustomStorageDevice{
|
|
||||||
Enabled: false,
|
|
||||||
},
|
|
||||||
proxmox.CustomStorageDevice{
|
|
||||||
Enabled: cdromCloudInitEnabled,
|
Enabled: cdromCloudInitEnabled,
|
||||||
FileVolume: cdromCloudInitFileID,
|
FileVolume: cdromCloudInitFileID,
|
||||||
Media: &ideDevice2Media,
|
Media: &ideDevice2Media,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide2": proxmox.CustomStorageDevice{
|
||||||
Enabled: cdromEnabled,
|
Enabled: cdromEnabled,
|
||||||
FileVolume: cdromFileID,
|
FileVolume: cdromFileID,
|
||||||
Media: &ideDevice2Media,
|
Media: &ideDevice2Media,
|
||||||
@ -1993,8 +2019,13 @@ func resourceVirtualEnvironmentVMGetCPUArchitectureValidator() schema.SchemaVali
|
|||||||
}, false)
|
}, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, m interface{}) (map[string]map[string]proxmox.CustomStorageDevice, error) {
|
func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, m interface{}, disks []interface{}) (map[string]map[string]proxmox.CustomStorageDevice, error) {
|
||||||
diskDevice := d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{})
|
var diskDevice []interface{}
|
||||||
|
if disks != nil {
|
||||||
|
diskDevice = disks
|
||||||
|
} else {
|
||||||
|
diskDevice = d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{})
|
||||||
|
}
|
||||||
diskDeviceObjects := make(map[string]map[string]proxmox.CustomStorageDevice)
|
diskDeviceObjects := make(map[string]map[string]proxmox.CustomStorageDevice)
|
||||||
resource := resourceVirtualEnvironmentVM()
|
resource := resourceVirtualEnvironmentVM()
|
||||||
|
|
||||||
@ -2021,6 +2052,13 @@ func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, m
|
|||||||
diskDevice.FileVolume = fmt.Sprintf("%s:%d", datastoreID, size)
|
diskDevice.FileVolume = fmt.Sprintf("%s:%d", datastoreID, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diskDevice.ID = &datastoreID
|
||||||
|
diskDevice.Interface = &diskInterface
|
||||||
|
diskDevice.FileID = &fileID
|
||||||
|
sizeString := fmt.Sprintf("%dG", size)
|
||||||
|
diskDevice.Size = &sizeString
|
||||||
|
diskDevice.SizeInt = &size
|
||||||
|
|
||||||
if len(speedBlock) > 0 {
|
if len(speedBlock) > 0 {
|
||||||
speedLimitRead := speedBlock[mkResourceVirtualEnvironmentVMDiskSpeedRead].(int)
|
speedLimitRead := speedBlock[mkResourceVirtualEnvironmentVMDiskSpeedRead].(int)
|
||||||
speedLimitReadBurstable := speedBlock[mkResourceVirtualEnvironmentVMDiskSpeedReadBurstable].(int)
|
speedLimitReadBurstable := speedBlock[mkResourceVirtualEnvironmentVMDiskSpeedReadBurstable].(int)
|
||||||
@ -2443,31 +2481,10 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
|
|||||||
d.Set(mkResourceVirtualEnvironmentVMCPU, []interface{}{cpu})
|
d.Set(mkResourceVirtualEnvironmentVMCPU, []interface{}{cpu})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare the disks to those stored in the state.
|
diskMap := map[string]interface{}{}
|
||||||
currentDisks := d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{})
|
orderedDiskList := []interface{}{}
|
||||||
|
|
||||||
diskList := []interface{}{}
|
|
||||||
diskObjects := getDiskInfo(vmConfig)
|
diskObjects := getDiskInfo(vmConfig)
|
||||||
|
|
||||||
currentDiskMap := make(map[string]*proxmox.CustomStorageDevice)
|
|
||||||
|
|
||||||
for _, dd := range currentDisks {
|
|
||||||
var disk proxmox.CustomStorageDevice
|
|
||||||
currentDiskEntry := dd.(map[string]interface{})
|
|
||||||
|
|
||||||
id := currentDiskEntry[mkResourceVirtualEnvironmentVMDiskDatastoreID].(string)
|
|
||||||
diskInterface := currentDiskEntry[mkResourcevirtualEnvironmentVMDiskInterface].(string)
|
|
||||||
format := currentDiskEntry[mkResourceVirtualEnvironmentVMDiskFileFormat].(string)
|
|
||||||
fileId := currentDiskEntry[mkResourceVirtualEnvironmentVMDiskFileID].(string)
|
|
||||||
|
|
||||||
disk.Interface = &diskInterface
|
|
||||||
disk.ID = &id
|
|
||||||
disk.Format = &format
|
|
||||||
disk.FileId = &fileId
|
|
||||||
|
|
||||||
currentDiskMap[diskInterface] = &disk
|
|
||||||
}
|
|
||||||
|
|
||||||
for di, dd := range diskObjects {
|
for di, dd := range diskObjects {
|
||||||
disk := map[string]interface{}{}
|
disk := map[string]interface{}{}
|
||||||
|
|
||||||
@ -2479,14 +2496,13 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
|
|||||||
|
|
||||||
disk[mkResourceVirtualEnvironmentVMDiskDatastoreID] = fileIDParts[0]
|
disk[mkResourceVirtualEnvironmentVMDiskDatastoreID] = fileIDParts[0]
|
||||||
|
|
||||||
if val, ok := currentDiskMap[di]; ok {
|
disk[mkResourceVirtualEnvironmentVMDiskFileID] = dd.FileID
|
||||||
if *val.FileId != "" {
|
if dd.Format == nil {
|
||||||
disk[mkResourceVirtualEnvironmentVMDiskFileID] = val.FileId
|
disk[mkResourceVirtualEnvironmentVMDiskFileFormat] = "qcow2"
|
||||||
}
|
} else {
|
||||||
|
disk[mkResourceVirtualEnvironmentVMDiskFileFormat] = dd.Format
|
||||||
disk[mkResourceVirtualEnvironmentVMDiskFileFormat] = val.Format
|
|
||||||
disk[mkResourcevirtualEnvironmentVMDiskInterface] = val.Interface
|
|
||||||
}
|
}
|
||||||
|
disk[mkResourcevirtualEnvironmentVMDiskInterface] = di
|
||||||
|
|
||||||
diskSize := 0
|
diskSize := 0
|
||||||
|
|
||||||
@ -2534,16 +2550,23 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
|
|||||||
disk[mkResourceVirtualEnvironmentVMDiskSpeed] = []interface{}{}
|
disk[mkResourceVirtualEnvironmentVMDiskSpeed] = []interface{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
diskList = append(diskList, disk)
|
diskMap[di] = disk
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] NUMBER CURRENT DISKS %d NUMBER READ DISKS %d", len(currentDisks), len(diskList))
|
keyList := []string{}
|
||||||
|
for key := range diskMap {
|
||||||
|
keyList = append(keyList, key)
|
||||||
|
}
|
||||||
|
sort.Strings(keyList)
|
||||||
|
for _, k := range keyList {
|
||||||
|
orderedDiskList = append(orderedDiskList, diskMap[k])
|
||||||
|
}
|
||||||
if len(clone) > 0 {
|
if len(clone) > 0 {
|
||||||
if len(currentDisks) > 0 || len(diskList) > 0 {
|
if len(orderedDiskList) > 0 {
|
||||||
d.Set(mkResourceVirtualEnvironmentVMDisk, diskList)
|
d.Set(mkResourceVirtualEnvironmentVMDisk, orderedDiskList)
|
||||||
}
|
}
|
||||||
} else if len(currentDisks) > 0 || len(diskList) > 0 {
|
} else if len(orderedDiskList) > 0 {
|
||||||
d.Set(mkResourceVirtualEnvironmentVMDisk, diskList)
|
d.Set(mkResourceVirtualEnvironmentVMDisk, orderedDiskList)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare the initialization configuration to the one stored in the state.
|
// Compare the initialization configuration to the one stored in the state.
|
||||||
@ -3084,16 +3107,16 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
|
|
||||||
updateBody := &proxmox.VirtualEnvironmentVMUpdateRequestBody{
|
updateBody := &proxmox.VirtualEnvironmentVMUpdateRequestBody{
|
||||||
IDEDevices: proxmox.CustomStorageDevices{
|
IDEDevices: proxmox.CustomStorageDevices{
|
||||||
proxmox.CustomStorageDevice{
|
"ide0": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide1": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide2": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
proxmox.CustomStorageDevice{
|
"ide3": proxmox.CustomStorageDevice{
|
||||||
Enabled: false,
|
Enabled: false,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -3212,7 +3235,7 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
|
|
||||||
cdromMedia := "cdrom"
|
cdromMedia := "cdrom"
|
||||||
|
|
||||||
updateBody.IDEDevices[3] = proxmox.CustomStorageDevice{
|
updateBody.IDEDevices["ide3"] = proxmox.CustomStorageDevice{
|
||||||
Enabled: cdromEnabled,
|
Enabled: cdromEnabled,
|
||||||
FileVolume: cdromFileID,
|
FileVolume: cdromFileID,
|
||||||
Media: &cdromMedia,
|
Media: &cdromMedia,
|
||||||
@ -3266,7 +3289,7 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
|
|
||||||
// Prepare the new disk device configuration.
|
// Prepare the new disk device configuration.
|
||||||
if d.HasChange(mkResourceVirtualEnvironmentVMDisk) {
|
if d.HasChange(mkResourceVirtualEnvironmentVMDisk) {
|
||||||
diskDeviceObjects, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m)
|
diskDeviceObjects, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -3275,8 +3298,6 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
diskDeviceInfo := getDiskInfo(vmConfig)
|
diskDeviceInfo := getDiskInfo(vmConfig)
|
||||||
|
|
||||||
for prefix, diskMap := range diskDeviceObjects {
|
for prefix, diskMap := range diskDeviceObjects {
|
||||||
index := 0
|
|
||||||
|
|
||||||
if diskMap == nil {
|
if diskMap == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -3286,39 +3307,33 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
return fmt.Errorf("Missing %s device %s", prefix, key)
|
return fmt.Errorf("Missing %s device %s", prefix, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tmp := *diskDeviceInfo[key]
|
||||||
|
tmp.BurstableReadSpeedMbps = value.BurstableReadSpeedMbps
|
||||||
|
tmp.BurstableWriteSpeedMbps = value.BurstableWriteSpeedMbps
|
||||||
|
tmp.MaxReadSpeedMbps = value.MaxReadSpeedMbps
|
||||||
|
tmp.MaxWriteSpeedMbps = value.MaxWriteSpeedMbps
|
||||||
|
|
||||||
switch prefix {
|
switch prefix {
|
||||||
case "virtio":
|
case "virtio":
|
||||||
{
|
{
|
||||||
if updateBody.VirtualIODevices == nil {
|
if updateBody.VirtualIODevices == nil {
|
||||||
updateBody.VirtualIODevices = make(proxmox.CustomStorageDevices, len(diskMap))
|
updateBody.VirtualIODevices = make(proxmox.CustomStorageDevices)
|
||||||
}
|
}
|
||||||
updateBody.VirtualIODevices[index] = *diskDeviceInfo[key]
|
updateBody.VirtualIODevices[key] = tmp
|
||||||
updateBody.VirtualIODevices[index].BurstableReadSpeedMbps = value.BurstableReadSpeedMbps
|
|
||||||
updateBody.VirtualIODevices[index].BurstableWriteSpeedMbps = value.BurstableWriteSpeedMbps
|
|
||||||
updateBody.VirtualIODevices[index].MaxReadSpeedMbps = value.MaxReadSpeedMbps
|
|
||||||
updateBody.VirtualIODevices[index].MaxWriteSpeedMbps = value.MaxWriteSpeedMbps
|
|
||||||
}
|
}
|
||||||
case "sata":
|
case "sata":
|
||||||
{
|
{
|
||||||
if updateBody.SATADevices == nil {
|
if updateBody.SATADevices == nil {
|
||||||
updateBody.SATADevices = make(proxmox.CustomStorageDevices, len(diskMap))
|
updateBody.SATADevices = make(proxmox.CustomStorageDevices)
|
||||||
}
|
}
|
||||||
updateBody.SATADevices[index] = *diskDeviceInfo[key]
|
updateBody.SATADevices[key] = tmp
|
||||||
updateBody.SATADevices[index].BurstableReadSpeedMbps = value.BurstableReadSpeedMbps
|
|
||||||
updateBody.SATADevices[index].BurstableWriteSpeedMbps = value.BurstableWriteSpeedMbps
|
|
||||||
updateBody.SATADevices[index].MaxReadSpeedMbps = value.MaxReadSpeedMbps
|
|
||||||
updateBody.SATADevices[index].MaxWriteSpeedMbps = value.MaxWriteSpeedMbps
|
|
||||||
}
|
}
|
||||||
case "scsi":
|
case "scsi":
|
||||||
{
|
{
|
||||||
if updateBody.SCSIDevices == nil {
|
if updateBody.SCSIDevices == nil {
|
||||||
updateBody.SCSIDevices = make(proxmox.CustomStorageDevices, len(diskMap))
|
updateBody.SCSIDevices = make(proxmox.CustomStorageDevices)
|
||||||
}
|
}
|
||||||
updateBody.SCSIDevices[index] = *diskDeviceInfo[key]
|
updateBody.SCSIDevices[key] = tmp
|
||||||
updateBody.SCSIDevices[index].BurstableReadSpeedMbps = value.BurstableReadSpeedMbps
|
|
||||||
updateBody.SCSIDevices[index].BurstableWriteSpeedMbps = value.BurstableWriteSpeedMbps
|
|
||||||
updateBody.SCSIDevices[index].MaxReadSpeedMbps = value.MaxReadSpeedMbps
|
|
||||||
updateBody.SCSIDevices[index].MaxWriteSpeedMbps = value.MaxWriteSpeedMbps
|
|
||||||
}
|
}
|
||||||
case "ide":
|
case "ide":
|
||||||
{
|
{
|
||||||
@ -3327,7 +3342,6 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
default:
|
default:
|
||||||
return fmt.Errorf("Device prefix %s not supported", prefix)
|
return fmt.Errorf("Device prefix %s not supported", prefix)
|
||||||
}
|
}
|
||||||
index = index + 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3351,7 +3365,7 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
|
|
||||||
cdromMedia := "cdrom"
|
cdromMedia := "cdrom"
|
||||||
|
|
||||||
updateBody.IDEDevices[2] = proxmox.CustomStorageDevice{
|
updateBody.IDEDevices["ide2"] = proxmox.CustomStorageDevice{
|
||||||
Enabled: true,
|
Enabled: true,
|
||||||
FileVolume: fmt.Sprintf("%s:cloudinit", initializationDatastoreID),
|
FileVolume: fmt.Sprintf("%s:cloudinit", initializationDatastoreID),
|
||||||
Media: &cdromMedia,
|
Media: &cdromMedia,
|
||||||
@ -3359,7 +3373,9 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
|
|||||||
|
|
||||||
if vmConfig.IDEDevice2 != nil {
|
if vmConfig.IDEDevice2 != nil {
|
||||||
if strings.Contains(vmConfig.IDEDevice2.FileVolume, fmt.Sprintf("vm-%d-cloudinit", vmID)) {
|
if strings.Contains(vmConfig.IDEDevice2.FileVolume, fmt.Sprintf("vm-%d-cloudinit", vmID)) {
|
||||||
updateBody.IDEDevices[2].Enabled = false
|
var tmp = updateBody.IDEDevices["ide2"]
|
||||||
|
tmp.Enabled = true
|
||||||
|
updateBody.IDEDevices["ide2"] = tmp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3517,44 +3533,41 @@ func resourceVirtualEnvironmentVMUpdateDiskLocationAndSize(d *schema.ResourceDat
|
|||||||
if d.HasChange(mkResourceVirtualEnvironmentVMDisk) {
|
if d.HasChange(mkResourceVirtualEnvironmentVMDisk) {
|
||||||
diskOld, diskNew := d.GetChange(mkResourceVirtualEnvironmentVMDisk)
|
diskOld, diskNew := d.GetChange(mkResourceVirtualEnvironmentVMDisk)
|
||||||
|
|
||||||
diskOldEntries := diskOld.([]interface{})
|
diskOldEntries, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, diskOld.([]interface{}))
|
||||||
diskNewEntries := diskNew.([]interface{})
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
diskNewEntries, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, diskNew.([]interface{}))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
diskMoveBodies := []*proxmox.VirtualEnvironmentVMMoveDiskRequestBody{}
|
diskMoveBodies := []*proxmox.VirtualEnvironmentVMMoveDiskRequestBody{}
|
||||||
diskResizeBodies := []*proxmox.VirtualEnvironmentVMResizeDiskRequestBody{}
|
diskResizeBodies := []*proxmox.VirtualEnvironmentVMResizeDiskRequestBody{}
|
||||||
|
|
||||||
for i := range diskOldEntries {
|
for prefix, diskMap := range diskOldEntries {
|
||||||
diskOldBlock := diskOldEntries[i].(map[string]interface{})
|
for oldKey, oldDisk := range diskMap {
|
||||||
diskNewBlock := diskNewEntries[i].(map[string]interface{})
|
if _, present := diskNewEntries[prefix][oldKey]; !present {
|
||||||
|
return fmt.Errorf("Deletion of disks not supported. Please delete disk by hand. Old Interface was %s", *oldDisk.Interface)
|
||||||
|
}
|
||||||
|
|
||||||
diskOldDatastoreID := diskOldBlock[mkResourceVirtualEnvironmentVMDiskDatastoreID].(string)
|
if oldDisk.ID != diskNewEntries[prefix][oldKey].ID {
|
||||||
diskOldInterface := diskOldBlock[mkResourcevirtualEnvironmentVMDiskInterface].(string)
|
deleteOriginalDisk := proxmox.CustomBool(true)
|
||||||
diskNewInterface := diskNewBlock[mkResourcevirtualEnvironmentVMDiskInterface].(string)
|
|
||||||
|
|
||||||
if diskNewInterface != diskOldInterface {
|
diskMoveBodies = append(diskMoveBodies, &proxmox.VirtualEnvironmentVMMoveDiskRequestBody{
|
||||||
return fmt.Errorf("Alteration of disk interface is not supported. Old Interface was %s", diskOldInterface)
|
DeleteOriginalDisk: &deleteOriginalDisk,
|
||||||
}
|
Disk: *oldDisk.Interface,
|
||||||
|
TargetStorage: *diskNewEntries[prefix][oldKey].ID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
diskNewDatastoreID := diskNewBlock[mkResourceVirtualEnvironmentVMDiskDatastoreID].(string)
|
if *oldDisk.SizeInt <= *diskNewEntries[prefix][oldKey].SizeInt {
|
||||||
|
diskResizeBodies = append(diskResizeBodies, &proxmox.VirtualEnvironmentVMResizeDiskRequestBody{
|
||||||
if diskOldDatastoreID != diskNewDatastoreID {
|
Disk: *oldDisk.Interface,
|
||||||
deleteOriginalDisk := proxmox.CustomBool(true)
|
Size: *diskNewEntries[prefix][oldKey].Size,
|
||||||
|
})
|
||||||
diskMoveBodies = append(diskMoveBodies, &proxmox.VirtualEnvironmentVMMoveDiskRequestBody{
|
}
|
||||||
DeleteOriginalDisk: &deleteOriginalDisk,
|
|
||||||
Disk: diskOldInterface,
|
|
||||||
TargetStorage: diskNewDatastoreID,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
diskOldSize := diskOldBlock[mkResourceVirtualEnvironmentVMDiskSize].(int)
|
|
||||||
diskNewSize := diskNewBlock[mkResourceVirtualEnvironmentVMDiskSize].(int)
|
|
||||||
|
|
||||||
if diskOldSize <= diskNewSize {
|
|
||||||
diskResizeBodies = append(diskResizeBodies, &proxmox.VirtualEnvironmentVMResizeDiskRequestBody{
|
|
||||||
Disk: diskOldInterface,
|
|
||||||
Size: fmt.Sprintf("%dG", diskNewSize),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,22 +384,6 @@ func getVMIDValidator() schema.SchemaValidateFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getOrderedDiskDeviceList(diskDeviceMap map[string]map[string]proxmox.CustomStorageDevice, diskInterface string) proxmox.CustomStorageDevices {
|
|
||||||
diskDevices := diskDeviceMap[diskInterface]
|
|
||||||
|
|
||||||
if diskDevices == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
orderedDiskList := make(proxmox.CustomStorageDevices, len(diskDevices))
|
|
||||||
|
|
||||||
for _, value := range diskDevices {
|
|
||||||
orderedDiskList = append(orderedDiskList, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
return orderedDiskList
|
|
||||||
}
|
|
||||||
|
|
||||||
func getDiskInfo(data *proxmox.VirtualEnvironmentVMGetResponseData) map[string]*proxmox.CustomStorageDevice {
|
func getDiskInfo(data *proxmox.VirtualEnvironmentVMGetResponseData) map[string]*proxmox.CustomStorageDevice {
|
||||||
storageDevices := make(map[string]*proxmox.CustomStorageDevice)
|
storageDevices := make(map[string]*proxmox.CustomStorageDevice)
|
||||||
storageDevices["ide0"] = data.IDEDevice0
|
storageDevices["ide0"] = data.IDEDevice0
|
||||||
@ -445,6 +429,13 @@ func getDiskInfo(data *proxmox.VirtualEnvironmentVMGetResponseData) map[string]*
|
|||||||
storageDevices["virtio14"] = data.VirtualIODevice14
|
storageDevices["virtio14"] = data.VirtualIODevice14
|
||||||
storageDevices["virtio15"] = data.VirtualIODevice15
|
storageDevices["virtio15"] = data.VirtualIODevice15
|
||||||
|
|
||||||
|
for key, value := range storageDevices {
|
||||||
|
if value != nil {
|
||||||
|
tmpKey := key
|
||||||
|
value.Interface = &tmpKey
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return storageDevices
|
return storageDevices
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user