From 47c5c51ecfa322ca25fd8c80561d9f881e6f644b Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Thu, 1 Sep 2022 15:12:54 +0100 Subject: [PATCH] Adding plan modifier (#303) --- internal/planmodifiers/attribute.go | 56 +++++++++++++++++++++++++++ internal/provider/resource_integer.go | 8 +++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/internal/planmodifiers/attribute.go b/internal/planmodifiers/attribute.go index 9464b1e8..afe34118 100644 --- a/internal/planmodifiers/attribute.go +++ b/internal/planmodifiers/attribute.go @@ -143,3 +143,59 @@ func (d *numberNumericAttributePlanModifier) Modify(ctx context.Context, req tfs return } } + +func RequiresReplaceIfValuesNotNull() tfsdk.AttributePlanModifier { + return requiresReplaceIfValuesNotNullModifier{} +} + +type requiresReplaceIfValuesNotNullModifier struct{} + +func (r requiresReplaceIfValuesNotNullModifier) Modify(ctx context.Context, req tfsdk.ModifyAttributePlanRequest, resp *tfsdk.ModifyAttributePlanResponse) { + if req.AttributeConfig == nil || req.AttributePlan == nil || req.AttributeState == nil { + // shouldn't happen, but let's not panic if it does + return + } + + if req.State.Raw.IsNull() { + // if we're creating the resource, no need to delete and + // recreate it + return + } + + if req.Plan.Raw.IsNull() { + // if we're deleting the resource, no need to delete and + // recreate it + return + } + + attrConfig := req.AttributeConfig + + m, ok := attrConfig.(types.Map) + if !ok { + return + } + + replace := false + + for _, v := range m.Elems { + if v.IsNull() && !v.IsUnknown() { + continue + } + + replace = true + } + + if replace { + resp.RequiresReplace = true + } +} + +// Description returns a human-readable description of the plan modifier. +func (r requiresReplaceIfValuesNotNullModifier) Description(ctx context.Context) string { + return "If the value of this attribute changes, Terraform will destroy and recreate the resource." +} + +// MarkdownDescription returns a markdown description of the plan modifier. +func (r requiresReplaceIfValuesNotNullModifier) MarkdownDescription(ctx context.Context) string { + return "If the value of this attribute changes, Terraform will destroy and recreate the resource." +} diff --git a/internal/provider/resource_integer.go b/internal/provider/resource_integer.go index c1e50b56..0502d5c0 100644 --- a/internal/provider/resource_integer.go +++ b/internal/provider/resource_integer.go @@ -12,6 +12,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/terraform-providers/terraform-provider-random/internal/planmodifiers" "github.com/terraform-providers/terraform-provider-random/internal/random" ) @@ -34,8 +35,11 @@ func (r *integerResourceType) GetSchema(context.Context) (tfsdk.Schema, diag.Dia Type: types.MapType{ ElemType: types.StringType, }, - Optional: true, - PlanModifiers: []tfsdk.AttributePlanModifier{resource.RequiresReplace()}, + Optional: true, + Computed: true, + PlanModifiers: []tfsdk.AttributePlanModifier{ + planmodifiers.RequiresReplaceIfValuesNotNull(), + }, }, "min": { Description: "The minimum inclusive value of the range.",