0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 19:12:59 +00:00

fix: state diff issue

This commit is contained in:
Dan Petersen 2021-02-13 07:18:29 +01:00
parent fec2a7c9e1
commit 781d83535b
9 changed files with 125 additions and 87 deletions

View File

@ -1,4 +1,8 @@
## v0.4.2 (UNRELEASED)
## v0.4.2
BUG FIXES:
* resource/virtual_environment_vm: Fix `disk.file_id` diff issue
OTHER:
@ -47,7 +51,7 @@ BUG FIXES:
* resource/virtual_environment_container: Fix VM ID collision when `vm_id` is not specified
* resource/virtual_environment_vm: Fix VM ID collision when `vm_id` is not specified
* resource/virtual_environment_vm: Fix disk import issue when importing from directory-based datastores
* resource/virtual/environment/vm: Fix handling of storage name - correct handling of `-`
* resource/virtual_environment/vm: Fix handling of storage name - correct handling of `-`
WORKAROUNDS:

View File

@ -12,7 +12,7 @@ import (
)
// CreateAlias create an alias
func (c *VirtualEnvironmentClient) CreateAlias(d *VirtualEnvironmentClusterAliasCreateRequestBody) error {
func (c *VirtualEnvironmentClient) CreateAlias(d *VirtualEnvironmentClusterAliasCreateRequestBody) error {
return c.DoRequest(hmPOST, "cluster/firewall/aliases", d, nil)
}
@ -60,4 +60,4 @@ func (c *VirtualEnvironmentClient) ListAliases() ([]*VirtualEnvironmentClusterAl
// UpdateAlias updates an alias.
func (c *VirtualEnvironmentClient) UpdateAlias(id string, d *VirtualEnvironmentClusterAliasUpdateRequestBody) error {
return c.DoRequest(hmPUT, fmt.Sprintf("cluster/firewall/aliases/%s", url.PathEscape(id)), d, nil)
}
}

View File

@ -18,11 +18,11 @@ type VirtualEnvironmentClusterAliasGetResponseBody struct {
// VirtualEnvironmentClusterAliasGetResponseData contains the data from an alias get response.
type VirtualEnvironmentClusterAliasGetResponseData struct {
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Name string `json:"name" url:"name"`
CIDR string `json:"cidr" url:"cidr"`
Digest *string `json:"digest" url:"digest"`
IPVersion int `json:"ipversion" url:"ipversion"`
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Name string `json:"name" url:"name"`
CIDR string `json:"cidr" url:"cidr"`
Digest *string `json:"digest" url:"digest"`
IPVersion int `json:"ipversion" url:"ipversion"`
}
// VirtualEnvironmentClusterAliasListResponseBody contains the data from an alias get response.
@ -33,7 +33,6 @@ type VirtualEnvironmentClusterAliasListResponseBody struct {
// VirtualEnvironmentClusterAliasUpdateRequestBody contains the data for an alias update request.
type VirtualEnvironmentClusterAliasUpdateRequestBody struct {
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
ReName string `json:"rename" url:"rename"`
CIDR string `json:"cidr" url:"cidr"`
ReName string `json:"rename" url:"rename"`
CIDR string `json:"cidr" url:"cidr"`
}

View File

@ -12,7 +12,7 @@ import (
)
// CreateIPSet create an IPSet
func (c *VirtualEnvironmentClient) CreateIPSet(d *VirtualEnvironmentClusterIPSetCreateRequestBody) error {
func (c *VirtualEnvironmentClient) CreateIPSet(d *VirtualEnvironmentClusterIPSetCreateRequestBody) error {
return c.DoRequest(hmPOST, "cluster/firewall/ipset", d, nil)
}
@ -70,4 +70,4 @@ func (c *VirtualEnvironmentClient) GetListIPSets() (*VirtualEnvironmentClusterIP
})
return resBody, nil
}
}

View File

@ -16,7 +16,7 @@ type VirtualEnvironmentClusterIPSetListResponseBody struct {
// VirtualEnvironmentClusterIPSetCreateRequestBody contains the data for an IPSet create request
type VirtualEnvironmentClusterIPSetCreateRequestBody struct {
Comment string `json:"comment,omitempty" url:"comment,omitempty"`
Name string `json:"name" url:"name"`
Name string `json:"name" url:"name"`
}
// VirtualEnvironmentClusterIPSetGetResponseBody contains the body from an IPSet get response.
@ -26,22 +26,22 @@ type VirtualEnvironmentClusterIPSetGetResponseBody struct {
// VirtualEnvironmentClusterIPSetGetResponseData contains the data from an IPSet get response.
type VirtualEnvironmentClusterIPSetGetResponseData struct {
CIDR string `json:"cidr" url:"cidr"`
NoMatch *CustomBool `json:"nomatch,omitempty" url:"nomatch,omitempty,int"`
Comment string `json:"comment,omitempty" url:"comment,omitempty"`
CIDR string `json:"cidr" url:"cidr"`
NoMatch *CustomBool `json:"nomatch,omitempty" url:"nomatch,omitempty,int"`
Comment string `json:"comment,omitempty" url:"comment,omitempty"`
}
// VirtualEnvironmentClusterIPSetUpdateRequestBody contains the data for an IPSet update request.
type VirtualEnvironmentClusterIPSetUpdateRequestBody struct {
ReName string `json:"rename,omitempty" url:"rename,omitempty"`
ReName string `json:"rename,omitempty" url:"rename,omitempty"`
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Name string `json:"name" url:"name"`
}
// VirtualEnvironmentClusterIPSetGetResponseData contains list of IPSets from
type VirtualEnvironmentClusterIPSetListResponseData struct {
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Name string `json:"name" url:"name"`
Comment *string `json:"comment,omitempty" url:"comment,omitempty"`
Name string `json:"name" url:"name"`
}
// VirtualEnvironmentClusterIPSetContent is an array of VirtualEnvironmentClusterIPSetGetResponseData.

View File

@ -6,6 +6,7 @@ package proxmoxtf
import (
"fmt"
"sort"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
@ -126,7 +127,9 @@ func dataSourceVirtualEnvironmentDatastoresRead(d *schema.ResourceData, m interf
}
if v.ContentTypes != nil {
contentTypes[i] = []string(*v.ContentTypes)
contentTypeList := []string(*v.ContentTypes)
sort.Strings(contentTypeList)
contentTypes[i] = contentTypeList
} else {
contentTypes[i] = []string{}
}

View File

@ -177,7 +177,7 @@ func resourceVirtualEnvironmentClusterIPSetRead(d *schema.ResourceData, m interf
return err
}
for key, _ := range IPSet {
for key := range IPSet {
d.Set(mkResourceVirtualEnvironmentClusterIPSetCIDR, IPSet[key])
}

View File

@ -1395,8 +1395,7 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
return err
}
allDiskInfo := getDiskInfo(vmConfig)
allDiskInfo := getDiskInfo(vmConfig, d)
diskDeviceObjects, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, nil)
if err != nil {
@ -1404,7 +1403,6 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
}
for i := range disk {
diskBlock := disk[i].(map[string]interface{})
diskInterface := diskBlock[mkResourcevirtualEnvironmentVMDiskInterface].(string)
dataStoreID := diskBlock[mkResourceVirtualEnvironmentVMDiskDatastoreID].(string)
@ -1418,17 +1416,17 @@ func resourceVirtualEnvironmentVMCreateClone(d *schema.ResourceData, m interface
switch prefix {
case "virtio":
if diskUpdateBody.VirtualIODevices == nil {
diskUpdateBody.VirtualIODevices = make(proxmox.CustomStorageDevices)
diskUpdateBody.VirtualIODevices = proxmox.CustomStorageDevices{}
}
diskUpdateBody.VirtualIODevices[diskInterface] = diskDeviceObjects[prefix][diskInterface]
case "sata":
if diskUpdateBody.SATADevices == nil {
diskUpdateBody.SATADevices = make(proxmox.CustomStorageDevices)
diskUpdateBody.SATADevices = proxmox.CustomStorageDevices{}
}
diskUpdateBody.SATADevices[diskInterface] = diskDeviceObjects[prefix][diskInterface]
case "scsi":
if diskUpdateBody.SCSIDevices == nil {
diskUpdateBody.SCSIDevices = make(proxmox.CustomStorageDevices)
diskUpdateBody.SCSIDevices = proxmox.CustomStorageDevices{}
}
diskUpdateBody.SCSIDevices[diskInterface] = diskDeviceObjects[prefix][diskInterface]
}
@ -1798,7 +1796,7 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(d *schema.ResourceData, m int
fileFormat, _ := block[mkResourceVirtualEnvironmentVMDiskFileFormat].(string)
size, _ := block[mkResourceVirtualEnvironmentVMDiskSize].(int)
speed := block[mkResourceVirtualEnvironmentVMDiskSpeed].([]interface{})
diskInterface,_ := block[mkResourcevirtualEnvironmentVMDiskInterface].(string)
diskInterface, _ := block[mkResourcevirtualEnvironmentVMDiskInterface].(string)
if len(speed) == 0 {
diskSpeedDefault, err := diskSpeedResource.DefaultValue()
@ -2087,12 +2085,14 @@ func resourceVirtualEnvironmentVMGetCPUArchitectureValidator() schema.SchemaVali
func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, m interface{}, disks []interface{}) (map[string]map[string]proxmox.CustomStorageDevice, error) {
var diskDevice []interface{}
if disks != nil {
diskDevice = disks
} else {
diskDevice = d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{})
}
diskDeviceObjects := make(map[string]map[string]proxmox.CustomStorageDevice)
diskDeviceObjects := map[string]map[string]proxmox.CustomStorageDevice{}
resource := resourceVirtualEnvironmentVM()
for _, diskEntry := range diskDevice {
@ -2156,7 +2156,7 @@ func resourceVirtualEnvironmentVMGetDiskDeviceObjects(d *schema.ResourceData, m
}
if _, present := diskDeviceObjects[baseDiskInterface]; !present {
diskDeviceObjects[baseDiskInterface] = make(map[string]proxmox.CustomStorageDevice)
diskDeviceObjects[baseDiskInterface] = map[string]proxmox.CustomStorageDevice{}
}
diskDeviceObjects[baseDiskInterface][diskInterface] = diskDevice
@ -2439,7 +2439,6 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
// Compare the IDE devices to the CDROM and cloud-init configurations stored in the state.
if vmConfig.IDEDevice3 != nil {
cdrom := make([]interface{}, 1)
cdromBlock := map[string]interface{}{}
currentCDROM := d.Get(mkResourceVirtualEnvironmentVMCDROM).([]interface{})
@ -2549,9 +2548,10 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
d.Set(mkResourceVirtualEnvironmentVMCPU, []interface{}{cpu})
}
currentDiskList := d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{})
diskMap := map[string]interface{}{}
diskObjects := getDiskInfo(vmConfig, d)
orderedDiskList := []interface{}{}
diskObjects := getDiskInfo(vmConfig)
for di, dd := range diskObjects {
disk := map[string]interface{}{}
@ -2564,12 +2564,16 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
disk[mkResourceVirtualEnvironmentVMDiskDatastoreID] = fileIDParts[0]
disk[mkResourceVirtualEnvironmentVMDiskFileID] = dd.FileID
if dd.Format == nil {
disk[mkResourceVirtualEnvironmentVMDiskFileFormat] = "qcow2"
} else {
disk[mkResourceVirtualEnvironmentVMDiskFileFormat] = dd.Format
}
if dd.FileID != nil {
disk[mkResourceVirtualEnvironmentVMDiskFileID] = dd.FileID
}
disk[mkResourcevirtualEnvironmentVMDiskInterface] = di
diskSize := 0
@ -2622,18 +2626,22 @@ func resourceVirtualEnvironmentVMReadCustom(d *schema.ResourceData, m interface{
}
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(orderedDiskList) > 0 {
if len(currentDiskList) > 0 {
d.Set(mkResourceVirtualEnvironmentVMDisk, orderedDiskList)
}
} else if len(orderedDiskList) > 0 {
} else if len(currentDiskList) > 0 {
d.Set(mkResourceVirtualEnvironmentVMDisk, orderedDiskList)
}
@ -3378,7 +3386,7 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
return err
}
diskDeviceInfo := getDiskInfo(vmConfig)
diskDeviceInfo := getDiskInfo(vmConfig, d)
for prefix, diskMap := range diskDeviceObjects {
if diskMap == nil {
@ -3400,27 +3408,30 @@ func resourceVirtualEnvironmentVMUpdate(d *schema.ResourceData, m interface{}) e
case "virtio":
{
if updateBody.VirtualIODevices == nil {
updateBody.VirtualIODevices = make(proxmox.CustomStorageDevices)
updateBody.VirtualIODevices = proxmox.CustomStorageDevices{}
}
updateBody.VirtualIODevices[key] = tmp
}
case "sata":
{
if updateBody.SATADevices == nil {
updateBody.SATADevices = make(proxmox.CustomStorageDevices)
updateBody.SATADevices = proxmox.CustomStorageDevices{}
}
updateBody.SATADevices[key] = tmp
}
case "scsi":
{
if updateBody.SCSIDevices == nil {
updateBody.SCSIDevices = make(proxmox.CustomStorageDevices)
updateBody.SCSIDevices = proxmox.CustomStorageDevices{}
}
updateBody.SCSIDevices[key] = tmp
}
case "ide":
{
//not sure right now
// Investigate whether to support IDE mapping.
}
default:
return fmt.Errorf("Device prefix %s not supported", prefix)
@ -3618,11 +3629,13 @@ func resourceVirtualEnvironmentVMUpdateDiskLocationAndSize(d *schema.ResourceDat
diskOld, diskNew := d.GetChange(mkResourceVirtualEnvironmentVMDisk)
diskOldEntries, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, diskOld.([]interface{}))
if err != nil {
return err
}
diskNewEntries, err := resourceVirtualEnvironmentVMGetDiskDeviceObjects(d, m, diskNew.([]interface{}))
if err != nil {
return err
}

View File

@ -384,55 +384,74 @@ func getVMIDValidator() schema.SchemaValidateFunc {
}
}
func getDiskInfo(data *proxmox.VirtualEnvironmentVMGetResponseData) map[string]*proxmox.CustomStorageDevice {
storageDevices := make(map[string]*proxmox.CustomStorageDevice)
storageDevices["ide0"] = data.IDEDevice0
storageDevices["ide1"] = data.IDEDevice1
storageDevices["ide2"] = data.IDEDevice2
func getDiskInfo(vm *proxmox.VirtualEnvironmentVMGetResponseData, d *schema.ResourceData) map[string]*proxmox.CustomStorageDevice {
currentDisk := d.Get(mkResourceVirtualEnvironmentVMDisk)
storageDevices["sata0"] = data.SATADevice0
storageDevices["sata1"] = data.SATADevice1
storageDevices["sata2"] = data.SATADevice2
storageDevices["sata3"] = data.SATADevice3
storageDevices["sata4"] = data.SATADevice4
storageDevices["sata5"] = data.SATADevice5
currentDiskList := currentDisk.([]interface{})
currentDiskMap := map[string]map[string]interface{}{}
storageDevices["scsi0"] = data.SCSIDevice0
storageDevices["scsi1"] = data.SCSIDevice1
storageDevices["scsi2"] = data.SCSIDevice2
storageDevices["scsi3"] = data.SCSIDevice3
storageDevices["scsi4"] = data.SCSIDevice4
storageDevices["scsi5"] = data.SCSIDevice5
storageDevices["scsi6"] = data.SCSIDevice6
storageDevices["scsi7"] = data.SCSIDevice7
storageDevices["scsi8"] = data.SCSIDevice8
storageDevices["scsi9"] = data.SCSIDevice9
storageDevices["scsi10"] = data.SCSIDevice10
storageDevices["scsi11"] = data.SCSIDevice11
storageDevices["scsi12"] = data.SCSIDevice12
storageDevices["scsi13"] = data.SCSIDevice13
for _, v := range currentDiskList {
diskMap := v.(map[string]interface{})
diskInterface := diskMap[mkResourcevirtualEnvironmentVMDiskInterface].(string)
storageDevices["virtio0"] = data.VirtualIODevice0
storageDevices["virtio1"] = data.VirtualIODevice1
storageDevices["virtio2"] = data.VirtualIODevice2
storageDevices["virtio3"] = data.VirtualIODevice3
storageDevices["virtio4"] = data.VirtualIODevice4
storageDevices["virtio5"] = data.VirtualIODevice5
storageDevices["virtio6"] = data.VirtualIODevice6
storageDevices["virtio7"] = data.VirtualIODevice7
storageDevices["virtio8"] = data.VirtualIODevice8
storageDevices["virtio9"] = data.VirtualIODevice9
storageDevices["virtio10"] = data.VirtualIODevice10
storageDevices["virtio11"] = data.VirtualIODevice11
storageDevices["virtio12"] = data.VirtualIODevice12
storageDevices["virtio13"] = data.VirtualIODevice13
storageDevices["virtio14"] = data.VirtualIODevice14
storageDevices["virtio15"] = data.VirtualIODevice15
currentDiskMap[diskInterface] = diskMap
}
for key, value := range storageDevices {
if value != nil {
tmpKey := key
value.Interface = &tmpKey
storageDevices := map[string]*proxmox.CustomStorageDevice{}
storageDevices["ide0"] = vm.IDEDevice0
storageDevices["ide1"] = vm.IDEDevice1
storageDevices["ide2"] = vm.IDEDevice2
storageDevices["sata0"] = vm.SATADevice0
storageDevices["sata1"] = vm.SATADevice1
storageDevices["sata2"] = vm.SATADevice2
storageDevices["sata3"] = vm.SATADevice3
storageDevices["sata4"] = vm.SATADevice4
storageDevices["sata5"] = vm.SATADevice5
storageDevices["scsi0"] = vm.SCSIDevice0
storageDevices["scsi1"] = vm.SCSIDevice1
storageDevices["scsi2"] = vm.SCSIDevice2
storageDevices["scsi3"] = vm.SCSIDevice3
storageDevices["scsi4"] = vm.SCSIDevice4
storageDevices["scsi5"] = vm.SCSIDevice5
storageDevices["scsi6"] = vm.SCSIDevice6
storageDevices["scsi7"] = vm.SCSIDevice7
storageDevices["scsi8"] = vm.SCSIDevice8
storageDevices["scsi9"] = vm.SCSIDevice9
storageDevices["scsi10"] = vm.SCSIDevice10
storageDevices["scsi11"] = vm.SCSIDevice11
storageDevices["scsi12"] = vm.SCSIDevice12
storageDevices["scsi13"] = vm.SCSIDevice13
storageDevices["virtio0"] = vm.VirtualIODevice0
storageDevices["virtio1"] = vm.VirtualIODevice1
storageDevices["virtio2"] = vm.VirtualIODevice2
storageDevices["virtio3"] = vm.VirtualIODevice3
storageDevices["virtio4"] = vm.VirtualIODevice4
storageDevices["virtio5"] = vm.VirtualIODevice5
storageDevices["virtio6"] = vm.VirtualIODevice6
storageDevices["virtio7"] = vm.VirtualIODevice7
storageDevices["virtio8"] = vm.VirtualIODevice8
storageDevices["virtio9"] = vm.VirtualIODevice9
storageDevices["virtio10"] = vm.VirtualIODevice10
storageDevices["virtio11"] = vm.VirtualIODevice11
storageDevices["virtio12"] = vm.VirtualIODevice12
storageDevices["virtio13"] = vm.VirtualIODevice13
storageDevices["virtio14"] = vm.VirtualIODevice14
storageDevices["virtio15"] = vm.VirtualIODevice15
for k, v := range storageDevices {
if v != nil {
if currentDiskMap[k] != nil {
if currentDiskMap[k][mkResourceVirtualEnvironmentVMDiskFileID] != nil {
fileID := currentDiskMap[k][mkResourceVirtualEnvironmentVMDiskFileID].(string)
v.FileID = &fileID
}
}
v.Interface = &k
}
}