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

Method fetchTemplates

coderd/templates.go:555–608  ·  view source on GitHub ↗

@Summary Get all templates @Description Returns a list of templates. @Description By default, only non-deprecated templates are returned. @Description To include deprecated templates, specify `deprecated:true` in the search query. @ID get-all-templates @Security CoderSessionToken @Produce json @Tags

(mutate func(r *http.Request, arg *database.GetTemplatesWithFilterParams))

Source from the content-addressed store, hash-verified

553// @Success 200 {array} codersdk.Template
554// @Router /api/v2/templates [get]
555func (api *API) fetchTemplates(mutate func(r *http.Request, arg *database.GetTemplatesWithFilterParams)) http.HandlerFunc {
556 return func(rw http.ResponseWriter, r *http.Request) {
557 ctx := r.Context()
558 key := httpmw.APIKey(r)
559
560 queryStr := r.URL.Query().Get("q")
561 filter, errs := searchquery.Templates(ctx, api.Database, key.UserID, queryStr)
562 if len(errs) > 0 {
563 httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
564 Message: "Invalid template search query.",
565 Validations: errs,
566 })
567 return
568 }
569
570 prepared, err := api.HTTPAuth.AuthorizeSQLFilter(r, policy.ActionRead, rbac.ResourceTemplate.Type)
571 if err != nil {
572 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
573 Message: "Internal error preparing sql filter.",
574 Detail: err.Error(),
575 })
576 return
577 }
578
579 args := filter
580 if mutate != nil {
581 mutate(r, &args)
582 }
583
584 // By default, deprecated templates are excluded unless explicitly requested
585 if !args.Deprecated.Valid {
586 args.Deprecated = sql.NullBool{
587 Bool: false,
588 Valid: true,
589 }
590 }
591
592 // Filter templates based on rbac permissions
593 templates, err := api.Database.GetAuthorizedTemplates(ctx, args, prepared)
594 if errors.Is(err, sql.ErrNoRows) {
595 err = nil
596 }
597
598 if err != nil {
599 httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
600 Message: "Internal error fetching templates in organization.",
601 Detail: err.Error(),
602 })
603 return
604 }
605
606 httpapi.Write(ctx, rw, http.StatusOK, api.convertTemplates(templates))
607 }
608}
609
610// @Summary Get templates by organization and template name
611// @ID get-templates-by-organization-and-template-name

Callers 2

NewFunction · 0.95

Calls 10

convertTemplatesMethod · 0.95
APIKeyFunction · 0.92
TemplatesFunction · 0.92
WriteFunction · 0.92
AuthorizeSQLFilterMethod · 0.80
ContextMethod · 0.65
GetMethod · 0.65
ErrorMethod · 0.45
IsMethod · 0.45

Tested by

no test coverage detected