Skip to content

Commit 899752c

Browse files
Merge pull request #1385 from hashicorp/SwiftEngineer/TF-11539
Support for Data Retention Policies
2 parents 863e9f2 + 54ad3ca commit 899752c

File tree

6 files changed

+931
-0
lines changed

6 files changed

+931
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ ENHANCEMENTS:
1515
* `r/tfe_oauth_client`: Add `agent_pool_id` as an optional argument to enable Private VCS support, by @roleesinhaHC [1255](https://github.com/hashicorp/terraform-provider-tfe/pull/1255)
1616
* `r/tfe_project`: Increase the Project name length from 36 to 40 characters @hs26gill [#1351](https://github.com/hashicorp/terraform-provider-tfe/pull/1351)
1717

18+
FEATURES:
19+
* **New Resource**: `r/tfe_data_retention_policy` is a new resource for managing data retention policies for organizations and workspaces, by @SwiftEngineer [1385](https://github.com/hashicorp/terraform-provider-tfe/pull/1385)
20+
1821
BUG FIXES:
1922
* `r/tfe_registry_module`: Prevents constant diff after a successful apply when `tags` and `tests_enabled` is not set by @Uk1288 [#1357](https://github.com/hashicorp/terraform-provider-tfe/pull/1357)
2023

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"github.com/hashicorp/go-tfe"
6+
"github.com/hashicorp/terraform-plugin-framework/attr"
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
10+
"math/big"
11+
)
12+
13+
type modelTFEDataRetentionPolicy struct {
14+
ID types.String `tfsdk:"id"`
15+
Organization types.String `tfsdk:"organization"`
16+
WorkspaceID types.String `tfsdk:"workspace_id"`
17+
DeleteOlderThan types.Object `tfsdk:"delete_older_than"`
18+
DontDelete types.Object `tfsdk:"dont_delete"`
19+
}
20+
21+
type modelTFEDeleteOlderThan struct {
22+
Days types.Number `tfsdk:"days"`
23+
}
24+
25+
func (m modelTFEDeleteOlderThan) AttributeTypes() map[string]attr.Type {
26+
return map[string]attr.Type{
27+
"days": types.NumberType,
28+
}
29+
}
30+
31+
func DontDeleteEmptyObject() basetypes.ObjectValue {
32+
object, diags := types.ObjectValue(map[string]attr.Type{}, map[string]attr.Value{})
33+
if diags.HasError() {
34+
panic(diags.Errors())
35+
}
36+
return object
37+
}
38+
39+
func modelFromTFEDataRetentionPolicyDeleteOlder(ctx context.Context, model modelTFEDataRetentionPolicy, deleteOlder *tfe.DataRetentionPolicyDeleteOlder) (modelTFEDataRetentionPolicy, diag.Diagnostics) {
40+
deleteOlderThan := modelTFEDeleteOlderThan{
41+
Days: types.NumberValue(big.NewFloat(float64(deleteOlder.DeleteOlderThanNDays))),
42+
}
43+
deleteOlderThanObject, diags := types.ObjectValueFrom(ctx, deleteOlderThan.AttributeTypes(), deleteOlderThan)
44+
45+
organization := types.StringNull()
46+
if model.WorkspaceID.IsNull() {
47+
organization = model.Organization
48+
}
49+
50+
return modelTFEDataRetentionPolicy{
51+
ID: types.StringValue(deleteOlder.ID),
52+
Organization: organization,
53+
WorkspaceID: model.WorkspaceID,
54+
DeleteOlderThan: deleteOlderThanObject,
55+
DontDelete: types.ObjectNull(map[string]attr.Type{}),
56+
}, diags
57+
}
58+
59+
func modelFromTFEDataRetentionPolicyDontDelete(model modelTFEDataRetentionPolicy, dontDelete *tfe.DataRetentionPolicyDontDelete) modelTFEDataRetentionPolicy {
60+
organization := types.StringNull()
61+
if model.WorkspaceID.IsNull() {
62+
organization = model.Organization
63+
}
64+
65+
return modelTFEDataRetentionPolicy{
66+
ID: types.StringValue(dontDelete.ID),
67+
Organization: organization,
68+
WorkspaceID: model.WorkspaceID,
69+
DeleteOlderThan: types.ObjectNull(modelTFEDeleteOlderThan{}.AttributeTypes()),
70+
DontDelete: DontDeleteEmptyObject(),
71+
}
72+
}
73+
74+
func modelFromTFEDataRetentionPolicyChoice(ctx context.Context, model modelTFEDataRetentionPolicy, choice *tfe.DataRetentionPolicyChoice) (modelTFEDataRetentionPolicy, diag.Diagnostics) {
75+
if choice.DataRetentionPolicyDeleteOlder != nil {
76+
return modelFromTFEDataRetentionPolicyDeleteOlder(ctx, model, choice.DataRetentionPolicyDeleteOlder)
77+
}
78+
79+
var emptyDiag []diag.Diagnostic
80+
return modelFromTFEDataRetentionPolicyDontDelete(model, choice.DataRetentionPolicyDontDelete), emptyDiag
81+
}

internal/provider/provider_next.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ func (p *frameworkProvider) Resources(ctx context.Context) []func() resource.Res
140140
NewRegistryGPGKeyResource,
141141
NewRegistryProviderResource,
142142
NewResourceVariable,
143+
NewDataRetentionPolicyResource,
143144
NewResourceWorkspaceSettings,
144145
NewSAMLSettingsResource,
145146
NewTestVariableResource,

0 commit comments

Comments
 (0)