From f04cba4d59d5b276b358e8c15f0dff48bdfd0922 Mon Sep 17 00:00:00 2001 From: Rugwiro Mbanda Valentin Date: Thu, 1 Dec 2022 18:42:38 +0200 Subject: [PATCH 1/2] Add update settings http endpoint --- pkg/commands/admin.go | 27 +++++++++++++++++++++++++++ pkg/commands/handler.go | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/commands/admin.go b/pkg/commands/admin.go index 1521c65d..c52b4ce3 100644 --- a/pkg/commands/admin.go +++ b/pkg/commands/admin.go @@ -3,6 +3,7 @@ package commands import ( "encoding/json" "fmt" + "io" "net/http" "os/exec" "time" @@ -147,3 +148,29 @@ func handleViewSettings(w http.ResponseWriter, r *http.Request) { } render.JSON(w, res, http.StatusOK) } + +func handleUpdateSettings(w http.ResponseWriter, r *http.Request) { + env, err := util.BuildEnv() + if err != nil { + render.Err(w, err) + return + } + + config, err := io.ReadAll(r.Body) + + defer r.Body.Close() + + if err != nil { + err = fmt.Errorf("failed to read request body: %w", err) + render.Err(w, err) + return + } + + if _, err := stolon.Ctl([]string{"update", "--patch", string(config)}, env); err != nil { + render.Err(w, err) + return + } + resp := &Response{Result: "update completed successfully"} + + render.JSON(w, resp, http.StatusOK) +} diff --git a/pkg/commands/handler.go b/pkg/commands/handler.go index 7a705653..51732151 100644 --- a/pkg/commands/handler.go +++ b/pkg/commands/handler.go @@ -32,7 +32,7 @@ func Handler() http.Handler { r.Get("/failover/trigger", handleFailoverTrigger) r.Get("/restart", handleRestart) r.Get("/settings/view", handleViewSettings) - + r.Post("/settings/update", handleUpdateSettings) }) return r From 8454f2ce1835f05a2fca6126b8a8b8fe6ce3e043 Mon Sep 17 00:00:00 2001 From: Rugwiro Mbanda Valentin Date: Thu, 1 Dec 2022 18:45:59 +0200 Subject: [PATCH 2/2] Change response --- pkg/commands/admin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/commands/admin.go b/pkg/commands/admin.go index c52b4ce3..13c7d230 100644 --- a/pkg/commands/admin.go +++ b/pkg/commands/admin.go @@ -170,7 +170,7 @@ func handleUpdateSettings(w http.ResponseWriter, r *http.Request) { render.Err(w, err) return } - resp := &Response{Result: "update completed successfully"} + resp := &Response{Result: "Ok"} render.JSON(w, resp, http.StatusOK) }