0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-29 18:21:10 +00:00
terraform-provider-proxmox/utils/sets.go
Pavel Boldyrev 80cafa689f
feat(vm2): add initial support for cdrom (#1370)
feat(vm2): add initial support for `cdrom`

This is a breaking change comparing to v1 - switching the cdrom schema from a nested block to a nested attribute map.

Improvements comparing to v1:
- support for `ide`, `sata`, `scsi` interfaces
- support for multiple cdroms

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-06-10 01:28:18 +00:00

46 lines
1.2 KiB
Go

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package utils
import "reflect"
// SetDiff compares two slices of elements and returns the elements that are in the plan but not
// in the state (toCreate), the elements that are in the plan and in the state but are different (toUpdate),
// and the elements that are in the state but not in the plan (toDelete).
// The keyFunc is used to extract a unique key from each element to compare them.
func SetDiff[T any](plan []T, state []T, keyFunc func(t T) string) ([]T, []T, []T) {
var toCreate, toUpdate, toDelete []T
stateMap := map[string]T{}
for _, s := range state {
stateMap[keyFunc(s)] = s
}
planMap := map[string]T{}
for _, p := range plan {
planMap[keyFunc(p)] = p
}
for _, p := range plan {
s, ok := stateMap[keyFunc(p)]
if !ok {
toCreate = append(toCreate, p)
} else if !reflect.DeepEqual(p, s) {
toUpdate = append(toUpdate, p)
}
}
for _, s := range state {
_, ok := planMap[keyFunc(s)]
if !ok {
toDelete = append(toDelete, s)
}
}
return toCreate, toUpdate, toDelete
}