changeset 1037:a04126989d91

Added endpoint to delete job from import queue. This has no effect on running jobs. DELETE path: /api/imports/[0-9]+
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 24 Oct 2018 16:21:44 +0200
parents ae2e716b85d2
children e8ebfbc2aa05
files pkg/controllers/importqueue.go pkg/controllers/routes.go schema/auth.sql
diffstat 3 files changed, 65 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importqueue.go	Wed Oct 24 16:07:52 2018 +0200
+++ b/pkg/controllers/importqueue.go	Wed Oct 24 16:21:44 2018 +0200
@@ -40,6 +40,10 @@
 	selectHasImportSQL = `
 SELECT true FROM Waterway.imports WHERE id = $1`
 
+	selectHasNoRunningImportSQL = `
+SELECT true FROM Waterway.imports
+WHERE id = $1 AND state <> 'running'::waterway.import_state`
+
 	selectImportLogsSQL = `
 SELECT
   time,
@@ -48,6 +52,12 @@
 FROM waterway.import_logs
 WHERE import_id = $1
 ORDER BY time`
+
+	deleteLogsSQL = `
+DELETE FROM waterway.import_logs WHERE import_id = $1`
+
+	deleteImportSQL = `
+DELETE FROM waterway.imports WHERE id = $1`
 )
 
 func listImports(
@@ -161,3 +171,50 @@
 	}
 	return
 }
+
+func deleteImport(
+	_ interface{},
+	req *http.Request,
+	conn *sql.Conn,
+) (jr JSONResult, err error) {
+
+	ctx := req.Context()
+	id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
+
+	var tx *sql.Tx
+	tx, err = conn.BeginTx(ctx, nil)
+	if err != nil {
+		return
+	}
+	defer tx.Rollback()
+
+	// Check if he have such a import job first.
+	var dummy bool
+	err = tx.QueryRowContext(ctx, selectHasNoRunningImportSQL, id).Scan(&dummy)
+	switch {
+	case err == sql.ErrNoRows:
+		err = JSONError{
+			Code:    http.StatusNotFound,
+			Message: fmt.Sprintf("Cannot find import #%d.", id),
+		}
+		return
+	case err != nil:
+		return
+	}
+
+	if _, err = tx.ExecContext(ctx, deleteLogsSQL, id); err != nil {
+		return
+	}
+
+	if _, err = tx.ExecContext(ctx, deleteImportSQL, id); err != nil {
+		return
+	}
+
+	if err = tx.Commit(); err != nil {
+		return
+	}
+
+	jr = JSONResult{Code: http.StatusNoContent}
+
+	return
+}
--- a/pkg/controllers/routes.go	Wed Oct 24 16:07:52 2018 +0200
+++ b/pkg/controllers/routes.go	Wed Oct 24 16:21:44 2018 +0200
@@ -169,13 +169,15 @@
 			"offset", "{offset:[0-9]+}",
 			"limit", "{limit:[0-9]+}")
 
-	api.Handle("/imports", lsImports).
-		Methods(http.MethodGet)
+	api.Handle("/imports", lsImports).Methods(http.MethodGet)
 
 	api.Handle("/imports/{id:[0-9]+}", waterwayAdmin(&JSONHandler{
 		Handle: importLogs,
-	})).
-		Methods(http.MethodGet)
+	})).Methods(http.MethodGet)
+
+	api.Handle("/imports/{id:[0-9]+}", waterwayAdmin(&JSONHandler{
+		Handle: deleteImport,
+	})).Methods(http.MethodDelete)
 
 	// Token handling: Login/Logout.
 	api.HandleFunc("/login", login).
--- a/schema/auth.sql	Wed Oct 24 16:07:52 2018 +0200
+++ b/schema/auth.sql	Wed Oct 24 16:21:44 2018 +0200
@@ -23,6 +23,8 @@
 -- TODO: will there ever be UPDATEs or can we drop that due to historicisation?
 GRANT INSERT, UPDATE, DELETE ON
     users.templates, users.user_templates TO waterway_admin;
+GRANT INSERT, UPDATE, DELETE ON
+    waterway.imports, waterway.import_logs TO waterway_admin;
 
 --
 -- Extended privileges for sys_admin