@Summary Update workspace autostart schedule by ID @ID update-workspace-autostart-schedule-by-id @Security CoderSessionToken @Accept json @Tags Workspaces @Param workspace path string true "Workspace ID" format(uuid) @Param request body codersdk.UpdateWorkspaceAutostartRequest true "Schedule update
(rw http.ResponseWriter, r *http.Request)
| 1210 | // @Success 204 |
| 1211 | // @Router /api/v2/workspaces/{workspace}/autostart [put] |
| 1212 | func (api *API) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) { |
| 1213 | var ( |
| 1214 | ctx = r.Context() |
| 1215 | workspace = httpmw.WorkspaceParam(r) |
| 1216 | auditor = api.Auditor.Load() |
| 1217 | aReq, commitAudit = audit.InitRequest[database.WorkspaceTable](rw, &audit.RequestParams{ |
| 1218 | Audit: *auditor, |
| 1219 | Log: api.Logger, |
| 1220 | Request: r, |
| 1221 | Action: database.AuditActionWrite, |
| 1222 | OrganizationID: workspace.OrganizationID, |
| 1223 | }) |
| 1224 | ) |
| 1225 | defer commitAudit() |
| 1226 | aReq.Old = workspace.WorkspaceTable() |
| 1227 | |
| 1228 | var req codersdk.UpdateWorkspaceAutostartRequest |
| 1229 | if !httpapi.Read(ctx, rw, r, &req) { |
| 1230 | return |
| 1231 | } |
| 1232 | |
| 1233 | // Autostart configuration is not supported for prebuilt workspaces. |
| 1234 | // Prebuild lifecycle is managed by the reconciliation loop, with scheduling behavior |
| 1235 | // defined per preset at the template level, not per workspace. |
| 1236 | if workspace.IsPrebuild() { |
| 1237 | httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{ |
| 1238 | Message: "Autostart is not supported for prebuilt workspaces", |
| 1239 | Detail: "Prebuilt workspace scheduling is configured per preset at the template level. Workspace-level overrides are not supported.", |
| 1240 | }) |
| 1241 | return |
| 1242 | } |
| 1243 | |
| 1244 | dbSched, err := validWorkspaceSchedule(req.Schedule) |
| 1245 | if err != nil { |
| 1246 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1247 | Message: "Invalid autostart schedule.", |
| 1248 | Validations: []codersdk.ValidationError{{Field: "schedule", Detail: err.Error()}}, |
| 1249 | }) |
| 1250 | return |
| 1251 | } |
| 1252 | |
| 1253 | // Check if the template allows users to configure autostart. |
| 1254 | templateSchedule, err := (*api.TemplateScheduleStore.Load()).Get(ctx, api.Database, workspace.TemplateID) |
| 1255 | if err != nil { |
| 1256 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 1257 | Message: "Internal error getting template schedule options.", |
| 1258 | Detail: err.Error(), |
| 1259 | }) |
| 1260 | return |
| 1261 | } |
| 1262 | if !templateSchedule.UserAutostartEnabled { |
| 1263 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 1264 | Message: "Autostart is not allowed for workspaces using this template.", |
| 1265 | Validations: []codersdk.ValidationError{{Field: "schedule", Detail: "Autostart is not allowed for workspaces using this template."}}, |
| 1266 | }) |
| 1267 | return |
| 1268 | } |
| 1269 |
no test coverage detected