Skip to content

Commit

Permalink
Merge pull request #29 from nimbolus/return-existing-lockinfo
Browse files Browse the repository at this point in the history
fix: return existing lockinfo when already locked
  • Loading branch information
tobikris authored May 7, 2024
2 parents 830fc79 + 378ab44 commit ed592d6
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pkg/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,15 @@ func Lock(w http.ResponseWriter, r *http.Request, state *terraform.State, body [
HTTPResponse(w, r, http.StatusInternalServerError, "")
} else if !ok {
log.Warnf("state with id %s is already locked by %s", state.ID, state.Lock)
HTTPResponse(w, r, http.StatusLocked, state.Lock.ID)

lockInfo, err := json.Marshal(state.Lock)
if err != nil {
log.Errorf("failed to marshal lock info: %v", err)
HTTPResponse(w, r, http.StatusInternalServerError, "")
return
}

HTTPResponse(w, r, http.StatusLocked, string(lockInfo))
} else {
log.Debugf("state with id %s was locked successfully", state.ID)
HTTPResponse(w, r, http.StatusOK, "")
Expand All @@ -123,8 +131,16 @@ func Unlock(w http.ResponseWriter, r *http.Request, state *terraform.State, body
log.Errorf("failed to unlock state with id %s: %v", state.ID, err)
HTTPResponse(w, r, http.StatusInternalServerError, "")
} else if !ok {
log.Warnf("failed to unlock state with id %s: %v", state.ID, err)
HTTPResponse(w, r, http.StatusBadRequest, state.Lock.ID)
log.Warnf("failed to unlock state with id %s: locks not equal", state.ID)

lockInfo, err := json.Marshal(state.Lock)
if err != nil {
log.Errorf("failed to marshal lock info: %v", err)
HTTPResponse(w, r, http.StatusInternalServerError, "")
return
}

HTTPResponse(w, r, http.StatusBadRequest, string(lockInfo))
} else {
log.Debugf("state with id %s was unlocked successfully", state.ID)
HTTPResponse(w, r, http.StatusOK, "")
Expand Down

0 comments on commit ed592d6

Please sign in to comment.