(w http.ResponseWriter, r *http.Request)
| 33 | ) |
| 34 | |
| 35 | func SubmitBatchJob(w http.ResponseWriter, r *http.Request) { |
| 36 | vars := mux.Vars(r) |
| 37 | apiName := vars["apiName"] |
| 38 | dryRun := getOptionalBoolQParam("dryRun", false, r) |
| 39 | |
| 40 | deployedResource, err := resources.GetDeployedResourceByName(apiName) |
| 41 | if err != nil { |
| 42 | respondError(w, r, err) |
| 43 | return |
| 44 | } |
| 45 | if deployedResource.Kind != userconfig.BatchAPIKind { |
| 46 | respondError(w, r, resources.ErrorOperationIsOnlySupportedForKind(*deployedResource, userconfig.BatchAPIKind)) |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | // max payload size, same as API Gateway |
| 51 | rw := http.MaxBytesReader(w, r.Body, 10<<20) |
| 52 | |
| 53 | bodyBytes, err := ioutil.ReadAll(rw) |
| 54 | if err != nil { |
| 55 | respondError(w, r, err) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | submission := schema.BatchJobSubmission{} |
| 60 | |
| 61 | err = json.Unmarshal(bodyBytes, &submission) |
| 62 | if err != nil { |
| 63 | respondError(w, r, errors.Append(err, fmt.Sprintf("\n\njob submission schema can be found at https://docs.cortex.dev/v/%s/", consts.CortexVersionMinor))) |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | if dryRun { |
| 68 | // plain text response for dry run because it is typically consumed by people |
| 69 | w.Header().Set("Content-type", "text/plain") |
| 70 | |
| 71 | fileNames, err := batchapi.DryRun(&submission) |
| 72 | if err != nil { |
| 73 | w.WriteHeader(http.StatusBadRequest) |
| 74 | _, _ = io.WriteString(w, "\n"+err.Error()+"\n") |
| 75 | return |
| 76 | } |
| 77 | |
| 78 | for _, fileName := range fileNames { |
| 79 | _, err := io.WriteString(w, fileName+"\n") |
| 80 | if err != nil { |
| 81 | w.WriteHeader(http.StatusBadRequest) |
| 82 | _, _ = io.WriteString(w, "\n"+err.Error()+"\n") |
| 83 | return |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | _, _ = io.WriteString(w, "validations passed") |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | jobSpec, err := batchapi.SubmitJob(apiName, &submission) |
| 92 | if err != nil { |
nothing calls this directly
no test coverage detected