@Summary Get workspace metadata by user and workspace name @ID get-workspace-metadata-by-user-and-workspace-name @Security CoderSessionToken @Produce json @Tags Workspaces @Param user path string true "User ID, name, or me" @Param workspacename path string true "Workspace name" @Param include_delete
(rw http.ResponseWriter, r *http.Request)
| 272 | // @Success 200 {object} codersdk.Workspace |
| 273 | // @Router /api/v2/users/{user}/workspace/{workspacename} [get] |
| 274 | func (api *API) workspaceByOwnerAndName(rw http.ResponseWriter, r *http.Request) { |
| 275 | ctx := r.Context() |
| 276 | |
| 277 | mems := httpmw.OrganizationMembersParam(r) |
| 278 | workspaceName := chi.URLParam(r, "workspacename") |
| 279 | apiKey := httpmw.APIKey(r) |
| 280 | |
| 281 | includeDeleted := false |
| 282 | if s := r.URL.Query().Get("include_deleted"); s != "" { |
| 283 | var err error |
| 284 | includeDeleted, err = strconv.ParseBool(s) |
| 285 | if err != nil { |
| 286 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 287 | Message: fmt.Sprintf("Invalid boolean value %q for \"include_deleted\" query param.", s), |
| 288 | Validations: []codersdk.ValidationError{ |
| 289 | {Field: "include_deleted", Detail: "Must be a valid boolean"}, |
| 290 | }, |
| 291 | }) |
| 292 | return |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | workspace, err := api.Database.GetWorkspaceByOwnerIDAndName(ctx, database.GetWorkspaceByOwnerIDAndNameParams{ |
| 297 | OwnerID: mems.UserID(), |
| 298 | Name: workspaceName, |
| 299 | }) |
| 300 | if includeDeleted && errors.Is(err, sql.ErrNoRows) { |
| 301 | workspace, err = api.Database.GetWorkspaceByOwnerIDAndName(ctx, database.GetWorkspaceByOwnerIDAndNameParams{ |
| 302 | OwnerID: mems.UserID(), |
| 303 | Name: workspaceName, |
| 304 | Deleted: includeDeleted, |
| 305 | }) |
| 306 | } |
| 307 | if httpapi.Is404Error(err) { |
| 308 | httpapi.ResourceNotFound(rw) |
| 309 | return |
| 310 | } |
| 311 | if err != nil { |
| 312 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 313 | Message: "Internal error fetching workspace by name.", |
| 314 | Detail: err.Error(), |
| 315 | }) |
| 316 | return |
| 317 | } |
| 318 | |
| 319 | data, err := api.workspaceData(ctx, []database.Workspace{workspace}) |
| 320 | if err != nil { |
| 321 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 322 | Message: "Internal error fetching workspace resources.", |
| 323 | Detail: err.Error(), |
| 324 | }) |
| 325 | return |
| 326 | } |
| 327 | |
| 328 | if len(data.builds) == 0 || len(data.templates) == 0 { |
| 329 | httpapi.ResourceNotFound(rw) |
| 330 | return |
| 331 | } |
nothing calls this directly
no test coverage detected