Skip to content

Commit 89d70db

Browse files
committed
add additional tests and implement update for data retention policy resource
1 parent e58bb23 commit 89d70db

File tree

2 files changed

+136
-52
lines changed

2 files changed

+136
-52
lines changed

internal/provider/resource_tfe_data_retention_policy.go

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/hashicorp/terraform-plugin-framework-validators/objectvalidator"
1717
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1818
"github.com/hashicorp/terraform-plugin-framework/types"
19+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/numberplanmodifier"
1920
)
2021

2122
// Ensure provider defined types fully satisfy framework interfaces.
@@ -57,10 +58,17 @@ func (r *resourceTFEDataRetentionPolicy) Schema(ctx context.Context, req resourc
5758
"organization": schema.StringAttribute{
5859
Description: "Name of the organization. If omitted, organization must be defined in the provider config.",
5960
Optional: true,
61+
Computed: true,
62+
PlanModifiers: []planmodifier.String{
63+
stringplanmodifier.RequiresReplace(),
64+
},
6065
},
6166
"workspace_id": schema.StringAttribute{
6267
Description: "ID of the workspace that the data retention policy should apply to. If omitted, the data retention policy will apply to the entire organization.",
6368
Optional: true,
69+
PlanModifiers: []planmodifier.String{
70+
stringplanmodifier.RequiresReplace(),
71+
},
6472
},
6573
},
6674
Blocks: map[string]schema.Block{
@@ -70,6 +78,9 @@ func (r *resourceTFEDataRetentionPolicy) Schema(ctx context.Context, req resourc
7078
"days": schema.NumberAttribute{
7179
Description: "Number of days",
7280
Required: true,
81+
PlanModifiers: []planmodifier.Number{
82+
numberplanmodifier.RequiresReplace(),
83+
},
7384
},
7485
},
7586
Validators: []validator.Object{
@@ -250,41 +261,46 @@ func (r *resourceTFEDataRetentionPolicy) Update(ctx context.Context, req resourc
250261
}
251262

252263
func (r *resourceTFEDataRetentionPolicy) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
253-
//var state modelTFERegistryGPGKey
254-
//
255-
//// Read Terraform prior state data into the model
256-
//resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
257-
//
258-
//if resp.Diagnostics.HasError() {
259-
// return
260-
//}
261-
//
262-
//keyID := tfe.GPGKeyID{
263-
// RegistryName: "private",
264-
// Namespace: state.Organization.ValueString(),
265-
// KeyID: state.ID.ValueString(),
266-
//}
267-
//
268-
//tflog.Debug(ctx, "Deleting private registry GPG key")
269-
//err := r.config.Client.GPGKeys.Delete(ctx, keyID)
270-
//if err != nil {
271-
// resp.Diagnostics.AddError("Unable to delete private registry GPG key", err.Error())
272-
// return
273-
//}
264+
var state modelTFEDataRetentionPolicy
265+
266+
// Read Terraform prior state data into the model
267+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
268+
269+
if resp.Diagnostics.HasError() {
270+
return
271+
}
272+
273+
if state.WorkspaceId.IsNull() {
274+
tflog.Debug(ctx, fmt.Sprintf("Deleting data retention policy for organization: %s", state.Organization))
275+
err := r.config.Client.Organizations.DeleteDataRetentionPolicy(ctx, state.Organization.ValueString())
276+
if err != nil {
277+
resp.Diagnostics.AddError(fmt.Sprintf("Deleting data retention policy for organization: %s", state.Organization), err.Error())
278+
return
279+
}
280+
} else {
281+
tflog.Debug(ctx, fmt.Sprintf("Deleting data retention policy for workspace: %s", state.WorkspaceId))
282+
err := r.config.Client.Workspaces.DeleteDataRetentionPolicy(ctx, state.WorkspaceId.ValueString())
283+
if err != nil {
284+
resp.Diagnostics.AddError(fmt.Sprintf("Deleting data retention policy for workspace: %s", state.WorkspaceId), err.Error())
285+
return
286+
}
287+
}
288+
274289
}
275290

276291
func (r *resourceTFEDataRetentionPolicy) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
277292
s := strings.SplitN(req.ID, "/", 2)
278-
if len(s) != 2 {
293+
294+
if len(s) != 2 && len(s) != 1 {
279295
resp.Diagnostics.AddError(
280296
"Error importing variable",
281-
fmt.Sprintf("Invalid variable import format: %s (expected <ORGANIZATION>/<KEY ID>)", req.ID),
297+
fmt.Sprintf("Invalid variable import format: %s (expected <ORGANIZATION>/<WORKSPACE ID> or <ORGANIZATION>)", req.ID),
282298
)
283299
return
284300
}
285301
org := s[0]
286-
id := s[1]
302+
wsId := s[1]
287303

288304
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("organization"), org)...)
289-
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
305+
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("workspace_id"), wsId)...)
290306
}

