summaryrefslogtreecommitdiff
path: root/pkg/backend/lock.go
blob: ef621989264d75c3dbe97dc8487e5040fa1e52f3 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package backend

import (
	"fmt"
	"net/http"
	"regexp"
	"time"

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

type lockRequest struct {
	Created   time.Time `json:"Created"`
	ID        string    `json:"ID"`
	Info      string    `json:"Info"`
	Operation string    `json:"Operation"`
	Path      string    `json:"Path"`
	Version   string    `json:"Version"`
	Who       string    `json:"Who"`
}

var (
	validID = regexp.MustCompile("[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}")
)

func (l *lockRequest) valid() []error {
	err := make([]error, 0)
	if !validID.MatchString(l.ID) {
		err = append(err, fmt.Errorf("invalid ID"))
	}
	return err
}

func handleLock(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 errs := lock.valid(); len(errs) > 0 {
			_ = helpers.Encode(w, http.StatusBadRequest,
				fmt.Errorf("invalid lock: %+v", errs))
			return
		}
		if success, err := db.SetLockOrGetExistingLock(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)
		}
	})
}