runservice api: get run from readdb

This commit is contained in:
Simone Gotti 2019-05-06 15:18:49 +02:00
parent 3139ef38d9
commit a590c21127
1 changed files with 28 additions and 16 deletions

View File

@ -335,6 +335,7 @@ func (h *ChangeGroupsUpdateTokensHandler) ServeHTTP(w http.ResponseWriter, r *ht
type RunResponse struct { type RunResponse struct {
Run *types.Run `json:"run"` Run *types.Run `json:"run"`
RunConfig *types.RunConfig `json:"run_config"` RunConfig *types.RunConfig `json:"run_config"`
ChangeGroupsUpdateToken string `json:"change_groups_update_tokens"`
} }
type RunHandler struct { type RunHandler struct {
@ -354,26 +355,36 @@ func NewRunHandler(logger *zap.Logger, e *etcd.Store, dm *datamanager.DataManage
} }
func (h *RunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *RunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r) vars := mux.Vars(r)
runID := vars["runid"] runID := vars["runid"]
run, _, err := store.GetRun(ctx, h.e, runID) query := r.URL.Query()
if err != nil && err != etcd.ErrKeyNotFound { changeGroups := query["changegroup"]
var run *types.Run
var cgt *types.ChangeGroupsUpdateToken
err := h.readDB.Do(func(tx *db.Tx) error {
var err error
run, err = h.readDB.GetRun(tx, runID)
if err != nil {
h.log.Errorf("err: %+v", err)
return err
}
cgt, err = h.readDB.GetChangeGroupsUpdateTokens(tx, changeGroups)
return err
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
if run == nil {
run, err = store.OSTGetRun(h.dm, runID) cgts, err := types.MarshalChangeGroupsUpdateToken(cgt)
if err != nil && err != objectstorage.ErrNotExist { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
}
if run == nil {
http.Error(w, "", http.StatusNotFound)
return
}
rc, err := store.OSTGetRunConfig(h.dm, run.ID) rc, err := store.OSTGetRunConfig(h.dm, run.ID)
if err != nil { if err != nil {
@ -384,6 +395,7 @@ func (h *RunHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
res := &RunResponse{ res := &RunResponse{
Run: run, Run: run,
RunConfig: rc, RunConfig: rc,
ChangeGroupsUpdateToken: cgts,
} }
if err := httpResponse(w, http.StatusOK, res); err != nil { if err := httpResponse(w, http.StatusOK, res); err != nil {