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

[#TF-723] proactively check for RUM before safe delete #699

Merged
merged 1 commit into from
Nov 21, 2022
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased
FEATURES:
* r/tfe_workspace: Add preemptive check for resources under management when `force_delete` attribute is false ([#699](https://github.com/hashicorp/terraform-provider-tfe/pull/699))

## v0.39.0 (November 18, 2022)

FEATURES:
Expand Down
16 changes: 16 additions & 0 deletions tfe/resource_tfe_workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,10 @@ func resourceTFEWorkspaceDelete(d *schema.ResourceData, meta interface{}) error
if forceDelete {
err = tfeClient.Workspaces.DeleteByID(ctx, id)
} else {
err = errWorkspaceResourceCountCheck(id, ws.ResourceCount)
if err != nil {
return err
}
err = tfeClient.Workspaces.SafeDeleteByID(ctx, id)
return errWorkspaceSafeDeleteWithPermission(id, err)
}
Expand All @@ -708,6 +712,10 @@ func resourceTFEWorkspaceDelete(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf(
"Error deleting workspace %s: missing required permissions to set force delete workspaces in the organization.", id)
}
err = errWorkspaceResourceCountCheck(id, ws.ResourceCount)
if err != nil {
return err
}
err = tfeClient.Workspaces.SafeDeleteByID(ctx, id)
}

Expand Down Expand Up @@ -814,3 +822,11 @@ func errWorkspaceSafeDeleteWithPermission(workspaceID string, err error) error {
}
return nil
}

func errWorkspaceResourceCountCheck(workspaceID string, resourceCount int) error {
if resourceCount > 0 {
return fmt.Errorf(
"Error deleting workspace %s: This workspace has %v resources under management and must be force deleted by setting force_delete = true", workspaceID, resourceCount)
}
return nil
}
8 changes: 8 additions & 0 deletions tfe/resource_tfe_workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,7 @@ func TestTFEWorkspace_delete_withoutCanForceDeletePermission(t *testing.T) {
t.Fatalf("unexpected err creating mock workspace %v", err)
}
workspace.Permissions.CanForceDelete = nil
workspace.ResourceCount = 2

rd := resourceTFEWorkspace().TestResourceData()
rd.SetId(workspace.ID)
Expand All @@ -1925,6 +1926,13 @@ func TestTFEWorkspace_delete_withoutCanForceDeletePermission(t *testing.T) {
t.Fatalf("unexpected err creating configuration state %v", err)
}

err = resourceTFEWorkspaceDelete(rd, client)
if err == nil {
t.Fatalf("Expected an error deleting workspace with CanForceDelete=nil, force_delete=true, and %v resources", workspace.ResourceCount)
}

workspace.ResourceCount = 0

err = resourceTFEWorkspaceDelete(rd, client)
if err == nil {
t.Fatalf("Expected an error deleting workspace with CanForceDelete=nil and force_delete=false")
Expand Down