@Summary Update health settings @ID update-health-settings @Security CoderSessionToken @Accept json @Produce json @Tags Debug @Param request body healthsdk.UpdateHealthSettings true "Update health settings" @Success 200 {object} healthsdk.UpdateHealthSettings @Router /api/v2/debug/health/settings [p
(rw http.ResponseWriter, r *http.Request)
| 206 | // @Success 200 {object} healthsdk.UpdateHealthSettings |
| 207 | // @Router /api/v2/debug/health/settings [put] |
| 208 | func (api *API) putDeploymentHealthSettings(rw http.ResponseWriter, r *http.Request) { |
| 209 | ctx := r.Context() |
| 210 | |
| 211 | if !api.Authorize(r, policy.ActionUpdate, rbac.ResourceDeploymentConfig) { |
| 212 | httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{ |
| 213 | Message: "Insufficient permissions to update health settings.", |
| 214 | }) |
| 215 | return |
| 216 | } |
| 217 | |
| 218 | var settings healthsdk.HealthSettings |
| 219 | if !httpapi.Read(ctx, rw, r, &settings) { |
| 220 | return |
| 221 | } |
| 222 | |
| 223 | err := validateHealthSettings(settings) |
| 224 | if err != nil { |
| 225 | httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ |
| 226 | Message: "Failed to validate health settings.", |
| 227 | Detail: err.Error(), |
| 228 | }) |
| 229 | return |
| 230 | } |
| 231 | |
| 232 | settingsJSON, err := json.Marshal(&settings) |
| 233 | if err != nil { |
| 234 | httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ |
| 235 | Message: "Failed to marshal health settings.", |
| 236 | Detail: err.Error(), |
| 237 | }) |
| 238 | return |
| 239 | } |
| 240 | |
| 241 | currentSettingsJSON, err := api.Database.GetHealthSettings(r.Context()) |
| 242 | if err != nil { |
| 243 | httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ |
| 244 | Message: "Failed to fetch current health settings.", |
| 245 | Detail: err.Error(), |
| 246 | }) |
| 247 | return |
| 248 | } |
| 249 | |
| 250 | if bytes.Equal(settingsJSON, []byte(currentSettingsJSON)) { |
| 251 | // See: https://www.rfc-editor.org/rfc/rfc7231#section-6.3.5 |
| 252 | rw.WriteHeader(http.StatusNoContent) |
| 253 | return |
| 254 | } |
| 255 | |
| 256 | auditor := api.Auditor.Load() |
| 257 | aReq, commitAudit := audit.InitRequest[database.HealthSettings](rw, &audit.RequestParams{ |
| 258 | Audit: *auditor, |
| 259 | Log: api.Logger, |
| 260 | Request: r, |
| 261 | Action: database.AuditActionWrite, |
| 262 | }) |
| 263 | defer commitAudit() |
| 264 | |
| 265 | aReq.New = database.HealthSettings{ |
nothing calls this directly
no test coverage detected