Skip to content

Commit c4d3b14

Browse files
authored
Merge pull request #975 from hashicorp/TF-20366-unlocking-a-workspace-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
2 parents bd1cdb9 + 109d3f1 commit c4d3b14

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ var (
6363
// ErrWorkspaceLockedByRun is returned when trying to unlock a workspace locked by a run.
6464
ErrWorkspaceLockedByRun = errors.New("unable to unlock workspace locked by run")
6565

66+
// ErrWorkspaceLockedByTeam is returned when trying to unlock a workspace locked by a team.
67+
ErrWorkspaceLockedByTeam = errors.New("unable to unlock workspace locked by team")
68+
69+
// ErrWorkspaceLockedByUser is returned when trying to unlock a workspace locked by a user.
70+
ErrWorkspaceLockedByUser = errors.New("unable to unlock workspace locked by user")
71+
6672
// ErrWorkspaceStillProcessing is returned when a workspace is still processing state
6773
// to determine if it is safe to delete. "conflict" followed by newline is used to
6874
// preserve go-tfe version compatibility with the error constructed at runtime before it was

tfe.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,14 @@ func checkResponseCode(r *http.Response) error {
973973
return ErrWorkspaceLockedByRun
974974
}
975975

976+
if errorPayloadContains(errs, "is locked by Team") {
977+
return ErrWorkspaceLockedByTeam
978+
}
979+
980+
if errorPayloadContains(errs, "is locked by User") {
981+
return ErrWorkspaceLockedByUser
982+
}
983+
976984
return ErrWorkspaceNotLocked
977985
case strings.HasSuffix(r.Request.URL.Path, "actions/force-unlock"):
978986
return ErrWorkspaceNotLocked

workspace_integration_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,41 @@ func TestWorkspacesUnlock(t *testing.T) {
20292029
assert.Equal(t, ErrWorkspaceLockedByRun, err)
20302030
})
20312031

2032+
t.Run("when a workspace is locked by a team", func(t *testing.T) {
2033+
wTest2, wTest2Cleanup := createWorkspace(t, client, orgTest)
2034+
t.Cleanup(wTest2Cleanup)
2035+
2036+
// Create a new team to lock the workspace
2037+
tmTest, tmTestCleanup := createTeam(t, client, orgTest)
2038+
defer tmTestCleanup()
2039+
ta, err := client.TeamAccess.Add(ctx, TeamAccessAddOptions{
2040+
Access: Access(AccessAdmin),
2041+
Team: tmTest,
2042+
Workspace: wTest2,
2043+
})
2044+
assert.Nil(t, err)
2045+
defer func() {
2046+
err := client.TeamAccess.Remove(ctx, ta.ID)
2047+
if err != nil {
2048+
t.Logf("error removing team access (%s): %s", ta.ID, err)
2049+
}
2050+
}()
2051+
tt, ttTestCleanup := createTeamToken(t, client, tmTest)
2052+
defer ttTestCleanup()
2053+
2054+
// Create a new client with the team token
2055+
teamClient := testClient(t)
2056+
teamClient.token = tt.Token
2057+
2058+
// Lock the workspace with the team client
2059+
_, err = teamClient.Workspaces.Lock(ctx, wTest2.ID, WorkspaceLockOptions{})
2060+
assert.Nil(t, err)
2061+
2062+
// Attempt to unlock the workspace with the original client
2063+
_, err = client.Workspaces.Unlock(ctx, wTest2.ID)
2064+
assert.Equal(t, ErrWorkspaceLockedByTeam, err)
2065+
})
2066+
20322067
t.Run("without a valid workspace ID", func(t *testing.T) {
20332068
w, err := client.Workspaces.Unlock(ctx, badIdentifier)
20342069
assert.Nil(t, w)

0 commit comments

Comments
 (0)