Skip to content

Commit 6df6998

Browse files
committed
add myWorkspaceAgent
1 parent 1c179a4 commit 6df6998

File tree

4 files changed

+37
-17
lines changed

4 files changed

+37
-17
lines changed

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,10 @@ func New(options *Options) *API {
410410
r.Post("/google-instance-identity", api.postWorkspaceAuthGoogleInstanceIdentity)
411411
r.Route("/me", func(r chi.Router) {
412412
r.Use(httpmw.ExtractWorkspaceAgent(options.Database))
413+
r.Get("/", api.myWorkspaceAgent)
413414
r.Get("/metadata", api.workspaceAgentMetadata)
414415
r.Post("/version", api.postWorkspaceAgentVersion)
415416
r.Post("/app-health", api.postWorkspaceAppHealth)
416-
r.Get("/apps", api.workspaceAgentApps)
417417
r.Get("/gitsshkey", api.agentGitSSHKey)
418418
r.Get("/coordinate", api.workspaceAgentCoordinate)
419419
r.Get("/report-stats", api.workspaceAgentReportStats)

coderd/coderdtest/authorize.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
6060
"GET:/api/v2/workspaceagents/me/coordinate": {NoAuthorize: true},
6161
"POST:/api/v2/workspaceagents/me/version": {NoAuthorize: true},
6262
"POST:/api/v2/workspaceagents/me/app-health": {NoAuthorize: true},
63-
"GET:/api/v2/workspaceagents/me/apps": {NoAuthorize: true},
6463
"GET:/api/v2/workspaceagents/me/report-stats": {NoAuthorize: true},
6564

6665
// These endpoints have more assertions. This is good, add more endpoints to assert if you can!

coderd/workspaceagents.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,28 @@ func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) {
6161
httpapi.Write(ctx, rw, http.StatusOK, apiAgent)
6262
}
6363

64+
func (api *API) myWorkspaceAgent(rw http.ResponseWriter, r *http.Request) {
65+
workspaceAgent := httpmw.WorkspaceAgent(r)
66+
dbApps, err := api.Database.GetWorkspaceAppsByAgentID(r.Context(), workspaceAgent.ID)
67+
if err != nil && !xerrors.Is(err, sql.ErrNoRows) {
68+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
69+
Message: "Internal error fetching workspace agent applications.",
70+
Detail: err.Error(),
71+
})
72+
return
73+
}
74+
apiAgent, err := convertWorkspaceAgent(api.DERPMap, api.TailnetCoordinator, workspaceAgent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout)
75+
if err != nil {
76+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
77+
Message: "Internal error reading workspace agent.",
78+
Detail: err.Error(),
79+
})
80+
return
81+
}
82+
83+
httpapi.Write(rw, http.StatusOK, apiAgent)
84+
}
85+
6486
func (api *API) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request) {
6587
ctx := r.Context()
6688
workspaceAgent := httpmw.WorkspaceAgent(r)
@@ -764,21 +786,6 @@ func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request)
764786
httpapi.Write(rw, http.StatusOK, nil)
765787
}
766788

767-
func (api *API) workspaceAgentApps(rw http.ResponseWriter, r *http.Request) {
768-
workspaceAgent := httpmw.WorkspaceAgent(r)
769-
770-
apps, err := api.Database.GetWorkspaceAppsByAgentID(r.Context(), workspaceAgent.ID)
771-
if err != nil {
772-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
773-
Message: "Internal error fetching workspace agent applications.",
774-
Detail: err.Error(),
775-
})
776-
return
777-
}
778-
779-
httpapi.Write(rw, http.StatusOK, convertApps(apps))
780-
}
781-
782789
// wsNetConn wraps net.Conn created by websocket.NetConn(). Cancel func
783790
// is called if a read or write error is encountered.
784791
type wsNetConn struct {

codersdk/workspaceagents.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,20 @@ func (c *Client) WorkspaceAgent(ctx context.Context, id uuid.UUID) (WorkspaceAge
348348
return workspaceAgent, json.NewDecoder(res.Body).Decode(&workspaceAgent)
349349
}
350350

351+
// WorkspaceAgent returns the requesting agent.
352+
func (c *Client) MyWorkspaceAgent(ctx context.Context) (WorkspaceAgent, error) {
353+
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaceagents/me", nil)
354+
if err != nil {
355+
return WorkspaceAgent{}, err
356+
}
357+
defer res.Body.Close()
358+
if res.StatusCode != http.StatusOK {
359+
return WorkspaceAgent{}, readBodyAsError(res)
360+
}
361+
var workspaceAgent WorkspaceAgent
362+
return workspaceAgent, json.NewDecoder(res.Body).Decode(&workspaceAgent)
363+
}
364+
351365
func (c *Client) PostWorkspaceAgentVersion(ctx context.Context, version string) error {
352366
// Phone home and tell the mothership what version we're on.
353367
versionReq := PostWorkspaceAgentVersionRequest{Version: version}

0 commit comments

Comments
 (0)