d9d8cc7b6d
The API returns null for empty arrays, but OpenTofu requires that the state match the plan exactly — an empty list [] in the plan must remain [] in the state, not become null. This caused "inconsistent result after apply" errors on every resource with empty optional list fields like mutable_patterns and ban_tags.
40 lines
1.0 KiB
Go
40 lines
1.0 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
)
|
|
|
|
func listToStrings(ctx context.Context, l types.List) []string {
|
|
if l.IsNull() || l.IsUnknown() {
|
|
return nil
|
|
}
|
|
var result []string
|
|
l.ElementsAs(ctx, &result, false)
|
|
return result
|
|
}
|
|
|
|
func stringsToList(ctx context.Context, ss []string) types.List {
|
|
if ss == nil {
|
|
return types.ListNull(types.StringType)
|
|
}
|
|
elems := make([]types.String, len(ss))
|
|
for i, s := range ss {
|
|
elems[i] = types.StringValue(s)
|
|
}
|
|
list, _ := types.ListValueFrom(ctx, types.StringType, elems)
|
|
return list
|
|
}
|
|
|
|
// preserveListNullEmptySemantics keeps the prior null/empty distinction when
|
|
// the API returns null for a field that was previously an empty list.
|
|
// Without this, OpenTofu reports "inconsistent result after apply" because
|
|
// the plan/state had [] but the provider returned null.
|
|
func preserveListNullEmptySemantics(prior, current types.List) types.List {
|
|
if current.IsNull() && !prior.IsNull() && len(prior.Elements()) == 0 {
|
|
return prior
|
|
}
|
|
return current
|
|
}
|