Skip to content

Commit

Permalink
Merge pull request #975 from hashicorp/TF-20366-unlocking-a-workspace…
Browse files Browse the repository at this point in the history
…-that-is-locked-by-a-team-or-user-returns-the-wrong-error

[TF-20366] Return correct error message when workspace is locked by a Team or User
  • Loading branch information
Maed223 authored Sep 17, 2024
2 parents bd1cdb9 + 109d3f1 commit c4d3b14
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ var (
// ErrWorkspaceLockedByRun is returned when trying to unlock a workspace locked by a run.
ErrWorkspaceLockedByRun = errors.New("unable to unlock workspace locked by run")

// ErrWorkspaceLockedByTeam is returned when trying to unlock a workspace locked by a team.
ErrWorkspaceLockedByTeam = errors.New("unable to unlock workspace locked by team")

// ErrWorkspaceLockedByUser is returned when trying to unlock a workspace locked by a user.
ErrWorkspaceLockedByUser = errors.New("unable to unlock workspace locked by user")

// ErrWorkspaceStillProcessing is returned when a workspace is still processing state
// to determine if it is safe to delete. "conflict" followed by newline is used to
// preserve go-tfe version compatibility with the error constructed at runtime before it was
Expand Down
8 changes: 8 additions & 0 deletions tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,14 @@ func checkResponseCode(r *http.Response) error {
return ErrWorkspaceLockedByRun
}

if errorPayloadContains(errs, "is locked by Team") {
return ErrWorkspaceLockedByTeam
}

if errorPayloadContains(errs, "is locked by User") {
return ErrWorkspaceLockedByUser
}

return ErrWorkspaceNotLocked
case strings.HasSuffix(r.Request.URL.Path, "actions/force-unlock"):
return ErrWorkspaceNotLocked
Expand Down
35 changes: 35 additions & 0 deletions workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2029,6 +2029,41 @@ func TestWorkspacesUnlock(t *testing.T) {
assert.Equal(t, ErrWorkspaceLockedByRun, err)
})

t.Run("when a workspace is locked by a team", func(t *testing.T) {
wTest2, wTest2Cleanup := createWorkspace(t, client, orgTest)
t.Cleanup(wTest2Cleanup)

// Create a new team to lock the workspace
tmTest, tmTestCleanup := createTeam(t, client, orgTest)
defer tmTestCleanup()
ta, err := client.TeamAccess.Add(ctx, TeamAccessAddOptions{
Access: Access(AccessAdmin),
Team: tmTest,
Workspace: wTest2,
})
assert.Nil(t, err)
defer func() {
err := client.TeamAccess.Remove(ctx, ta.ID)
if err != nil {
t.Logf("error removing team access (%s): %s", ta.ID, err)
}
}()
tt, ttTestCleanup := createTeamToken(t, client, tmTest)
defer ttTestCleanup()

// Create a new client with the team token
teamClient := testClient(t)
teamClient.token = tt.Token

// Lock the workspace with the team client
_, err = teamClient.Workspaces.Lock(ctx, wTest2.ID, WorkspaceLockOptions{})
assert.Nil(t, err)

// Attempt to unlock the workspace with the original client
_, err = client.Workspaces.Unlock(ctx, wTest2.ID)
assert.Equal(t, ErrWorkspaceLockedByTeam, err)
})

t.Run("without a valid workspace ID", func(t *testing.T) {
w, err := client.Workspaces.Unlock(ctx, badIdentifier)
assert.Nil(t, w)
Expand Down

0 comments on commit c4d3b14

Please sign in to comment.