Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data retention policy #1170

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ FEATURES:
* **New Resource**: `r/tfe_registry_gpg_key` is a new resource for managing private registry GPG keys, by @tmatilai [1160](https://github.com/hashicorp/terraform-provider-tfe/pull/1160)
* **New Data Source**: `d/tfe_registry_gpg_key` is a new data source to retrieve a private registry GPG key, by @tmatilai [1160](https://github.com/hashicorp/terraform-provider-tfe/pull/1160)
* **New Data Source**: `d/tfe_registry_gpg_keys` is a new data source to retrieve all private registry GPG keys of an organization, by @tmatilai [1160](https://github.com/hashicorp/terraform-provider-tfe/pull/1160)
* `r/tfe_workspace`: Add `data_retention_policy` attribute to allow configuration of a workspace's data retention policy, by @mwudka [1170](https://github.com/hashicorp/terraform-provider-tfe/pull/1170)
* `r/tfe_organization`: Add `data_retention_policy` attribute to allow configuration of an organization's data retention policy, by @mwudka [1170](https://github.com/hashicorp/terraform-provider-tfe/pull/1170)

ENHANCEMENTS:
* `d/tfe_organization`: Make `name` argument optional if configured for the provider, by @tmatilai [1133](https://github.com/hashicorp/terraform-provider-tfe/pull/1133)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/hashicorp/go-slug v0.13.2
github.com/hashicorp/go-tfe v1.40.0
github.com/hashicorp/go-slug v0.13.3
github.com/hashicorp/go-tfe v1.41.0
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/hcl v1.0.0
github.com/hashicorp/hcl/v2 v2.19.1 // indirect
Expand All @@ -30,7 +30,7 @@ require (
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.4.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)

Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ github.com/hashicorp/go-plugin v1.6.0 h1:wgd4KxHJTVGGqWBq4QPB1i5BZNEx9BR8+OFmHDm
github.com/hashicorp/go-plugin v1.6.0/go.mod h1:lBS5MtSSBZk0SHc66KACcjjlU6WzEVP/8pwz68aMkCI=
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/hashicorp/go-slug v0.13.2 h1:ArlarJ8w1Rinx4P1N6Sr00t+GmjJWtYZuSEDYPQBErA=
github.com/hashicorp/go-slug v0.13.2/go.mod h1:RA4C+ezyC2nDsiPM5+1djqagveBBJdSN/fM2QCUziYQ=
github.com/hashicorp/go-tfe v1.40.0 h1:xgR/ijk5XAAo4jQHIOrKnIJrc4iM0jjYPskKgf4eDTU=
github.com/hashicorp/go-tfe v1.40.0/go.mod h1:pc7+wHCH26BaoFP5txiKkM7Coi5PmB9VwNfnnT2XpKE=
github.com/hashicorp/go-slug v0.13.3 h1:JiYNpOkD0HmMWw/lNYiBAUD6+WIBIV7UftKiqIbpNqM=
github.com/hashicorp/go-slug v0.13.3/go.mod h1:RA4C+ezyC2nDsiPM5+1djqagveBBJdSN/fM2QCUziYQ=
github.com/hashicorp/go-tfe v1.41.0 h1:ieQrbFwH4KITMxqyBni7v5jTBkcvQqCbnPNsV2eh4TY=
github.com/hashicorp/go-tfe v1.41.0/go.mod h1:i29eWGEtI/glZaixaMTVUipNWxQFN6gw7hy3swKOC/A=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down Expand Up @@ -239,8 +239,8 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.4.0 h1:Z81tqI5ddIoXDPvVQ7/7CC9TnLM7ubaFG2qXYd5BbYY=
golang.org/x/time v0.4.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk=
golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
Expand Down
12 changes: 12 additions & 0 deletions internal/provider/client_mock_workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,15 @@ func (m *mockWorkspaces) SafeDelete(ctx context.Context, organization string, wo
func (m *mockWorkspaces) SafeDeleteByID(ctx context.Context, workspaceID string) error {
panic("not implemented")
}

func (m *mockWorkspaces) ReadDataRetentionPolicy(ctx context.Context, workspaceID string) (*tfe.DataRetentionPolicy, error) {
panic("not implemented")
}

func (m *mockWorkspaces) SetDataRetentionPolicy(ctx context.Context, workspaceID string, options tfe.DataRetentionPolicySetOptions) (*tfe.DataRetentionPolicy, error) {
panic("not implemented")
}

func (m *mockWorkspaces) DeleteDataRetentionPolicy(ctx context.Context, workspaceID string) error {
panic("not implemented")
}
63 changes: 63 additions & 0 deletions internal/provider/resource_tfe_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ func resourceTFEOrganization() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},

"data_retention_policy": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_older_than_n_days": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
}
}
Expand All @@ -115,6 +130,19 @@ func resourceTFEOrganizationCreate(d *schema.ResourceData, meta interface{}) err

d.SetId(org.Name)

if v, ok := d.GetOk("data_retention_policy"); ok {
drp := v.([]interface{})[0].(map[string]interface{})

_, err = config.Client.Organizations.SetDataRetentionPolicy(ctx, org.Name, tfe.DataRetentionPolicySetOptions{
DeleteOlderThanNDays: drp["delete_older_than_n_days"].(int),
})

if err != nil {
d.Partial(true)
return fmt.Errorf("Error setting data retention policy on organization %s: %w", org.Name, err)
}
}

return resourceTFEOrganizationUpdate(d, meta)
}

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

var dataRetentionPolicy []interface{}
if org.DataRetentionPolicy != nil {
policy, err := config.Client.Organizations.ReadDataRetentionPolicy(ctx, org.Name)

if err != nil {
return fmt.Errorf(
"Error getting data retention policy for organization %s: %w", org.Name, err)
}

drp := map[string]interface{}{
"delete_older_than_n_days": policy.DeleteOlderThanNDays,
}
dataRetentionPolicy = append(dataRetentionPolicy, drp)
}
d.Set("data_retention_policy", dataRetentionPolicy)

return nil
}

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

