diff --git a/internal/services/gateway/api/run.go b/internal/services/gateway/api/run.go index 80f23d3..e13a791 100644 --- a/internal/services/gateway/api/run.go +++ b/internal/services/gateway/api/run.go @@ -332,6 +332,59 @@ func (h *RunsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } +type RunActionType string + +const ( + RunActionTypeRestart RunActionType = "restart" +) + +type RunActionsRequest struct { + ActionType RunActionType `json:"action_type"` + + // Restart + FromStart bool `json:"from_start"` +} + +type RunActionsHandler struct { + log *zap.SugaredLogger + runserviceClient *rsapi.Client +} + +func NewRunActionsHandler(logger *zap.Logger, runserviceClient *rsapi.Client) *RunActionsHandler { + return &RunActionsHandler{log: logger.Sugar(), runserviceClient: runserviceClient} +} + +func (h *RunActionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + vars := mux.Vars(r) + runID := vars["runid"] + + var req RunActionsRequest + d := json.NewDecoder(r.Body) + if err := d.Decode(&req); err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + switch req.ActionType { + case RunActionTypeRestart: + req := &rsapi.RunCreateRequest{ + RunID: runID, + FromStart: req.FromStart, + } + + resp, err := h.runserviceClient.CreateRun(ctx, req) + if err != nil { + if resp != nil && resp.StatusCode == http.StatusNotFound { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } +} + type LogsHandler struct { log *zap.SugaredLogger runserviceClient *rsapi.Client diff --git a/internal/services/gateway/gateway.go b/internal/services/gateway/gateway.go index e904885..3e47977 100644 --- a/internal/services/gateway/gateway.go +++ b/internal/services/gateway/gateway.go @@ -174,6 +174,7 @@ func (g *Gateway) Run(ctx context.Context) error { runHandler := api.NewRunHandler(logger, g.runserviceClient) runsHandler := api.NewRunsHandler(logger, g.runserviceClient) runtaskHandler := api.NewRuntaskHandler(logger, g.runserviceClient) + runActionsHandler := api.NewRunActionsHandler(logger, g.runserviceClient) logsHandler := api.NewLogsHandler(logger, g.runserviceClient) @@ -229,6 +230,7 @@ func (g *Gateway) Run(ctx context.Context) error { apirouter.Handle("/orgs/{orgname}", authForcedHandler(deleteOrgHandler)).Methods("DELETE") apirouter.Handle("/run/{runid}", authForcedHandler(runHandler)).Methods("GET") + apirouter.Handle("/run/{runid}/actions", authForcedHandler(runActionsHandler)).Methods("POST") apirouter.Handle("/run/{runid}/task/{taskid}", authForcedHandler(runtaskHandler)).Methods("GET") apirouter.Handle("/runs", authForcedHandler(runsHandler)).Methods("GET")