internal/provider/resource_tfe_data_retention_policy_test.go

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1111
"fmt"
12+
"os"
1213
)
1314

1415
func TestAccTFEDataRetentionPolicy_basic(t *testing.T) {
@@ -21,10 +22,9 @@ func TestAccTFEDataRetentionPolicy_basic(t *testing.T) {
2122
CheckDestroy: testAccCheckTFEDataRetentionPolicyDestroy,
2223
Steps: []resource.TestStep{
2324
{
24-
Config: testAccTFEDataRetentionPolicy_basic(rInt),
25+
Config: testAccTFEDataRetentionPolicy_basic(rInt, 42),
2526
Check: resource.ComposeTestCheckFunc(
2627
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
27-
testAccCheckTFEDataRetentionPolicyAttributes(policy),
2828
resource.TestCheckResourceAttr(
2929
"tfe_data_retention_policy.foobar", "delete_older_than.days", "42"),
3030
),
@@ -33,7 +33,68 @@ func TestAccTFEDataRetentionPolicy_basic(t *testing.T) {
3333
})
3434
}
3535

36-
func testAccTFEDataRetentionPolicy_basic(rInt int) string {
36+
func TestAccTFEDataRetentionPolicy_update(t *testing.T) {
37+
policy := &tfe.DataRetentionPolicyChoice{}
38+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
39+
40+
resource.Test(t, resource.TestCase{
41+
PreCheck: func() { testAccPreCheck(t) },
42+
ProtoV5ProviderFactories: testAccMuxedProviders,
43+
CheckDestroy: testAccCheckTFEDataRetentionPolicyDestroy,
44+
Steps: []resource.TestStep{
45+
{
46+
Config: testAccTFEDataRetentionPolicy_basic(rInt, 42),
47+
Check: resource.ComposeTestCheckFunc(
48+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
49+
resource.TestCheckResourceAttr(
50+
"tfe_data_retention_policy.foobar", "delete_older_than.days", "42"),
51+
),
52+
},
53+
54+
{
55+
Config: testAccTFEDataRetentionPolicy_basic(rInt, 1337),
56+
Check: resource.ComposeTestCheckFunc(
57+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
58+
resource.TestCheckResourceAttr(
59+
"tfe_data_retention_policy.foobar", "delete_older_than.days", "1337"),
60+
),
61+
},
62+
},
63+
})
64+
}
65+
66+
func TestAccTFEDataRetentionPolicy_organization_level(t *testing.T) {
67+
policy := &tfe.DataRetentionPolicyChoice{}
68+
defaultOrgName, _ := setupDefaultOrganization(t)
69+
70+
os.Setenv("TFE_ORGANIZATION", defaultOrgName)
71+
72+
resource.Test(t, resource.TestCase{
73+
PreCheck: func() { testAccPreCheck(t) },
74+
ProtoV5ProviderFactories: testAccMuxedProviders,
75+
CheckDestroy: testAccCheckTFEDataRetentionPolicyDestroy,
76+
Steps: []resource.TestStep{
77+
{
78+
Config: testAccTFEDataRetentionPolicy_implicit_organization(42),
79+
Check: resource.ComposeTestCheckFunc(
80+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
81+
resource.TestCheckResourceAttr(
82+
"tfe_data_retention_policy.foobar", "delete_older_than.days", "42"),
83+
),
84+
},
85+
{
86+
Config: testAccTFEDataRetentionPolicy_implicit_organization(1337),
87+
Check: resource.ComposeTestCheckFunc(
88+
testAccCheckTFEDataRetentionPolicyExists("tfe_data_retention_policy.foobar", policy),
89+
resource.TestCheckResourceAttr(
90+
"tfe_data_retention_policy.foobar", "delete_older_than.days", "1337"),
91+
),
92+
},
93+
},
94+
})
95+
}
96+
97+
func testAccTFEDataRetentionPolicy_basic(rInt int, deleteOlderThan int) string {
3798
return fmt.Sprintf(`
3899
resource "tfe_organization" "foobar" {
39100
name = "tst-terraform-%d"
@@ -49,9 +110,18 @@ resource "tfe_data_retention_policy" "foobar" {
49110
workspace_id = tfe_workspace.foobar.id
50111
51112
delete_older_than {
52-
days = 42
113+
days = %d
53114
}
54-
}`, rInt)
115+
}`, rInt, deleteOlderThan)
116+
}
117+
118+
func testAccTFEDataRetentionPolicy_implicit_organization(deleteOlderThan int) string {
119+
return fmt.Sprintf(`
120+
resource "tfe_data_retention_policy" "foobar" {
121+
delete_older_than {
122+
days = %d
123+
}
124+
}`, deleteOlderThan)
55125
}
56126

57127
func testAccCheckTFEDataRetentionPolicyExists(
@@ -69,33 +139,31 @@ func testAccCheckTFEDataRetentionPolicyExists(
69139
}
70140

71141
wsID := rs.Primary.Attributes["workspace_id"]
72-
ws, err := config.Client.Workspaces.ReadByID(ctx, wsID)
73-
if err != nil {
74-
return fmt.Errorf(
75-
"Error retrieving workspace %s: %w", wsID, err)
76-
}
77142

78-
drp, err := config.Client.Workspaces.ReadDataRetentionPolicyChoice(ctx, ws.ID)
79-
if err != nil {
80-
return fmt.Errorf(
81-
"Error retrieving data retention policy for workspace %s: %w", ws.ID, err)
82-
}
143+
if wsID != "" {
144+
ws, err := config.Client.Workspaces.ReadByID(ctx, wsID)
145+
if err != nil {
146+
return fmt.Errorf(
147+
"Error retrieving workspace %s: %w", wsID, err)
148+
}
83149

84-
*policy = *drp
150+
drp, err := config.Client.Workspaces.ReadDataRetentionPolicyChoice(ctx, ws.ID)
151+
if err != nil {
152+
return fmt.Errorf(
153+
"Error retrieving data retention policy for workspace %s: %w", ws.ID, err)
154+
}
85155

86-
return nil
87-
}
88-
}
156+
*policy = *drp
157+
} else {
158+
orgName := rs.Primary.Attributes["organization"]
89159

90-
func testAccCheckTFEDataRetentionPolicyAttributes(
91-
policy *tfe.DataRetentionPolicyChoice) resource.TestCheckFunc {
92-
return func(s *terraform.State) error {
93-
if policy.DataRetentionPolicyDeleteOlder == nil {
94-
return fmt.Errorf("policy wasn't of type 'delete_older'")
95-
}
160+
drp, err := config.Client.Organizations.ReadDataRetentionPolicyChoice(ctx, orgName)
161+
if err != nil {
162+
return fmt.Errorf(
163+
"Error retrieving data retention policy for organization %s: %w", orgName, err)
164+
}
96165

97-
if policy.DataRetentionPolicyDeleteOlder.DeleteOlderThanNDays != 42 {
98-
return fmt.Errorf("bad delete_older_than_n_days: %d", policy.DataRetentionPolicyDeleteOlder.DeleteOlderThanNDays)
166+
*policy = *drp
99167
}
100168

101169
return nil

0 commit comments

Comments
 (0)