Skip to content

Commit

Permalink
Merge pull request #978 from hashicorp/TF-20458-improve-hcp-terraform…
Browse files Browse the repository at this point in the history
…-workspace-unlock-retry-messaging-in-terraform-cli

Adds ErrWorkspaceLockedStateVersionStillPending
  • Loading branch information
brandonc authored Sep 23, 2024
2 parents ec9ee35 + fc4e1bf commit 3d29602
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# UNRELEASED

## Enhancements

* `Workspaces`: The `Unlock` method now returns a `ErrWorkspaceLockedStateVersionStillPending` error if the latest state version upload is still pending within the platform. This is a retryable error. by @brandonc

# v1.66.0

## Enhancements
Expand Down
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ var (
// ErrWorkspaceLockedByUser is returned when trying to unlock a workspace locked by a user.
ErrWorkspaceLockedByUser = errors.New("unable to unlock workspace locked by user")

// ErrWorkspaceLockedStateVersionStillPending is returned when trying to unlock whose
// latest state version is still pending.
ErrWorkspaceLockedStateVersionStillPending = errors.New("unable to unlock workspace while state version upload is still pending")

// 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
6 changes: 3 additions & 3 deletions tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,10 +934,10 @@ func parsePagination(body io.Reader) (*Pagination, error) {
return &raw.Meta.Pagination, nil
}

// checkResponseCode can be used to check the status code of an HTTP request.

// checkResponseCode refines typical API errors into more specific errors
// if possible. It returns nil if the response code < 400
func checkResponseCode(r *http.Response) error {
if r.StatusCode >= 200 && r.StatusCode <= 299 {
if r.StatusCode >= 200 && r.StatusCode <= 399 {
return nil
}

Expand Down
3 changes: 3 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,9 @@ func (s *workspaces) Unlock(ctx context.Context, workspaceID string) (*Workspace
w := &Workspace{}
err = req.Do(ctx, w)
if err != nil {
if strings.Contains(err.Error(), "latest state version is still pending") {
return nil, ErrWorkspaceLockedStateVersionStillPending
}
return nil, err
}

Expand Down

0 comments on commit 3d29602

Please sign in to comment.