@Summary Delete license @ID delete-license @Security CoderSessionToken @Produce json @Tags Enterprise @Param id path string true "License ID" format(number) @Success 200 @Router /api/v2/licenses/{id} [delete]
(rw http.ResponseWriter, r *http.Request)
| 275 | // @Success 200 |
| 276 | // @Router /api/v2/licenses/{id} [delete] |
| 277 | func (api *API) deleteLicense(rw http.ResponseWriter, r *http.Request) { |
| 278 | var ( |
| 279 | ctx = r.Context() |
| 280 | auditor = api.AGPL.Auditor.Load() |
| 281 | ) |
| 282 | |
| 283 | idStr := chi.URLParam(r, "id") |
| 284 | id, err := strconv.ParseInt(idStr, 10, 32) |
| 285 | if err != nil { |
| 286 | httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{ |
| 287 | Message: "License ID must be an integer", |
| 288 | }) |
| 289 | return |
| 290 | } |
| 291 | |
| 292 | dl, err := api.Database.GetLicenseByID(ctx, int32(id)) |
| 293 | if err != nil { |
| 294 | // don't fail the HTTP request simply because we cannot audit |
| 295 | api.Logger.Warn(context.Background(), "could not retrieve license; cannot audit", slog.Error(err)) |
| 296 | } |
| 297 | |
| 298 | aReq, commitAudit := audit.InitRequest[database.License](rw, &audit.RequestParams{ |
| 299 | Audit: *auditor, |
| 300 | Log: api.Logger, |
| 301 | Request: r, |
| 302 | Action: database.AuditActionDelete, |
| 303 | }) |
| 304 | defer commitAudit() |
| 305 | aReq.Old = dl |
| 306 | |
| 307 | if !api.AGPL.Authorize(r, policy.ActionDelete, rbac.ResourceLicense) { |
| 308 | httpapi.Forbidden(rw) |
| 309 | return |
| 310 | } |
| 311 | |
| 312 | _, err = api.Database.DeleteLicense(ctx, int32(id)) |
| 313 | if httpapi.Is404Error(err) { |
| 314 | httpapi.Write(ctx, rw, http.StatusNotFound, codersdk.Response{ |
| 315 | Message: "Unknown license ID", |
| 316 | }) |
| 317 | return |
| 318 | } |
| 319 | if err != nil { |
| 320 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 321 | Message: "Internal error deleting license", |
| 322 | Detail: err.Error(), |
| 323 | }) |
| 324 | return |
| 325 | } |
| 326 | err = api.updateEntitlements(ctx) |
| 327 | if err != nil { |
| 328 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 329 | Message: "Failed to update entitlements", |
| 330 | Detail: err.Error(), |
| 331 | }) |
| 332 | return |
| 333 | } |
| 334 | err = api.Pubsub.Publish(PubsubEventLicenses, []byte("delete")) |
nothing calls this directly
no test coverage detected