runservice/gateway: return run on run action

This commit is contained in:
Simone Gotti 2019-05-07 23:23:58 +02:00
parent 83273489e0
commit e4e7de4ad2
4 changed files with 23 additions and 16 deletions

View File

@ -115,17 +115,17 @@ type RunActionsRequest struct {
FromStart bool
}
func (h *ActionHandler) RunAction(ctx context.Context, req *RunActionsRequest) error {
func (h *ActionHandler) RunAction(ctx context.Context, req *RunActionsRequest) (*rsapi.RunResponse, error) {
runResp, resp, err := h.runserviceClient.GetRun(ctx, req.RunID, nil)
if err != nil {
return ErrFromRemote(resp, err)
return nil, ErrFromRemote(resp, err)
}
canGetRun, err := h.CanDoRunActions(ctx, runResp.RunConfig.Group)
if err != nil {
return errors.Wrapf(err, "failed to determine permissions")
return nil, errors.Wrapf(err, "failed to determine permissions")
}
if !canGetRun {
return util.NewErrForbidden(errors.Errorf("user not authorized"))
return nil, util.NewErrForbidden(errors.Errorf("user not authorized"))
}
switch req.ActionType {
@ -135,9 +135,9 @@ func (h *ActionHandler) RunAction(ctx context.Context, req *RunActionsRequest) e
FromStart: req.FromStart,
}
resp, err := h.runserviceClient.CreateRun(ctx, rsreq)
runResp, resp, err = h.runserviceClient.CreateRun(ctx, rsreq)
if err != nil {
return ErrFromRemote(resp, err)
return nil, ErrFromRemote(resp, err)
}
case RunActionTypeStop:
@ -145,16 +145,16 @@ func (h *ActionHandler) RunAction(ctx context.Context, req *RunActionsRequest) e
ActionType: rsapi.RunActionTypeStop,
}
resp, err := h.runserviceClient.RunActions(ctx, req.RunID, rsreq)
resp, err = h.runserviceClient.RunActions(ctx, req.RunID, rsreq)
if err != nil {
return ErrFromRemote(resp, err)
return nil, ErrFromRemote(resp, err)
}
default:
return util.NewErrBadRequest(errors.Errorf("wrong run action type %q", req.ActionType))
return nil, util.NewErrBadRequest(errors.Errorf("wrong run action type %q", req.ActionType))
}
return nil
return runResp, nil
}
type RunTaskActionType string

View File

@ -407,11 +407,16 @@ func (h *RunActionsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
FromStart: req.FromStart,
}
err := h.ah.RunAction(ctx, areq)
runResp, err := h.ah.RunAction(ctx, areq)
if httpError(w, err) {
h.log.Errorf("err: %+v", err)
return
}
res := createRunResponse(runResp.Run, runResp.RunConfig)
if err := httpResponse(w, http.StatusOK, res); err != nil {
h.log.Errorf("err: %+v", err)
}
}
type RunTaskActionsRequest struct {

View File

@ -370,7 +370,7 @@ func (h *webhooksHandler) createRuns(ctx context.Context, filename string, confi
Annotations: annotations,
}
if _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil {
if _, _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil {
log.Errorf("failed to create run: %+v", err)
return err
}
@ -391,7 +391,7 @@ func (h *webhooksHandler) createRuns(ctx context.Context, filename string, confi
Annotations: annotations,
}
if _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil {
if _, _, err := h.runserviceClient.CreateRun(ctx, createRunReq); err != nil {
log.Errorf("failed to create run: %+v", err)
return err
}

View File

@ -214,13 +214,15 @@ func (c *Client) GetGroupFirstQueuedRuns(ctx context.Context, group string, chan
return c.GetRuns(ctx, []string{"queued"}, []string{group}, false, changeGroups, "", 1, true)
}
func (c *Client) CreateRun(ctx context.Context, req *RunCreateRequest) (*http.Response, error) {
func (c *Client) CreateRun(ctx context.Context, req *RunCreateRequest) (*RunResponse, *http.Response, error) {
reqj, err := json.Marshal(req)
if err != nil {
return nil, err
return nil, nil, err
}
return c.getResponse(ctx, "POST", "/runs", nil, -1, jsonContent, bytes.NewReader(reqj))
res := new(RunResponse)
resp, err := c.getParsedResponse(ctx, "POST", "/runs", nil, jsonContent, bytes.NewReader(reqj), res)
return res, resp, err
}
func (c *Client) RunActions(ctx context.Context, runID string, req *RunActionsRequest) (*http.Response, error) {