Skip to content

Commit 1c179a4

Browse files
committed
add workspace agent apps route
1 parent e28c366 commit 1c179a4

File tree

4 files changed

+100
-84
lines changed

4 files changed

+100
-84
lines changed

coderd/coderd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ func New(options *Options) *API {
413413
r.Get("/metadata", api.workspaceAgentMetadata)
414414
r.Post("/version", api.postWorkspaceAgentVersion)
415415
r.Post("/app-health", api.postWorkspaceAppHealth)
416+
r.Get("/apps", api.workspaceAgentApps)
416417
r.Get("/gitsshkey", api.agentGitSSHKey)
417418
r.Get("/coordinate", api.workspaceAgentCoordinate)
418419
r.Get("/report-stats", api.workspaceAgentReportStats)

coderd/coderdtest/authorize.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ 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},
6364
"GET:/api/v2/workspaceagents/me/report-stats": {NoAuthorize: true},
6465

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

coderd/workspaceagents.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,104 @@ func (api *API) workspaceAgentReportStats(rw http.ResponseWriter, r *http.Reques
681681
}
682682
}
683683

684+
func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request) {
685+
workspaceAgent := httpmw.WorkspaceAgent(r)
686+
var req codersdk.PostWorkspaceAppHealthsRequest
687+
if !httpapi.Read(rw, r, &req) {
688+
return
689+
}
690+
691+
apps, err := api.Database.GetWorkspaceAppsByAgentID(r.Context(), workspaceAgent.ID)
692+
if err != nil {
693+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
694+
Message: "Error getting agent apps",
695+
Detail: err.Error(),
696+
})
697+
return
698+
}
699+
700+
var newApps []database.WorkspaceApp
701+
for name, health := range req.Healths {
702+
found := func() *database.WorkspaceApp {
703+
for _, app := range apps {
704+
if app.Name == name {
705+
return &app
706+
}
707+
}
708+
709+
return nil
710+
}()
711+
if found == nil {
712+
httpapi.Write(rw, http.StatusNotFound, codersdk.Response{
713+
Message: "Error setting workspace app health",
714+
Detail: xerrors.Errorf("workspace app name %s not found", name).Error(),
715+
})
716+
return
717+
}
718+
719+
if !found.HealthcheckEnabled {
720+
httpapi.Write(rw, http.StatusNotFound, codersdk.Response{
721+
Message: "Error setting workspace app health",
722+
Detail: xerrors.Errorf("health checking is disabled for workspace app %s", name).Error(),
723+
})
724+
return
725+
}
726+
727+
switch health {
728+
case codersdk.WorkspaceAppHealthInitializing:
729+
found.Health = database.WorkspaceAppHealthInitializing
730+
case codersdk.WorkspaceAppHealthHealthy:
731+
found.Health = database.WorkspaceAppHealthHealthy
732+
case codersdk.WorkspaceAppHealthUnhealthy:
733+
found.Health = database.WorkspaceAppHealthUnhealthy
734+
default:
735+
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
736+
Message: "Error setting workspace app health",
737+
Detail: xerrors.Errorf("workspace app health %s is not a valid value", health).Error(),
738+
})
739+
return
740+
}
741+
742+
// don't save if the value hasn't changed
743+
if found.Health == database.WorkspaceAppHealth(health) {
744+
continue
745+
}
746+
747+
newApps = append(newApps, *found)
748+
}
749+
750+
for _, app := range newApps {
751+
err = api.Database.UpdateWorkspaceAppHealthByID(r.Context(), database.UpdateWorkspaceAppHealthByIDParams{
752+
ID: app.ID,
753+
Health: app.Health,
754+
})
755+
if err != nil {
756+
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
757+
Message: "Error setting workspace app health",
758+
Detail: err.Error(),
759+
})
760+
return
761+
}
762+
}
763+
764+
httpapi.Write(rw, http.StatusOK, nil)
765+
}
766+
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+
684782
// wsNetConn wraps net.Conn created by websocket.NetConn(). Cancel func
685783
// is called if a read or write error is encountered.
686784
type wsNetConn struct {

coderd/workspaceapps.go

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/go-chi/chi/v5"
1212
"go.opentelemetry.io/otel/trace"
13-
"golang.org/x/xerrors"
1413

1514
"github.com/coder/coder/coderd/database"
1615
"github.com/coder/coder/coderd/httpapi"
@@ -255,86 +254,3 @@ func (api *API) applicationCookie(authCookie *http.Cookie) *http.Cookie {
255254
appCookie.Domain = "." + api.AccessURL.Hostname()
256255
return &appCookie
257256
}
258-
259-
func (api *API) postWorkspaceAppHealth(rw http.ResponseWriter, r *http.Request) {
260-
workspaceAgent := httpmw.WorkspaceAgent(r)
261-
var req codersdk.PostWorkspaceAppHealthsRequest
262-
if !httpapi.Read(rw, r, &req) {
263-
return
264-
}
265-
266-
apps, err := api.Database.GetWorkspaceAppsByAgentID(r.Context(), workspaceAgent.ID)
267-
if err != nil {
268-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
269-
Message: "Error getting agent apps",
270-
Detail: err.Error(),
271-
})
272-
return
273-
}
274-
275-
var newApps []database.WorkspaceApp
276-
for name, health := range req.Healths {
277-
found := func() *database.WorkspaceApp {
278-
for _, app := range apps {
279-
if app.Name == name {
280-
return &app
281-
}
282-
}
283-
284-
return nil
285-
}()
286-
if found == nil {
287-
httpapi.Write(rw, http.StatusNotFound, codersdk.Response{
288-
Message: "Error setting workspace app health",
289-
Detail: xerrors.Errorf("workspace app name %s not found", name).Error(),
290-
})
291-
return
292-
}
293-
294-
if !found.HealthcheckEnabled {
295-
httpapi.Write(rw, http.StatusNotFound, codersdk.Response{
296-
Message: "Error setting workspace app health",
297-
Detail: xerrors.Errorf("health checking is disabled for workspace app %s", name).Error(),
298-
})
299-
return
300-
}
301-
302-
switch health {
303-
case codersdk.WorkspaceAppHealthInitializing:
304-
found.Health = database.WorkspaceAppHealthInitializing
305-
case codersdk.WorkspaceAppHealthHealthy:
306-
found.Health = database.WorkspaceAppHealthHealthy
307-
case codersdk.WorkspaceAppHealthUnhealthy:
308-
found.Health = database.WorkspaceAppHealthUnhealthy
309-
default:
310-
httpapi.Write(rw, http.StatusBadRequest, codersdk.Response{
311-
Message: "Error setting workspace app health",
312-
Detail: xerrors.Errorf("workspace app health %s is not a valid value", health).Error(),
313-
})
314-
return
315-
}
316-
317-
// don't save if the value hasn't changed
318-
if found.Health == database.WorkspaceAppHealth(health) {
319-
continue
320-
}
321-
322-
newApps = append(newApps, *found)
323-
}
324-
325-
for _, app := range newApps {
326-
err = api.Database.UpdateWorkspaceAppHealthByID(r.Context(), database.UpdateWorkspaceAppHealthByIDParams{
327-
ID: app.ID,
328-
Health: app.Health,
329-
})
330-
if err != nil {
331-
httpapi.Write(rw, http.StatusInternalServerError, codersdk.Response{
332-
Message: "Error setting workspace app health",
333-
Detail: err.Error(),
334-
})
335-
return
336-
}
337-
}
338-
339-
httpapi.Write(rw, http.StatusOK, nil)
340-
}

0 commit comments

Comments
 (0)