2022-04-06 17:42:40 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
2022-06-10 19:24:21 +00:00
|
|
|
"context"
|
2022-04-06 17:42:40 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-06-07 12:37:45 +00:00
|
|
|
"time"
|
2022-04-06 17:42:40 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
2022-05-16 19:36:27 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-04-06 17:42:40 +00:00
|
|
|
|
|
|
|
"github.com/coder/coder/coderd/database"
|
|
|
|
"github.com/coder/coder/coderd/httpapi"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
2022-05-24 13:43:34 +00:00
|
|
|
"github.com/coder/coder/coderd/rbac"
|
2022-06-17 05:26:40 +00:00
|
|
|
"github.com/coder/coder/coderd/telemetry"
|
2022-06-07 12:37:45 +00:00
|
|
|
"github.com/coder/coder/coderd/util/ptr"
|
2022-04-06 17:42:40 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
|
|
|
)
|
|
|
|
|
2022-06-07 12:37:45 +00:00
|
|
|
var (
|
|
|
|
maxTTLDefault = 24 * 7 * time.Hour
|
|
|
|
minAutostartIntervalDefault = time.Hour
|
|
|
|
)
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
// Returns a single template.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) template(rw http.ResponseWriter, r *http.Request) {
|
2022-04-06 17:42:40 +00:00
|
|
|
template := httpmw.TemplateParam(r)
|
2022-05-13 22:54:32 +00:00
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
workspaceCounts, err := api.Database.GetWorkspaceOwnerCountsByTemplateIDs(r.Context(), []uuid.UUID{template.ID})
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace count.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-24 13:43:34 +00:00
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-24 13:43:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
count := uint32(0)
|
|
|
|
if len(workspaceCounts) > 0 {
|
|
|
|
count = uint32(workspaceCounts[0].Count)
|
|
|
|
}
|
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
createdByNameMap, err := getCreatedByNamesByTemplateIDs(r.Context(), api.Database, []database.Template{template})
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-10 19:24:21 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertTemplate(template, count, createdByNameMap[template.ID.String()]))
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
|
2022-04-06 17:42:40 +00:00
|
|
|
template := httpmw.TemplateParam(r)
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionDelete, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-24 13:43:34 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-06 17:42:40 +00:00
|
|
|
|
2022-06-17 05:26:40 +00:00
|
|
|
workspaces, err := api.Database.GetWorkspaces(r.Context(), database.GetWorkspacesParams{
|
2022-06-14 13:46:33 +00:00
|
|
|
TemplateIds: []uuid.UUID{template.ID},
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspaces by template id.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(workspaces) > 0 {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusPreconditionFailed, codersdk.Response{
|
2022-04-06 17:42:40 +00:00
|
|
|
Message: "All workspaces must be deleted before a template can be removed.",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = api.Database.UpdateTemplateDeletedByID(r.Context(), database.UpdateTemplateDeletedByIDParams{
|
2022-06-30 12:14:51 +00:00
|
|
|
ID: template.ID,
|
|
|
|
Deleted: true,
|
|
|
|
UpdatedAt: database.Now(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error deleting template.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusOK, codersdk.Response{
|
2022-04-06 17:42:40 +00:00
|
|
|
Message: "Template has been deleted!",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
// Create a new template in an organization.
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Request) {
|
2022-05-16 19:36:27 +00:00
|
|
|
var createTemplate codersdk.CreateTemplateRequest
|
2022-05-24 13:43:34 +00:00
|
|
|
organization := httpmw.OrganizationParam(r)
|
2022-06-10 19:24:21 +00:00
|
|
|
apiKey := httpmw.APIKey(r)
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionCreate, rbac.ResourceTemplate.InOrg(organization.ID)) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-24 13:43:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
if !httpapi.Read(rw, r, &createTemplate) {
|
2022-05-10 07:44:09 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
_, err := api.Database.GetTemplateByOrganizationAndName(r.Context(), database.GetTemplateByOrganizationAndNameParams{
|
|
|
|
OrganizationID: organization.ID,
|
|
|
|
Name: createTemplate.Name,
|
2022-05-10 07:44:09 +00:00
|
|
|
})
|
2022-05-16 19:36:27 +00:00
|
|
|
if err == nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusConflict, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("Template with name %q already exists.", createTemplate.Name),
|
2022-07-13 00:15:02 +00:00
|
|
|
Validations: []codersdk.ValidationError{{
|
2022-05-16 19:36:27 +00:00
|
|
|
Field: "name",
|
2022-05-16 20:56:11 +00:00
|
|
|
Detail: "This value is already in use and should be unique.",
|
2022-05-16 19:36:27 +00:00
|
|
|
}},
|
|
|
|
})
|
2022-05-10 07:44:09 +00:00
|
|
|
return
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template by name.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
templateVersion, err := api.Database.GetTemplateVersionByID(r.Context(), createTemplate.VersionID)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusNotFound, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: fmt.Sprintf("Template version %q does not exist.", createTemplate.VersionID),
|
2022-07-13 00:15:02 +00:00
|
|
|
Validations: []codersdk.ValidationError{
|
2022-06-03 21:48:09 +00:00
|
|
|
{Field: "template_version_id", Detail: "Template version does not exist"},
|
|
|
|
},
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template version.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
importJob, err := api.Database.GetProvisionerJobByID(r.Context(), templateVersion.JobID)
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching provisioner job.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 12:37:45 +00:00
|
|
|
maxTTL := maxTTLDefault
|
|
|
|
if !ptr.NilOrZero(createTemplate.MaxTTLMillis) {
|
|
|
|
maxTTL = time.Duration(*createTemplate.MaxTTLMillis) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
|
|
|
minAutostartInterval := minAutostartIntervalDefault
|
|
|
|
if !ptr.NilOrZero(createTemplate.MinAutostartIntervalMillis) {
|
|
|
|
minAutostartInterval = time.Duration(*createTemplate.MinAutostartIntervalMillis) * time.Millisecond
|
|
|
|
}
|
|
|
|
|
2022-06-17 05:26:40 +00:00
|
|
|
var dbTemplate database.Template
|
2022-05-16 19:36:27 +00:00
|
|
|
var template codersdk.Template
|
|
|
|
err = api.Database.InTx(func(db database.Store) error {
|
|
|
|
now := database.Now()
|
2022-06-17 05:26:40 +00:00
|
|
|
dbTemplate, err = db.InsertTemplate(r.Context(), database.InsertTemplateParams{
|
2022-06-07 12:37:45 +00:00
|
|
|
ID: uuid.New(),
|
|
|
|
CreatedAt: now,
|
|
|
|
UpdatedAt: now,
|
|
|
|
OrganizationID: organization.ID,
|
|
|
|
Name: createTemplate.Name,
|
|
|
|
Provisioner: importJob.Provisioner,
|
|
|
|
ActiveVersionID: templateVersion.ID,
|
|
|
|
Description: createTemplate.Description,
|
|
|
|
MaxTtl: int64(maxTTL),
|
|
|
|
MinAutostartInterval: int64(minAutostartInterval),
|
2022-06-13 17:25:06 +00:00
|
|
|
CreatedBy: apiKey.UserID,
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert template: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.UpdateTemplateVersionByID(r.Context(), database.UpdateTemplateVersionByIDParams{
|
|
|
|
ID: templateVersion.ID,
|
|
|
|
TemplateID: uuid.NullUUID{
|
|
|
|
UUID: dbTemplate.ID,
|
|
|
|
Valid: true,
|
|
|
|
},
|
2022-07-15 21:12:39 +00:00
|
|
|
UpdatedAt: database.Now(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert template version: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, parameterValue := range createTemplate.ParameterValues {
|
|
|
|
_, err = db.InsertParameterValue(r.Context(), database.InsertParameterValueParams{
|
|
|
|
ID: uuid.New(),
|
|
|
|
Name: parameterValue.Name,
|
|
|
|
CreatedAt: database.Now(),
|
|
|
|
UpdatedAt: database.Now(),
|
|
|
|
Scope: database.ParameterScopeTemplate,
|
2022-06-17 17:22:28 +00:00
|
|
|
ScopeID: template.ID,
|
2022-05-19 18:04:44 +00:00
|
|
|
SourceScheme: database.ParameterSourceScheme(parameterValue.SourceScheme),
|
2022-05-16 19:36:27 +00:00
|
|
|
SourceValue: parameterValue.SourceValue,
|
2022-05-19 18:04:44 +00:00
|
|
|
DestinationScheme: database.ParameterDestinationScheme(parameterValue.DestinationScheme),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
2022-05-16 19:36:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("insert parameter value: %w", err)
|
|
|
|
}
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
createdByNameMap, err := getCreatedByNamesByTemplateIDs(r.Context(), db, []database.Template{dbTemplate})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("get creator name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
template = convertTemplate(dbTemplate, 0, createdByNameMap[dbTemplate.ID.String()])
|
2022-05-16 19:36:27 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error inserting template.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-05-16 19:36:27 +00:00
|
|
|
})
|
|
|
|
return
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
2022-04-12 15:17:33 +00:00
|
|
|
|
2022-06-17 05:26:40 +00:00
|
|
|
api.Telemetry.Report(&telemetry.Snapshot{
|
|
|
|
Templates: []telemetry.Template{telemetry.ConvertTemplate(dbTemplate)},
|
|
|
|
TemplateVersions: []telemetry.TemplateVersion{telemetry.ConvertTemplateVersion(templateVersion)},
|
|
|
|
})
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
httpapi.Write(rw, http.StatusCreated, template)
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request) {
|
2022-05-16 19:36:27 +00:00
|
|
|
organization := httpmw.OrganizationParam(r)
|
2022-06-14 13:46:33 +00:00
|
|
|
templates, err := api.Database.GetTemplatesWithFilter(r.Context(), database.GetTemplatesWithFilterParams{
|
2022-05-16 19:36:27 +00:00
|
|
|
OrganizationID: organization.ID,
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-05-16 19:36:27 +00:00
|
|
|
err = nil
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching templates in organization.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-24 13:43:34 +00:00
|
|
|
|
|
|
|
// Filter templates based on rbac permissions
|
2022-08-11 22:07:48 +00:00
|
|
|
templates, err = AuthorizeFilter(api, r, rbac.ActionRead, templates)
|
|
|
|
if err != nil {
|
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
Message: "Internal error fetching templates.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-24 13:43:34 +00:00
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
templateIDs := make([]uuid.UUID, 0, len(templates))
|
2022-05-24 13:43:34 +00:00
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
for _, template := range templates {
|
|
|
|
templateIDs = append(templateIDs, template.ID)
|
|
|
|
}
|
|
|
|
workspaceCounts, err := api.Database.GetWorkspaceOwnerCountsByTemplateIDs(r.Context(), templateIDs)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
2022-04-06 17:42:40 +00:00
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace counts.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
createdByNameMap, err := getCreatedByNamesByTemplateIDs(r.Context(), api.Database, templates)
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-10 19:24:21 +00:00
|
|
|
Message: "Internal error fetching creator names.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertTemplates(templates, workspaceCounts, createdByNameMap))
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 03:14:08 +00:00
|
|
|
func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Request) {
|
2022-05-16 19:36:27 +00:00
|
|
|
organization := httpmw.OrganizationParam(r)
|
|
|
|
templateName := chi.URLParam(r, "templatename")
|
|
|
|
template, err := api.Database.GetTemplateByOrganizationAndName(r.Context(), database.GetTemplateByOrganizationAndNameParams{
|
|
|
|
OrganizationID: organization.ID,
|
|
|
|
Name: templateName,
|
|
|
|
})
|
2022-04-06 17:42:40 +00:00
|
|
|
if err != nil {
|
2022-05-16 19:36:27 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-06-14 15:14:05 +00:00
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-16 19:36:27 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching template.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionRead, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-05-24 13:43:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:36:27 +00:00
|
|
|
workspaceCounts, err := api.Database.GetWorkspaceOwnerCountsByTemplateIDs(r.Context(), []uuid.UUID{template.ID})
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-07 14:33:06 +00:00
|
|
|
Message: "Internal error fetching workspace counts.",
|
2022-06-03 21:48:09 +00:00
|
|
|
Detail: err.Error(),
|
2022-04-06 17:42:40 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2022-05-16 19:36:27 +00:00
|
|
|
|
|
|
|
count := uint32(0)
|
|
|
|
if len(workspaceCounts) > 0 {
|
|
|
|
count = uint32(workspaceCounts[0].Count)
|
|
|
|
}
|
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
createdByNameMap, err := getCreatedByNamesByTemplateIDs(r.Context(), api.Database, []database.Template{template})
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-10 19:24:21 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertTemplate(template, count, createdByNameMap[template.ID.String()]))
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 14:14:57 +00:00
|
|
|
func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
template := httpmw.TemplateParam(r)
|
2022-06-14 15:14:05 +00:00
|
|
|
if !api.Authorize(r, rbac.ActionUpdate, template) {
|
|
|
|
httpapi.ResourceNotFound(rw)
|
2022-06-08 14:14:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req codersdk.UpdateTemplateMeta
|
|
|
|
if !httpapi.Read(rw, r, &req) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-13 00:15:02 +00:00
|
|
|
var validErrs []codersdk.ValidationError
|
2022-06-08 14:14:57 +00:00
|
|
|
if req.MaxTTLMillis < 0 {
|
2022-07-13 00:15:02 +00:00
|
|
|
validErrs = append(validErrs, codersdk.ValidationError{Field: "max_ttl_ms", Detail: "Must be a positive integer."})
|
2022-06-08 14:14:57 +00:00
|
|
|
}
|
|
|
|
if req.MinAutostartIntervalMillis < 0 {
|
2022-07-13 00:15:02 +00:00
|
|
|
validErrs = append(validErrs, codersdk.ValidationError{Field: "min_autostart_interval_ms", Detail: "Must be a positive integer."})
|
2022-06-08 14:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(validErrs) > 0 {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
|
2022-06-08 14:14:57 +00:00
|
|
|
Message: "Invalid request to update template metadata!",
|
|
|
|
Validations: validErrs,
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
count := uint32(0)
|
|
|
|
var updated database.Template
|
|
|
|
err := api.Database.InTx(func(s database.Store) error {
|
|
|
|
// Fetch workspace counts
|
|
|
|
workspaceCounts, err := s.GetWorkspaceOwnerCountsByTemplateIDs(r.Context(), []uuid.UUID{template.ID})
|
|
|
|
if xerrors.Is(err, sql.ErrNoRows) {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(workspaceCounts) > 0 {
|
|
|
|
count = uint32(workspaceCounts[0].Count)
|
|
|
|
}
|
|
|
|
|
2022-08-17 19:04:00 +00:00
|
|
|
if req.Name == template.Name &&
|
|
|
|
req.Description == template.Description &&
|
2022-06-08 14:14:57 +00:00
|
|
|
req.MaxTTLMillis == time.Duration(template.MaxTtl).Milliseconds() &&
|
|
|
|
req.MinAutostartIntervalMillis == time.Duration(template.MinAutostartInterval).Milliseconds() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update template metadata -- empty fields are not overwritten.
|
2022-08-17 19:04:00 +00:00
|
|
|
name := req.Name
|
2022-06-08 14:14:57 +00:00
|
|
|
desc := req.Description
|
|
|
|
maxTTL := time.Duration(req.MaxTTLMillis) * time.Millisecond
|
|
|
|
minAutostartInterval := time.Duration(req.MinAutostartIntervalMillis) * time.Millisecond
|
|
|
|
|
2022-08-17 19:04:00 +00:00
|
|
|
if name == "" {
|
|
|
|
name = template.Name
|
|
|
|
}
|
2022-06-08 14:14:57 +00:00
|
|
|
if desc == "" {
|
|
|
|
desc = template.Description
|
|
|
|
}
|
|
|
|
if maxTTL == 0 {
|
|
|
|
maxTTL = time.Duration(template.MaxTtl)
|
|
|
|
}
|
|
|
|
if minAutostartInterval == 0 {
|
|
|
|
minAutostartInterval = time.Duration(template.MinAutostartInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.UpdateTemplateMetaByID(r.Context(), database.UpdateTemplateMetaByIDParams{
|
|
|
|
ID: template.ID,
|
|
|
|
UpdatedAt: database.Now(),
|
2022-08-17 19:04:00 +00:00
|
|
|
Name: name,
|
2022-06-08 14:14:57 +00:00
|
|
|
Description: desc,
|
|
|
|
MaxTtl: int64(maxTTL),
|
|
|
|
MinAutostartInterval: int64(minAutostartInterval),
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated, err = s.GetTemplateByID(r.Context(), template.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-08 14:14:57 +00:00
|
|
|
Message: "Internal error updating template metadata.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if updated.UpdatedAt.IsZero() {
|
|
|
|
httpapi.Write(rw, http.StatusNotModified, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
createdByNameMap, err := getCreatedByNamesByTemplateIDs(r.Context(), api.Database, []database.Template{updated})
|
|
|
|
if err != nil {
|
2022-07-13 00:15:02 +00:00
|
|
|
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
|
2022-06-10 19:24:21 +00:00
|
|
|
Message: "Internal error fetching creator name.",
|
|
|
|
Detail: err.Error(),
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
httpapi.Write(rw, http.StatusOK, convertTemplate(updated, count, createdByNameMap[updated.ID.String()]))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, templates []database.Template) (map[string]string, error) {
|
|
|
|
creators := make(map[string]string, len(templates))
|
|
|
|
for _, template := range templates {
|
2022-06-13 17:25:06 +00:00
|
|
|
creator, err := db.GetUserByID(ctx, template.CreatedBy)
|
|
|
|
if err != nil {
|
|
|
|
return map[string]string{}, err
|
2022-06-10 19:24:21 +00:00
|
|
|
}
|
2022-06-13 17:25:06 +00:00
|
|
|
creators[template.ID.String()] = creator.Username
|
2022-06-10 19:24:21 +00:00
|
|
|
}
|
|
|
|
return creators, nil
|
2022-06-08 14:14:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
func convertTemplates(templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow, createdByNameMap map[string]string) []codersdk.Template {
|
2022-04-06 17:42:40 +00:00
|
|
|
apiTemplates := make([]codersdk.Template, 0, len(templates))
|
|
|
|
for _, template := range templates {
|
|
|
|
found := false
|
|
|
|
for _, workspaceCount := range workspaceCounts {
|
|
|
|
if workspaceCount.TemplateID.String() != template.ID.String() {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-10 19:24:21 +00:00
|
|
|
apiTemplates = append(apiTemplates, convertTemplate(template, uint32(workspaceCount.Count), createdByNameMap[template.ID.String()]))
|
2022-04-06 17:42:40 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !found {
|
2022-06-10 19:24:21 +00:00
|
|
|
apiTemplates = append(apiTemplates, convertTemplate(template, uint32(0), createdByNameMap[template.ID.String()]))
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiTemplates
|
|
|
|
}
|
|
|
|
|
2022-06-10 19:24:21 +00:00
|
|
|
func convertTemplate(template database.Template, workspaceOwnerCount uint32, createdByName string) codersdk.Template {
|
2022-04-06 17:42:40 +00:00
|
|
|
return codersdk.Template{
|
2022-06-07 12:37:45 +00:00
|
|
|
ID: template.ID,
|
|
|
|
CreatedAt: template.CreatedAt,
|
|
|
|
UpdatedAt: template.UpdatedAt,
|
|
|
|
OrganizationID: template.OrganizationID,
|
|
|
|
Name: template.Name,
|
|
|
|
Provisioner: codersdk.ProvisionerType(template.Provisioner),
|
|
|
|
ActiveVersionID: template.ActiveVersionID,
|
|
|
|
WorkspaceOwnerCount: workspaceOwnerCount,
|
|
|
|
Description: template.Description,
|
|
|
|
MaxTTLMillis: time.Duration(template.MaxTtl).Milliseconds(),
|
|
|
|
MinAutostartIntervalMillis: time.Duration(template.MinAutostartInterval).Milliseconds(),
|
2022-06-10 19:24:21 +00:00
|
|
|
CreatedByID: template.CreatedBy,
|
|
|
|
CreatedByName: createdByName,
|
2022-04-06 17:42:40 +00:00
|
|
|
}
|
|
|
|
}
|