@Summary Delete organization @ID delete-organization @Security CoderSessionToken @Produce json @Tags Organizations @Param organization path string true "Organization ID or name" @Success 200 {object} codersdk.Response @Router /api/v2/organizations/{organization} [delete]
(rw http.ResponseWriter, r *http.Request)
| 131 | // @Success 200 {object} codersdk.Response |
| 132 | // @Router /api/v2/organizations/{organization} [delete] |
| 133 | func (api *API) deleteOrganization(rw http.ResponseWriter, r *http.Request) { |
| 134 | var ( |
| 135 | ctx = r.Context() |
| 136 | organization = httpmw.OrganizationParam(r) |
| 137 | auditor = api.AGPL.Auditor.Load() |
| 138 | aReq, commitAudit = audit.InitRequest[database.Organization](rw, &audit.RequestParams{ |
| 139 | Audit: *auditor, |
| 140 | Log: api.Logger, |
| 141 | Request: r, |
| 142 | Action: database.AuditActionDelete, |
| 143 | OrganizationID: organization.ID, |
| 144 | }) |
| 145 | ) |
| 146 | aReq.Old = organization |
| 147 | defer commitAudit() |
| 148 | |
| 149 | if organization.IsDefault { |
| 150 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 151 | Message: "Default organization cannot be deleted.", |
| 152 | }) |
| 153 | return |
| 154 | } |
| 155 | |
| 156 | err := api.Database.InTx(func(tx database.Store) error { |
| 157 | err := tx.UpdateOrganizationDeletedByID(ctx, database.UpdateOrganizationDeletedByIDParams{ |
| 158 | ID: organization.ID, |
| 159 | UpdatedAt: dbtime.Now(), |
| 160 | }) |
| 161 | if err != nil { |
| 162 | return xerrors.Errorf("delete organization: %w", err) |
| 163 | } |
| 164 | return nil |
| 165 | }, nil) |
| 166 | if err != nil { |
| 167 | orgResourcesRow, queryErr := api.Database.GetOrganizationResourceCountByID(ctx, organization.ID) |
| 168 | if queryErr != nil { |
| 169 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 170 | Message: "Internal error deleting organization.", |
| 171 | Detail: fmt.Sprintf("delete organization: %s", err.Error()), |
| 172 | }) |
| 173 | |
| 174 | return |
| 175 | } |
| 176 | |
| 177 | detailParts := make([]string, 0) |
| 178 | |
| 179 | addDetailPart := func(resource string, count int64) { |
| 180 | if count == 1 { |
| 181 | detailParts = append(detailParts, fmt.Sprintf("1 %s", resource)) |
| 182 | } else if count > 1 { |
| 183 | detailParts = append(detailParts, fmt.Sprintf("%d %ss", count, resource)) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | addDetailPart("workspace", orgResourcesRow.WorkspaceCount) |
| 188 | addDetailPart("template", orgResourcesRow.TemplateCount) |
| 189 | |
| 190 | // There will always be one member and group so instead we need to check that |
nothing calls this directly
no test coverage detected