feat(tfstated): implement DELETE method
This commit is contained in:
parent
d4a82f610c
commit
8949cee8b3
3 changed files with 37 additions and 0 deletions
31
cmd/tfstated/delete.go
Normal file
31
cmd/tfstated/delete.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||
)
|
||||
|
||||
func handleDelete(db *database.DB) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/" {
|
||||
_ = errorResponse(w, http.StatusBadRequest,
|
||||
fmt.Errorf("no state path provided, cannot DELETE /"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := db.DeleteState(r.URL.Path); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
_ = errorResponse(w, http.StatusNotFound,
|
||||
fmt.Errorf("state path not found: %s", r.URL.Path))
|
||||
} else {
|
||||
_ = errorResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
})
|
||||
}
|
|
@ -12,6 +12,7 @@ func addRoutes(
|
|||
) {
|
||||
mux.Handle("GET /healthz", handleHealthz())
|
||||
|
||||
mux.Handle("DELETE /", handleDelete(db))
|
||||
mux.Handle("GET /", handleGet(db))
|
||||
mux.Handle("POST /", handlePost(db))
|
||||
}
|
||||
|
|
|
@ -4,6 +4,11 @@ import (
|
|||
"database/sql"
|
||||
)
|
||||
|
||||
func (db *DB) DeleteState(name string) error {
|
||||
_, err := db.Exec(`DELETE FROM states WHERE name = ?;`, name)
|
||||
return err
|
||||
}
|
||||
|
||||
func (db *DB) GetState(name string) ([]byte, error) {
|
||||
var encryptedData []byte
|
||||
err := db.QueryRow(`SELECT data FROM states WHERE name = ?;`, name).Scan(&encryptedData)
|
||||
|
|
Loading…
Add table
Reference in a new issue