summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2024-10-02 08:26:53 +0200
committerJulien Dessaux2024-10-02 23:23:46 +0200
commit8949cee8b3f561397202246dd7cc7617d7dc2999 (patch)
tree1ad55af185feffdb14670692297b1eb6d0f4b6c7
parentfeat(tfstated): add json encoding and decoding helpers (diff)
downloadtfstated-8949cee8b3f561397202246dd7cc7617d7dc2999.tar.gz
tfstated-8949cee8b3f561397202246dd7cc7617d7dc2999.tar.bz2
tfstated-8949cee8b3f561397202246dd7cc7617d7dc2999.zip
feat(tfstated): implement DELETE method
-rw-r--r--cmd/tfstated/delete.go31
-rw-r--r--cmd/tfstated/routes.go1
-rw-r--r--pkg/database/states.go5
3 files changed, 37 insertions, 0 deletions
diff --git a/cmd/tfstated/delete.go b/cmd/tfstated/delete.go
new file mode 100644
index 0000000..82811b0
--- /dev/null
+++ b/cmd/tfstated/delete.go
@@ -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)
+ }
+ })
+}
diff --git a/cmd/tfstated/routes.go b/cmd/tfstated/routes.go
index 853970a..da46078 100644
--- a/cmd/tfstated/routes.go
+++ b/cmd/tfstated/routes.go
@@ -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))
}
diff --git a/pkg/database/states.go b/pkg/database/states.go
index ef2263b..3a0b0b9 100644
--- a/pkg/database/states.go
+++ b/pkg/database/states.go
@@ -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)