gateway: implement initial basic run restart

This commit is contained in:
Simone Gotti 2019-03-04 16:12:07 +01:00
parent 9d2c133817
commit a4ad66ac2d
2 changed files with 55 additions and 0 deletions

View File

@ -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

View File

@ -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")