d.SetId(org.Name)

// nolint: nestif
if d.HasChange("data_retention_policy") {
v, ok := d.GetOk("data_retention_policy")
if ok {
drp := v.([]interface{})[0].(map[string]interface{})
_, err := config.Client.Organizations.SetDataRetentionPolicy(ctx, org.Name, tfe.DataRetentionPolicySetOptions{DeleteOlderThanNDays: drp["delete_older_than_n_days"].(int)})
if err != nil {
d.Partial(true)
return fmt.Errorf("Error setting data retention policy on organization %s: %w", org.Name, err)
}
} else {
err := config.Client.Organizations.DeleteDataRetentionPolicy(ctx, org.Name)
if err != nil {
d.Partial(true)
return fmt.Errorf("Error deleting data retention policy from organization %s: %w", org.Name, err)
}
}
}

return resourceTFEOrganizationRead(d, meta)
}

Expand Down
80 changes: 80 additions & 0 deletions internal/provider/resource_tfe_organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,75 @@ func TestAccTFEOrganization_import(t *testing.T) {
})
}

func TestAccTFEOrganization_updateDataRetentionPolicy(t *testing.T) {
skipIfCloud(t)

org := &tfe.Organization{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEOrganizationDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganization_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEOrganizationExists(
"tfe_organization.foobar", org),
resource.TestCheckNoResourceAttr("tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days"),
),
},
{
Config: testAccTFEOrganization_dataRetentionPolicy(rInt, 8),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days", "8"),
),
},
{
Config: testAccTFEOrganization_dataRetentionPolicy(rInt, 20),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days", "20"),
),
},
{
Config: testAccTFEOrganization_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEOrganizationExists(
"tfe_organization.foobar", org),
resource.TestCheckNoResourceAttr("tfe_organization.foobar", "data_retention_policy.0.delete_older_than_n_days"),
),
},
},
})
}

