Skip to content

Commit 89fb64c

Browse files
committed
handle drift when policy is replaced or removed
1 parent 106e414 commit 89fb64c

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

internal/provider/resource_tfe_data_retention_policy.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

internal/provider/resource_tfe_data_retention_policy_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,47 @@ func TestAccTFEDataRetentionPolicy_explicit_organization(t *testing.T) {
116116
})
117117
}
118118

119+
func TestAccTFEDataRetentionPolicy_update_type(t *testing.T) {
120+
skipIfCloud(t)
121+
122+
policy := &tfe.DataRetentionPolicyChoice{}
123+
defaultOrgName, _ := setupDefaultOrganization(t)
124+
125+
os.Setenv("TFE_ORGANIZATION", defaultOrgName)
126+
127+
resource.Test(t, resource.TestCase{
128+
PreCheck: func() { testAccPreCheck(t) },
129+
ProtoV5ProviderFactories: testAccMuxedProviders,
130+
CheckDestroy: testAccCheckTFEDataRetentionPolicyDestroy,
131+
Steps: []resource.TestStep{
132+
{
133+
Config: testAccTFEDataRetentionPolicy_implicit_organization(42),
134+
Check: resource.ComposeTestCheckFunc(
135+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
136+
resource.TestCheckResourceAttr(
137+
"tfe_data_retention_policy.foobar", "delete_older_than.days", "42"),
138+
),
139+
},
140+
{
141+
Config: testAccTFEDataRetentionPolicy_dontDelete_implicit_organization(),
142+
Check: resource.ComposeTestCheckFunc(
143+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
144+
resource.TestCheckResourceAttr(
145+
"tfe_data_retention_policy.foobar", "organization", defaultOrgName),
146+
),
147+
},
148+
{
149+
Config: testAccTFEDataRetentionPolicy_implicit_organization(42),
150+
Check: resource.ComposeTestCheckFunc(
151+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
152+
resource.TestCheckResourceAttr(
153+
"tfe_data_retention_policy.foobar", "delete_older_than.days", "42"),
154+
),
155+
},
156+
},
157+
})
158+
}
159+
119160
func TestAccTFEDataRetentionPolicy_implicit_organization(t *testing.T) {
120161
skipIfCloud(t)
121162

0 commit comments

Comments
 (0)