@Summary Get workspace agent by ID @ID get-workspace-agent-by-id @Security CoderSessionToken @Produce json @Tags Agents @Param workspaceagent path string true "Workspace agent ID" format(uuid) @Success 200 {object} codersdk.WorkspaceAgent @Router /api/v2/workspaceagents/{workspaceagent} [get]
(rw http.ResponseWriter, r *http.Request)
| 64 | // @Success 200 {object} codersdk.WorkspaceAgent |
| 65 | // @Router /api/v2/workspaceagents/{workspaceagent} [get] |
| 66 | func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) { |
| 67 | var ( |
| 68 | ctx = r.Context() |
| 69 | waws = httpmw.WorkspaceAgentAndWorkspaceParam(r) |
| 70 | dbApps []database.WorkspaceApp |
| 71 | scripts []database.GetWorkspaceAgentScriptsByAgentIDsRow |
| 72 | logSources []database.WorkspaceAgentLogSource |
| 73 | ) |
| 74 | |
| 75 | var eg errgroup.Group |
| 76 | eg.Go(func() (err error) { |
| 77 | dbApps, err = api.Database.GetWorkspaceAppsByAgentID(ctx, waws.WorkspaceAgent.ID) |
| 78 | return err |
| 79 | }) |
| 80 | eg.Go(func() (err error) { |
| 81 | //nolint:gocritic // TODO: can we make this not require system restricted? |
| 82 | scripts, err = api.Database.GetWorkspaceAgentScriptsByAgentIDs(dbauthz.AsSystemRestricted(ctx), []uuid.UUID{waws.WorkspaceAgent.ID}) |
| 83 | return err |
| 84 | }) |
| 85 | eg.Go(func() (err error) { |
| 86 | //nolint:gocritic // TODO: can we make this not require system restricted? |
| 87 | logSources, err = api.Database.GetWorkspaceAgentLogSourcesByAgentIDs(dbauthz.AsSystemRestricted(ctx), []uuid.UUID{waws.WorkspaceAgent.ID}) |
| 88 | return err |
| 89 | }) |
| 90 | err := eg.Wait() |
| 91 | if httpapi.Is404Error(err) { |
| 92 | httpapi.ResourceNotFound(rw) |
| 93 | return |
| 94 | } |
| 95 | if err != nil { |
| 96 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 97 | Message: "Internal error fetching workspace agent.", |
| 98 | Detail: err.Error(), |
| 99 | }) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | appIDs := []uuid.UUID{} |
| 104 | for _, app := range dbApps { |
| 105 | appIDs = append(appIDs, app.ID) |
| 106 | } |
| 107 | // nolint:gocritic // This is a system restricted operation. |
| 108 | statuses, err := api.Database.GetWorkspaceAppStatusesByAppIDs(dbauthz.AsSystemRestricted(ctx), appIDs) |
| 109 | if err != nil { |
| 110 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 111 | Message: "Internal error fetching workspace app statuses.", |
| 112 | Detail: err.Error(), |
| 113 | }) |
| 114 | return |
| 115 | } |
| 116 | |
| 117 | apiAgent, err := db2sdk.WorkspaceAgent( |
| 118 | api.DERPMap(), *api.TailnetCoordinator.Load(), waws.WorkspaceAgent, db2sdk.Apps(dbApps, statuses, waws.WorkspaceAgent, waws.OwnerUsername, waws.WorkspaceTable), convertScripts(scripts), convertLogSources(logSources), api.AgentInactiveDisconnectTimeout, |
| 119 | api.DeploymentValues.AgentFallbackTroubleshootingURL.String(), |
| 120 | ) |
| 121 | if err != nil { |
| 122 | httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ |
| 123 | Message: "Internal error reading workspace agent.", |
nothing calls this directly
no test coverage detected