|
| 1 | +package planmodifiers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 8 | +) |
| 9 | + |
| 10 | +func RequiresReplaceIfValuesNotNull() tfsdk.AttributePlanModifier { |
| 11 | + return requiresReplaceIfValuesNotNullModifier{} |
| 12 | +} |
| 13 | + |
| 14 | +type requiresReplaceIfValuesNotNullModifier struct{} |
| 15 | + |
| 16 | +func (r requiresReplaceIfValuesNotNullModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) { |
| 17 | + if req.AttributeConfig == nil || req.AttributePlan == nil || req.AttributeState == nil { |
| 18 | + // shouldn't happen, but let's not panic if it does |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + if req.State.Raw.IsNull() { |
| 23 | + // if we're creating the resource, no need to delete and |
| 24 | + // recreate it |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + if req.Plan.Raw.IsNull() { |
| 29 | + // if we're deleting the resource, no need to delete and |
| 30 | + // recreate it |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + // If there are no differences, do not mark the resource for replacement |
| 35 | + // and ensure the plan matches the configuration. |
| 36 | + if req.AttributeConfig.Equal(req.AttributeState) { |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if req.AttributeState.IsNull() { |
| 41 | + // terraform-plugin-sdk would store maps as null if all keys had null |
| 42 | + // values. To prevent unintentional replacement plans when migrating |
| 43 | + // to terraform-plugin-framework, only trigger replacement when the |
| 44 | + // prior state (map) is null and when there are not null map values. |
| 45 | + allNullValues := true |
| 46 | + |
| 47 | + configMap, ok := req.AttributeConfig.(types.Map) |
| 48 | + |
| 49 | + if !ok { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + for _, configValue := range configMap.Elems { |
| 54 | + if !configValue.IsNull() { |
| 55 | + allNullValues = false |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if allNullValues { |
| 60 | + return |
| 61 | + } |
| 62 | + } else { |
| 63 | + // terraform-plugin-sdk would completely omit storing map keys with |
| 64 | + // null values, so this also must prevent unintentional replacement |
| 65 | + // in that case as well. |
| 66 | + allNewNullValues := true |
| 67 | + |
| 68 | + configMap, ok := req.AttributeConfig.(types.Map) |
| 69 | + |
| 70 | + if !ok { |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + stateMap, ok := req.AttributeState.(types.Map) |
| 75 | + |
| 76 | + if !ok { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + for configKey, configValue := range configMap.Elems { |
| 81 | + stateValue, ok := stateMap.Elems[configKey] |
| 82 | + |
| 83 | + // If the key doesn't exist in state and the config value is |
| 84 | + // null, do not trigger replacement. |
| 85 | + if !ok && configValue.IsNull() { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + // If the state value exists and it is equal to the config value, |
| 90 | + // do not trigger replacement. |
| 91 | + if configValue.Equal(stateValue) { |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + allNewNullValues = false |
| 96 | + break |
| 97 | + } |
| 98 | + |
| 99 | + for stateKey := range stateMap.Elems { |
| 100 | + _, ok := configMap.Elems[stateKey] |
| 101 | + |
| 102 | + // If the key doesn't exist in the config, but there is a state |
| 103 | + // value, trigger replacement. |
| 104 | + if !ok { |
| 105 | + allNewNullValues = false |
| 106 | + break |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if allNewNullValues { |
| 111 | + return |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + resp.RequiresReplace = true |
| 116 | +} |
| 117 | + |
| 118 | +// Description returns a human-readable description of the plan modifier. |
| 119 | +func (r requiresReplaceIfValuesNotNullModifier) Description(ctx context.Context) string { |
| 120 | + return "If the value of this attribute changes, Terraform will destroy and recreate the resource." |
| 121 | +} |
| 122 | + |
| 123 | +// MarkdownDescription returns a markdown description of the plan modifier. |
| 124 | +func (r requiresReplaceIfValuesNotNullModifier) MarkdownDescription(ctx context.Context) string { |
| 125 | + return "If the value of this attribute changes, Terraform will destroy and recreate the resource." |
| 126 | +} |
0 commit comments