2024-10-03 00:13:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-10-06 00:13:53 +02:00
|
|
|
"regexp"
|
2024-10-03 00:13:09 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2024-10-06 00:13:53 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-03 00:13:09 +02:00
|
|
|
func handleLock(db *database.DB) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/" {
|
|
|
|
_ = encode(w, http.StatusBadRequest,
|
|
|
|
fmt.Errorf("no state path provided, cannot LOCK /"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var lock lockRequest
|
|
|
|
if err := decode(r, &lock); err != nil {
|
|
|
|
_ = encode(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
2024-10-06 00:13:53 +02:00
|
|
|
if errs := lock.valid(); len(errs) > 0 {
|
|
|
|
_ = encode(w, http.StatusBadRequest,
|
|
|
|
fmt.Errorf("invalid lock: %+v", errs))
|
|
|
|
return
|
|
|
|
}
|
2024-10-03 00:13:09 +02:00
|
|
|
if success, err := db.SetLockOrGetExistingLock(r.URL.Path, &lock); err != nil {
|
2024-10-06 00:13:53 +02:00
|
|
|
_ = errorResponse(w, http.StatusInternalServerError, err)
|
2024-10-03 00:13:09 +02:00
|
|
|
} else if success {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
} else {
|
|
|
|
_ = encode(w, http.StatusConflict, lock)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|