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

Method postTemplateVersionDryRun

coderd/templateversions.go:478–584  ·  view source on GitHub ↗

@Summary Create template version dry-run @ID create-template-version-dry-run @Security CoderSessionToken @Accept json @Produce json @Tags Templates @Param templateversion path string true "Template version ID" format(uuid) @Param request body codersdk.CreateTemplateVersionDryRunRequest true "Dry-run

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

Source from the content-addressed store, hash-verified

476// @Success 201 {object} codersdk.ProvisionerJob
477// @Router /api/v2/templateversions/{templateversion}/dry-run [post]
478func (api *API) postTemplateVersionDryRun(rw http.ResponseWriter, r *http.Request) {
479 ctx := r.Context()
480 var (
481 apiKey = httpmw.APIKey(r)
482 templateVersion = httpmw.TemplateVersionParam(r)
483 )
484
485 // We use the workspace RBAC check since we don't want to allow dry runs if
486 // the user can't create workspaces.
487 if !api.Authorize(r, policy.ActionCreate,
488 rbac.ResourceWorkspace.InOrg(templateVersion.OrganizationID).WithOwner(apiKey.UserID.String())) {
489 httpapi.ResourceNotFound(rw)
490 return
491 }
492
493 var req codersdk.CreateTemplateVersionDryRunRequest
494 if !httpapi.Read(ctx, rw, r, &req) {
495 return
496 }
497
498 job, err := api.Database.GetProvisionerJobByID(ctx, templateVersion.JobID)
499 if err != nil {
500 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
501 Message: "Internal error updating provisioner job.",
502 Detail: err.Error(),
503 })
504 return
505 }
506 if !job.CompletedAt.Valid {
507 httpapi.Write(ctx, rw, http.StatusTooEarly, codersdk.Response{
508 Message: "Template version import job hasn't completed!",
509 })
510 return
511 }
512
513 richParameterValues := make([]database.WorkspaceBuildParameter, len(req.RichParameterValues))
514 for i, v := range req.RichParameterValues {
515 richParameterValues[i] = database.WorkspaceBuildParameter{
516 WorkspaceBuildID: uuid.Nil,
517 Name: v.Name,
518 Value: v.Value,
519 }
520 }
521
522 // Marshal template version dry-run job with the parameters from the
523 // request.
524 input, err := json.Marshal(provisionerdserver.TemplateVersionDryRunJob{
525 TemplateVersionID: templateVersion.ID,
526 WorkspaceName: req.WorkspaceName,
527 RichParameterValues: richParameterValues,
528 })
529 if err != nil {
530 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
531 Message: "Internal error unmarshalling provisioner job.",
532 Detail: err.Error(),
533 })
534 return
535 }

Callers

nothing calls this directly

Calls 15

AuthorizeMethod · 0.95
APIKeyFunction · 0.92
TemplateVersionParamFunction · 0.92
ResourceNotFoundFunction · 0.92
ReadFunction · 0.92
WriteFunction · 0.92
MetadataFromContextFunction · 0.92
NowFunction · 0.92
PostJobFunction · 0.92
convertProvisionerJobFunction · 0.85
WithOwnerMethod · 0.80
InOrgMethod · 0.80

Tested by

no test coverage detected