func TestAccTFEOrganization_importDataRetentionPolicy(t *testing.T) {
skipIfCloud(t)

rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEWorkspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganization_dataRetentionPolicy(rInt, 10),
},
{
ResourceName: "tfe_organization.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckTFEOrganizationExists(
n string, org *tfe.Organization) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -434,3 +503,14 @@ resource "tfe_organization" "foobar" {
allow_force_delete_workspaces = %t
}`, orgName, orgEmail, costEstimationEnabled, assessmentsEnforced, allowForceDeleteWorkspaces)
}

func testAccTFEOrganization_dataRetentionPolicy(rInt int, deleteOlderThanNDays int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "[email protected]"
data_retention_policy {
delete_older_than_n_days = %d
}
}`, rInt, deleteOlderThanNDays)
}
62 changes: 62 additions & 0 deletions internal/provider/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ func resourceTFEWorkspace() *schema.Resource {
Default: "",
},

"data_retention_policy": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"delete_older_than_n_days": {
Type: schema.TypeInt,
Required: true,
},
},
},
},

"vcs_repo": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -428,6 +443,18 @@ func resourceTFEWorkspaceCreate(d *schema.ResourceData, meta interface{}) error

d.SetId(workspace.ID)

if v, ok := d.GetOk("data_retention_policy"); ok {
drp := v.([]interface{})[0].(map[string]interface{})

_, err = config.Client.Workspaces.SetDataRetentionPolicy(ctx, workspace.ID, tfe.DataRetentionPolicySetOptions{
DeleteOlderThanNDays: drp["delete_older_than_n_days"].(int),
})

if err != nil {
return fmt.Errorf("Error setting data retention policy on workspace %s: %w", name, err)
}
}

if sshKeyID, ok := d.GetOk("ssh_key_id"); ok {
_, err = config.Client.Workspaces.AssignSSHKey(ctx, workspace.ID, tfe.WorkspaceAssignSSHKeyOptions{
SSHKeyID: tfe.String(sshKeyID.(string)),
Expand Down Expand Up @@ -542,6 +569,22 @@ func resourceTFEWorkspaceRead(d *schema.ResourceData, meta interface{}) error {

d.Set("vcs_repo", vcsRepo)

var dataRetentionPolicy []interface{}
if workspace.DataRetentionPolicy != nil {
policy, err := config.Client.Workspaces.ReadDataRetentionPolicy(ctx, id)

if err != nil {
return fmt.Errorf(
"Error data retention policy for workspace %s: %w", id, err)
}

drp := map[string]interface{}{
"delete_older_than_n_days": policy.DeleteOlderThanNDays,
}
dataRetentionPolicy = append(dataRetentionPolicy, drp)
}
d.Set("data_retention_policy", dataRetentionPolicy)

if workspace.GlobalRemoteState {
d.Set("global_remote_state", true)
} else {
Expand Down Expand Up @@ -734,6 +777,25 @@ func resourceTFEWorkspaceUpdate(d *schema.ResourceData, meta interface{}) error
}
}

// nolint: nestif
if d.HasChange("data_retention_policy") {
v, ok := d.GetOk("data_retention_policy")
if ok {
drp := v.([]interface{})[0].(map[string]interface{})
_, err := config.Client.Workspaces.SetDataRetentionPolicy(ctx, id, tfe.DataRetentionPolicySetOptions{DeleteOlderThanNDays: drp["delete_older_than_n_days"].(int)})
if err != nil {
d.Partial(true)
return fmt.Errorf("Error setting data retention policy on workspace %s: %w", d.Id(), err)
}
} else {
err := config.Client.Workspaces.DeleteDataRetentionPolicy(ctx, id)
if err != nil {
d.Partial(true)
return fmt.Errorf("Error deleting data retention policy from workspace %s: %w", d.Id(), err)
}
}
}

if d.HasChange("tag_names") {
oldTagNameValues, newTagNameValues := d.GetChange("tag_names")
newTagNamesSet := newTagNameValues.(*schema.Set)
Expand Down
Loading