summaryrefslogtreecommitdiff
path: root/pkg/backend/unlock.go
blob: bc601f0831e06db4e740fe7886d35416a3a73172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package backend

import (
	"fmt"
	"net/http"

	"git.adyxax.org/adyxax/tfstated/pkg/database"
	"git.adyxax.org/adyxax/tfstated/pkg/helpers"
)

func handleUnlock(db *database.DB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			_ = helpers.Encode(w, http.StatusBadRequest,
				fmt.Errorf("no state path provided, cannot LOCK /"))
			return
		}

		var lock lockRequest
		if err := helpers.Decode(r, &lock); err != nil {
			_ = helpers.Encode(w, http.StatusBadRequest, err)
			return
		}
		if success, err := db.Unlock(r.URL.Path, &lock); err != nil {
			helpers.ErrorResponse(w, http.StatusInternalServerError, err)
		} else if success {
			w.WriteHeader(http.StatusOK)
		} else {
			_ = helpers.Encode(w, http.StatusConflict, lock)
		}
	})
}