Skip to content

Commit 0170dfb

Browse files
committed
Add data retention policy block to organization resource
1 parent 7451b66 commit 0170dfb

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

internal/provider/resource_tfe_organization.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,21 @@ func resourceTFEOrganization() *schema.Resource {
9191
Type: schema.TypeString,
9292
Computed: true,
9393
},
94+
95+
"data_retention_policy": {
96+
Type: schema.TypeList,
97+
Optional: true,
98+
MinItems: 1,
99+
MaxItems: 1,
100+
Elem: &schema.Resource{
101+
Schema: map[string]*schema.Schema{
102+
"delete_older_than_n_days": {
103+
Type: schema.TypeInt,
104+
Required: true,
105+
},
106+
},
107+
},
108+
},
94109
},
95110
}
96111
}
@@ -115,6 +130,19 @@ func resourceTFEOrganizationCreate(d *schema.ResourceData, meta interface{}) err
115130

116131
d.SetId(org.Name)
117132

133+
if v, ok := d.GetOk("data_retention_policy"); ok {
134+
drp := v.([]interface{})[0].(map[string]interface{})
135+
136+
_, err = config.Client.Organizations.SetDataRetentionPolicy(ctx, org.Name, tfe.DataRetentionPolicySetOptions{
137+
DeleteOlderThanNDays: drp["delete_older_than_n_days"].(int),
138+
})
139+
140+
if err != nil {
141+
d.Partial(true)
142+
return fmt.Errorf("Error setting data retention policy on organization %s: %w", org.Name, err)
143+
}
144+
}
145+
118146
return resourceTFEOrganizationUpdate(d, meta)
119147
}
120148

@@ -150,6 +178,22 @@ func resourceTFEOrganizationRead(d *schema.ResourceData, meta interface{}) error
150178
d.Set("default_project_id", org.DefaultProject.ID)
151179
}
152180

181+
var dataRetentionPolicy []interface{}
182+
if org.DataRetentionPolicy != nil {
183+
policy, err := config.Client.Organizations.ReadDataRetentionPolicy(ctx, org.Name)
184+
185+
if err != nil {
186+
return fmt.Errorf(
187+
"Error getting data retention policy for organization %s: %w", org.Name, err)
188+
}
189+
190+
drp := map[string]interface{}{
191+
"delete_older_than_n_days": policy.DeleteOlderThanNDays,
192+
}
193+
dataRetentionPolicy = append(dataRetentionPolicy, drp)
194+
}
195+
d.Set("data_retention_policy", dataRetentionPolicy)
196+
153197
return nil
154198
}
155199

@@ -210,6 +254,25 @@ func resourceTFEOrganizationUpdate(d *schema.ResourceData, meta interface{}) err
210254

211255
d.SetId(org.Name)
212256

257+
// nolint: nestif
258+
if d.HasChange("data_retention_policy") {
259+
v, ok := d.GetOk("data_retention_policy")
260+
if ok {
261+
drp := v.([]interface{})[0].(map[string]interface{})
262+
_, err := config.Client.Organizations.SetDataRetentionPolicy(ctx, org.Name, tfe.DataRetentionPolicySetOptions{DeleteOlderThanNDays: drp["delete_older_than_n_days"].(int)})
263+
if err != nil {
264+
d.Partial(true)
265+
return fmt.Errorf("Error setting data retention policy on organization %s: %w", org.Name, err)
266+
}
267+
} else {
268+
err := config.Client.Organizations.DeleteDataRetentionPolicy(ctx, org.Name)
269+
if err != nil {
270+
d.Partial(true)
271+
return fmt.Errorf("Error deleting data retention policy from organization %s: %w", org.Name, err)
272+
}
273+
}
274+
}
275+
213276
return resourceTFEOrganizationRead(d, meta)
214277
}
215278

internal/provider/resource_tfe_organization_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,75 @@ func TestAccTFEOrganization_import(t *testing.T) {
251251
})
252252
}
253253

