MCPcopy Index your code
hub / github.com/coder/coder / patchTemplateVersionDryRunCancel

Method patchTemplateVersionDryRunCancel

coderd/templateversions.go:691–741  ·  view source on GitHub ↗

@Summary Cancel template version dry-run by job ID @ID cancel-template-version-dry-run-by-job-id @Security CoderSessionToken @Produce json @Tags Templates @Param jobID path string true "Job ID" format(uuid) @Param templateversion path string true "Template version ID" format(uuid) @Success 200 {obje

(rw http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

689// @Success 200 {object} codersdk.Response
690// @Router /api/v2/templateversions/{templateversion}/dry-run/{jobID}/cancel [patch]
691func (api *API) patchTemplateVersionDryRunCancel(rw http.ResponseWriter, r *http.Request) {
692 ctx := r.Context()
693 templateVersion := httpmw.TemplateVersionParam(r)
694
695 job, ok := api.fetchTemplateVersionDryRunJob(rw, r)
696 if !ok {
697 return
698 }
699 if !api.Authorize(r, policy.ActionUpdate,
700 rbac.ResourceWorkspace.InOrg(templateVersion.OrganizationID).WithOwner(job.ProvisionerJob.InitiatorID.String())) {
701 httpapi.ResourceNotFound(rw)
702 return
703 }
704
705 if job.ProvisionerJob.CompletedAt.Valid {
706 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
707 Message: "Job has already completed.",
708 })
709 return
710 }
711 if job.ProvisionerJob.CanceledAt.Valid {
712 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
713 Message: "Job has already been marked as canceled.",
714 })
715 return
716 }
717
718 err := api.Database.UpdateProvisionerJobWithCancelByID(ctx, database.UpdateProvisionerJobWithCancelByIDParams{
719 ID: job.ProvisionerJob.ID,
720 CanceledAt: sql.NullTime{
721 Time: dbtime.Now(),
722 Valid: true,
723 },
724 CompletedAt: sql.NullTime{
725 Time: dbtime.Now(),
726 // If the job is running, don't mark it completed!
727 Valid: !job.ProvisionerJob.WorkerID.Valid,
728 },
729 })
730 if err != nil {
731 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
732 Message: "Internal error updating provisioner job.",
733 Detail: err.Error(),
734 })
735 return
736 }
737
738 httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{
739 Message: "Job has been marked as canceled.",
740 })
741}
742
743func (api *API) fetchTemplateVersionDryRunJob(rw http.ResponseWriter, r *http.Request) (database.GetProvisionerJobsByIDsWithQueuePositionRow, bool) {
744 var (

Callers

nothing calls this directly

Calls 12

AuthorizeMethod · 0.95
TemplateVersionParamFunction · 0.92
ResourceNotFoundFunction · 0.92
WriteFunction · 0.92
NowFunction · 0.92
WithOwnerMethod · 0.80
InOrgMethod · 0.80
ContextMethod · 0.65
StringMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected