mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-09 23:35:00 +00:00
fix(vm): yet another fix for disk reordering (#1297)
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
parent
bfb20e0bfd
commit
3180f81b4a
@ -580,13 +580,10 @@ func TestAccResourceVMDisks(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}`),
|
}`),
|
||||||
Check: testResourceAttributes("proxmox_virtual_environment_vm.test_disk4", map[string]string{
|
Check: testResourceAttributes("proxmox_virtual_environment_vm.test_disk4", map[string]string{
|
||||||
// I'd love to test this, but the order of the list items is not guaranteed, apparently
|
"disk.0.interface": "virtio0",
|
||||||
// resource_vm_test.go:669: Step 1/2 error: Check failed: proxmox_virtual_environment_vm.test_disk4: Attribute "disk.1.interface" value: expected 'virtio0' to match 'scsi0'
|
"disk.0.path_in_datastore": `vm-\d+-disk-1`,
|
||||||
// --- FAIL: TestAccResourceVMDisks/multiple_disks (5.24s)
|
"disk.1.interface": "scsi0",
|
||||||
// "disk.1.interface": "scsi0",
|
"disk.1.path_in_datastore": `vm-\d+-disk-0`,
|
||||||
// "disk.1.path_in_datastore": `vm-\d+-disk-0`,
|
|
||||||
// "disk.0.interface": "virtio0",
|
|
||||||
// "disk.0.path_in_datastore": `vm-\d+-disk-1`,
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||||
"golang.org/x/exp/maps"
|
|
||||||
|
|
||||||
"github.com/bpg/terraform-provider-proxmox/proxmox"
|
"github.com/bpg/terraform-provider-proxmox/proxmox"
|
||||||
"github.com/bpg/terraform-provider-proxmox/proxmox/nodes/vms"
|
"github.com/bpg/terraform-provider-proxmox/proxmox/nodes/vms"
|
||||||
@ -73,7 +72,7 @@ func GetInfo(resp *vms.GetResponseData, d *schema.ResourceData) vms.CustomStorag
|
|||||||
|
|
||||||
currentDisk := d.Get(MkDisk)
|
currentDisk := d.Get(MkDisk)
|
||||||
|
|
||||||
diskMap := utils.MapResourceList(currentDisk.([]interface{}), mkDiskInterface)
|
diskMap := utils.MapResourcesByAttribute(currentDisk.([]interface{}), mkDiskInterface)
|
||||||
|
|
||||||
for k, v := range storageDevices {
|
for k, v := range storageDevices {
|
||||||
if v != nil && diskMap[k] != nil {
|
if v != nil && diskMap[k] != nil {
|
||||||
@ -624,8 +623,7 @@ func Read(
|
|||||||
var diskList []interface{}
|
var diskList []interface{}
|
||||||
|
|
||||||
if len(currentDiskList) > 0 {
|
if len(currentDiskList) > 0 {
|
||||||
resMap := utils.MapResourceList(currentDiskList, mkDiskInterface)
|
interfaces := utils.ListResourcesAttributeValue(currentDiskList, mkDiskInterface)
|
||||||
interfaces := maps.Keys[map[string]interface{}](resMap)
|
|
||||||
diskList = utils.OrderedListFromMapByKeyValues(diskMap, interfaces)
|
diskList = utils.OrderedListFromMapByKeyValues(diskMap, interfaces)
|
||||||
} else {
|
} else {
|
||||||
diskList = utils.OrderedListFromMap(diskMap)
|
diskList = utils.OrderedListFromMap(diskMap)
|
||||||
|
@ -17,8 +17,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"golang.org/x/exp/maps"
|
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
@ -3664,8 +3662,7 @@ func vmReadCustom(
|
|||||||
var numaList []interface{}
|
var numaList []interface{}
|
||||||
|
|
||||||
if len(currentNUMAList) > 0 {
|
if len(currentNUMAList) > 0 {
|
||||||
resMap := utils.MapResourceList(currentNUMAList, mkNUMADevice)
|
devices := utils.ListResourcesAttributeValue(currentNUMAList, mkNUMADevice)
|
||||||
devices := maps.Keys[map[string]interface{}](resMap)
|
|
||||||
numaList = utils.OrderedListFromMapByKeyValues(numaMap, devices)
|
numaList = utils.OrderedListFromMapByKeyValues(numaMap, devices)
|
||||||
} else {
|
} else {
|
||||||
numaList = utils.OrderedListFromMap(numaMap)
|
numaList = utils.OrderedListFromMap(numaMap)
|
||||||
|
@ -168,8 +168,8 @@ func SuppressIfListsOfMapsAreEqualIgnoringOrderByKey(
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
oldMap := utils.MapResourceList(oldArray, keyAttr)
|
oldMap := utils.MapResourcesByAttribute(oldArray, keyAttr)
|
||||||
newMap := utils.MapResourceList(newArray, keyAttr)
|
newMap := utils.MapResourcesByAttribute(newArray, keyAttr)
|
||||||
|
|
||||||
for k, v := range oldMap {
|
for k, v := range oldMap {
|
||||||
if _, ok := newMap[k]; !ok {
|
if _, ok := newMap[k]; !ok {
|
||||||
|
@ -18,12 +18,29 @@ func OrderedListFromMap(inputMap map[string]interface{}) []interface{} {
|
|||||||
return OrderedListFromMapByKeyValues(inputMap, keyList)
|
return OrderedListFromMapByKeyValues(inputMap, keyList)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MapResourceList generates a list of strings from a Terraform resource list (list of maps).
|
// ListResourcesAttributeValue generates a list of strings from a Terraform resource list (which is list of maps).
|
||||||
// The list is generated from the value of the specified attribute.
|
// The list is generated by extracting a specific key attribute from each resource. If the attribute is not found in a
|
||||||
//
|
// resource, it is skipped.
|
||||||
// "Map" in this context is a functional programming term, not a Go map.
|
func ListResourcesAttributeValue(resourceList []interface{}, keyAttr string) []string {
|
||||||
// "Resource" in this context is a Terraform resource, i.e. a map of attributes.
|
var l []string
|
||||||
func MapResourceList(resourceList []interface{}, attrName string) map[string]interface{} {
|
|
||||||
|
for _, resource := range resourceList {
|
||||||
|
if resource == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
r := resource.(map[string]interface{})
|
||||||
|
if value, ok := r[keyAttr].(string); ok {
|
||||||
|
l = append(l, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// MapResourcesByAttribute generates a map of resources from a resource list, using a specified attribute as the key
|
||||||
|
// and the resource as the value. If the attribute is not found in a resource, it is skipped.
|
||||||
|
func MapResourcesByAttribute(resourceList []interface{}, keyAttr string) map[string]interface{} {
|
||||||
m := make(map[string]interface{}, len(resourceList))
|
m := make(map[string]interface{}, len(resourceList))
|
||||||
|
|
||||||
for _, resource := range resourceList {
|
for _, resource := range resourceList {
|
||||||
@ -32,7 +49,7 @@ func MapResourceList(resourceList []interface{}, attrName string) map[string]int
|
|||||||
}
|
}
|
||||||
|
|
||||||
r := resource.(map[string]interface{})
|
r := resource.(map[string]interface{})
|
||||||
if key, ok := r[attrName].(string); ok {
|
if key, ok := r[keyAttr].(string); ok {
|
||||||
m[key] = r
|
m[key] = r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ func TestOrderedListFromMap(t *testing.T) {
|
|||||||
result := OrderedListFromMap(inputMap)
|
result := OrderedListFromMap(inputMap)
|
||||||
|
|
||||||
if !reflect.DeepEqual(result, expected) {
|
if !reflect.DeepEqual(result, expected) {
|
||||||
t.Errorf("MapResourceList() = %v, want %v", result, expected)
|
t.Errorf("ListResourcesAttributeValue() = %v, want %v", result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,18 +35,20 @@ func TestMapResourceList(t *testing.T) {
|
|||||||
map[string]interface{}{"name": "resource2", "attr": "value2"},
|
map[string]interface{}{"name": "resource2", "attr": "value2"},
|
||||||
nil,
|
nil,
|
||||||
map[string]interface{}{"name": "resource3", "attr": "value3"},
|
map[string]interface{}{"name": "resource3", "attr": "value3"},
|
||||||
|
map[string]interface{}{"name": "resource4", "attr": "value4"},
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := map[string]interface{}{
|
expected := []string{
|
||||||
"value1": map[string]interface{}{"name": "resource1", "attr": "value1"},
|
"value1",
|
||||||
"value2": map[string]interface{}{"name": "resource2", "attr": "value2"},
|
"value2",
|
||||||
"value3": map[string]interface{}{"name": "resource3", "attr": "value3"},
|
"value3",
|
||||||
|
"value4",
|
||||||
}
|
}
|
||||||
|
|
||||||
result := MapResourceList(resourceList, "attr")
|
result := ListResourcesAttributeValue(resourceList, "attr")
|
||||||
|
|
||||||
if !reflect.DeepEqual(result, expected) {
|
if !reflect.DeepEqual(result, expected) {
|
||||||
t.Errorf("MapResourceList() = %v, want %v", result, expected)
|
t.Errorf("ListResourcesAttributeValue() = %v, want %v", result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user