254+
func TestAccTFEOrganization_updateDataRetentionPolicy(t *testing.T) {
255+
skipIfCloud(t)
256+
257+
org := &tfe.Organization{}
258+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
259+
260+
resource.Test(t, resource.TestCase{
261+
PreCheck: func() {
262+
testAccPreCheck(t)
263+
},
264+
Providers: testAccProviders,
265+
CheckDestroy: testAccCheckTFEOrganizationDestroy,
266+
Steps: []resource.TestStep{
267+
{
268+
Config: testAccTFEOrganization_basic(rInt),
269+
Check: resource.ComposeTestCheckFunc(
270+
testAccCheckTFEOrganizationExists(
271+
"tfe_organization.foobar", org),
272+
resource.TestCheckNoResourceAttr("tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days"),
273+
),
274+
},
275+
{
276+
Config: testAccTFEOrganization_dataRetentionPolicy(rInt, 8),
277+
Check: resource.ComposeTestCheckFunc(
278+
resource.TestCheckResourceAttr(
279+
"tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days", "8"),
280+
),
281+
},
282+
{
283+
Config: testAccTFEOrganization_dataRetentionPolicy(rInt, 20),
284+
Check: resource.ComposeTestCheckFunc(
285+
resource.TestCheckResourceAttr(
286+
"tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days", "20"),
287+
),
288+
},
289+
{
290+
Config: testAccTFEOrganization_basic(rInt),
291+
Check: resource.ComposeTestCheckFunc(
292+
testAccCheckTFEOrganizationExists(
293+
"tfe_organization.foobar", org),
294+
resource.TestCheckNoResourceAttr("tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days"),
295+
),
296+
},
297+
},
298+
})
299+
}
300+
301+
func TestAccTFEOrganization_importDataRetentionPolicy(t *testing.T) {
302+
skipIfCloud(t)
303+
304+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
305+
306+
resource.Test(t, resource.TestCase{
307+
PreCheck: func() { testAccPreCheck(t) },
308+
Providers: testAccProviders,
309+
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
310+
Steps: []resource.TestStep{
311+
{
312+
Config: testAccTFEOrganization_dataRetentionPolicy(rInt, 10),
313+
},
314+
{
315+
ResourceName: "tfe_organization.foobar",
316+
ImportState: true,
317+
ImportStateVerify: true,
318+
},
319+
},
320+
})
321+
}
322+
254323
func testAccCheckTFEOrganizationExists(
255324
n string, org *tfe.Organization) resource.TestCheckFunc {
256325
return func(s *terraform.State) error {
@@ -434,3 +503,14 @@ resource "tfe_organization" "foobar" {
434503
allow_force_delete_workspaces = %t
435504
}`, orgName, orgEmail, costEstimationEnabled, assessmentsEnforced, allowForceDeleteWorkspaces)
436505
}
506+
507+
func testAccTFEOrganization_dataRetentionPolicy(rInt int, deleteOlderThanNDays int) string {
508+
return fmt.Sprintf(`
509+
resource "tfe_organization" "foobar" {
510+
name = "tst-terraform-%d"
511+
512+
data_retention_policy {
513+
delete_older_than_n_days = %d
514+
}
515+
}`, rInt, deleteOlderThanNDays)
516+
}

website/docs/r/organization.html.markdown

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ resource "tfe_organization" "test" {
2424

2525
The following arguments are supported:
2626

27+
* `data_retention_policy` - (Optional) The organization's [data retention policy](https://developer.hashicorp.com/terraform/enterprise/workspaces/settings/deletion#data-retention-policies).
2728
* `name` - (Required) Name of the organization.
2829
* `email` - (Required) Admin email address.
2930
* `session_timeout_minutes` - (Optional) Session timeout after inactivity.
@@ -38,6 +39,11 @@ The following arguments are supported:
3839
* `assessments_enforced` - (Optional) (Available only in Terraform Cloud) Whether to force health assessments (drift detection) on all eligible workspaces or allow workspaces to set their own preferences.
3940
* `allow_force_delete_workspaces` - (Optional) Whether workspace administrators are permitted to delete workspaces with resources under management. If false, only organization owners may delete these workspaces. Defaults to false.
4041

42+
The `data_retention_policy` block supports:
43+
44+
* `delete_older_than_n_days` - (Required) Automatically delete backing data for state versions and configuration versions older than N days.
45+
46+
4147
## Attributes Reference
4248

4349
* `id` - The name of the organization.

0 commit comments

Comments
 (0)