@@ -11,12 +11,14 @@ import (
1111 "github.com/hashicorp/terraform-plugin-framework/resource"
1212 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1313 "github.com/hashicorp/terraform-plugin-framework/resource/schema/numberplanmodifier"
14+ "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
1415 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1516 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1617 "github.com/hashicorp/terraform-plugin-framework/schema/validator"
1718 "github.com/hashicorp/terraform-plugin-framework/types"
1819 "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1920 "github.com/hashicorp/terraform-plugin-log/tflog"
21+ "log"
2022 "strings"
2123)
2224
@@ -93,6 +95,9 @@ func (r *resourceTFEDataRetentionPolicy) Schema(ctx context.Context, req resourc
9395 path .MatchRelative ().AtParent ().AtName ("delete_older_than" ),
9496 ),
9597 },
98+ PlanModifiers : []planmodifier.Object {
99+ objectplanmodifier .RequiresReplace (),
100+ },
96101 },
97102 },
98103 }
@@ -255,16 +260,18 @@ func (r *resourceTFEDataRetentionPolicy) Read(ctx context.Context, req resource.
255260 var err error
256261 if state .WorkspaceID .IsNull () {
257262 policy , err = r .config .Client .Organizations .ReadDataRetentionPolicyChoice (ctx , state .Organization .ValueString ())
258- if err != nil {
259- resp .Diagnostics .AddError ("Failed to read data retention policy" , err .Error ())
260- return
261- }
262263 } else {
263264 policy , err = r .config .Client .Workspaces .ReadDataRetentionPolicyChoice (ctx , state .WorkspaceID .ValueString ())
264- if err != nil {
265- resp .Diagnostics .AddError ("Failed to read data retention policy" , err .Error ())
266- return
267- }
265+ }
266+ if err != nil {
267+ resp .Diagnostics .AddError ("Failed to read data retention policy" , err .Error ())
268+ return
269+ }
270+ // remove the policy from state if it no longer exists or has been replaced by another policy
271+ if policy == nil || r .getPolicyID (policy ) != state .ID .ValueString () {
272+ log .Printf ("[DEBUG] Data retention policy %s no longer exists" , state .ID )
273+ resp .State .RemoveResource (ctx )
274+ return
268275 }
269276 result , diags := modelFromTFEDataRetentionPolicyChoice (ctx , state , policy )
270277 if diags .HasError () {
0 commit comments