2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2023-01-11 03:35:36 -05:00
|
|
|
package collections
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/hashicorp/terraform/internal/command/jsonformat/computed"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/plans"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ProcessKey func(key string) computed.Diff
|
|
|
|
|
|
2023-04-24 04:28:21 -04:00
|
|
|
func TransformMap[Input any](before, after map[string]Input, keys []string, process ProcessKey) (map[string]computed.Diff, plans.Action) {
|
2023-01-11 03:35:36 -05:00
|
|
|
current := plans.NoOp
|
|
|
|
|
if before != nil && after == nil {
|
|
|
|
|
current = plans.Delete
|
|
|
|
|
}
|
|
|
|
|
if before == nil && after != nil {
|
|
|
|
|
current = plans.Create
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elements := make(map[string]computed.Diff)
|
2023-04-24 04:28:21 -04:00
|
|
|
for _, key := range keys {
|
2023-01-11 03:35:36 -05:00
|
|
|
elements[key] = process(key)
|
|
|
|
|
current = CompareActions(current, elements[key].Action)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return elements, current
|
|
|
|
|
}
|