|
4 | 4 | "database/sql"
|
5 | 5 | "fmt"
|
6 | 6 | "net/http"
|
| 7 | + "strings" |
7 | 8 |
|
8 | 9 | "github.com/google/uuid"
|
9 | 10 | "golang.org/x/xerrors"
|
@@ -161,10 +162,41 @@ func (api *API) deleteOrganization(rw http.ResponseWriter, r *http.Request) {
|
161 | 162 | return nil
|
162 | 163 | }, nil)
|
163 | 164 | if err != nil {
|
| 165 | + orgResourcesRow, queryErr := api.Database.GetOrganizationResourceCountByID(ctx, organization.ID) |
| 166 | + if queryErr != nil { |
| 167 | + httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 168 | + Message: "Internal error deleting organization.", |
| 169 | + Detail: fmt.Sprintf("delete organization: %s", err.Error()), |
| 170 | + }) |
| 171 | + |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + detailParts := make([]string, 0) |
| 176 | + |
| 177 | + addDetailPart := func(resource string, count int64) { |
| 178 | + if count == 1 { |
| 179 | + detailParts = append(detailParts, fmt.Sprintf("1 %s", resource)) |
| 180 | + } else if count > 1 { |
| 181 | + detailParts = append(detailParts, fmt.Sprintf("%d %ss", count, resource)) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + addDetailPart("workspace", orgResourcesRow.WorkspaceCount) |
| 186 | + addDetailPart("template", orgResourcesRow.TemplateCount) |
| 187 | + |
| 188 | + // There will always be one member and group so instead we need to check that |
| 189 | + // the count is greater than one. |
| 190 | + addDetailPart("member", orgResourcesRow.MemberCount-1) |
| 191 | + addDetailPart("group", orgResourcesRow.GroupCount-1) |
| 192 | + |
| 193 | + addDetailPart("provisioner key", orgResourcesRow.ProvisionerKeyCount) |
| 194 | + |
164 | 195 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
165 |
| - Message: "Internal error deleting organization.", |
166 |
| - Detail: fmt.Sprintf("delete organization: %s", err.Error()), |
| 196 | + Message: "Error deleting organization.", |
| 197 | + Detail: fmt.Sprintf("This organization has %s that must be deleted first.", strings.Join(detailParts, ", ")), |
167 | 198 | })
|
| 199 | + |
168 | 200 | return
|
169 | 201 | }
|
170 | 202 |
|
|
0 commit comments