Skip to content

Commit cce51ef

Browse files
committed
fix: add missing route for codersdk.PostLogSource
1 parent 4758952 commit cce51ef

File tree

10 files changed

+269
-4
lines changed

10 files changed

+269
-4
lines changed

coderd/apidoc/docs.go

+54
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apikey/apikey_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestGenerate(t *testing.T) {
128128

129129
// Assert that the hashed secret is correct.
130130
hashed := sha256.Sum256([]byte(keytokens[1]))
131-
assert.ElementsMatch(t, hashed, key.HashedSecret[:])
131+
assert.ElementsMatch(t, hashed, key.HashedSecret)
132132

133133
assert.Equal(t, tc.params.UserID, key.UserID)
134134
assert.WithinDuration(t, dbtime.Now(), key.CreatedAt, time.Second*5)

coderd/coderd.go

+1
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@ func New(options *Options) *API {
10211021
r.Post("/report-lifecycle", api.workspaceAgentReportLifecycle)
10221022
r.Post("/metadata", api.workspaceAgentPostMetadata)
10231023
r.Post("/metadata/{key}", api.workspaceAgentPostMetadataDeprecated)
1024+
r.Post("/log-source", api.workspaceAgentPostLogSource)
10241025
})
10251026
r.Route("/{workspaceagent}", func(r chi.Router) {
10261027
r.Use(

coderd/workspaceagents.go

+50
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,56 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R
10841084
}
10851085
}
10861086

1087+
// @Summary Post workspace agent log source
1088+
// @ID post-workspace-agent-log-source
1089+
// @Security CoderSessionToken
1090+
// @Accept json
1091+
// @Produce json
1092+
// @Tags Agents
1093+
// @Param request body agentsdk.PostLogSourceRequest true "Log source request"
1094+
// @Success 200 {object} codersdk.WorkspaceAgentLogSource
1095+
// @Router /workspaceagents/me/log-source [post]
1096+
func (api *API) workspaceAgentPostLogSource(rw http.ResponseWriter, r *http.Request) {
1097+
ctx := r.Context()
1098+
var req agentsdk.PostLogSourceRequest
1099+
if !httpapi.Read(ctx, rw, r, &req) {
1100+
return
1101+
}
1102+
1103+
workspaceAgent := httpmw.WorkspaceAgent(r)
1104+
1105+
sources, err := api.Database.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{
1106+
WorkspaceAgentID: workspaceAgent.ID,
1107+
CreatedAt: dbtime.Now(),
1108+
ID: []uuid.UUID{req.ID},
1109+
DisplayName: []string{req.DisplayName},
1110+
Icon: []string{req.Icon},
1111+
})
1112+
if err != nil {
1113+
if database.IsUniqueViolation(err, "workspace_agent_log_sources_pkey") {
1114+
httpapi.Write(ctx, rw, http.StatusCreated, codersdk.WorkspaceAgentLogSource{
1115+
WorkspaceAgentID: workspaceAgent.ID,
1116+
CreatedAt: dbtime.Now(),
1117+
ID: req.ID,
1118+
DisplayName: req.DisplayName,
1119+
Icon: req.Icon,
1120+
})
1121+
return
1122+
}
1123+
httpapi.InternalServerError(rw, err)
1124+
return
1125+
}
1126+
1127+
if len(sources) != 1 {
1128+
httpapi.InternalServerError(rw, xerrors.Errorf("database should've returned 1 row, got %d", len(sources)))
1129+
return
1130+
}
1131+
1132+
apiSource := convertLogSources(sources)[0]
1133+
1134+
httpapi.Write(ctx, rw, http.StatusCreated, apiSource)
1135+
}
1136+
10871137
// convertProvisionedApps converts applications that are in the middle of provisioning process.
10881138
// It means that they may not have an agent or workspace assigned (dry-run job).
10891139
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {

coderd/workspaceagents_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,48 @@ func TestWorkspaceAgentAppHealth(t *testing.T) {
921921
require.EqualValues(t, codersdk.WorkspaceAppHealthUnhealthy, manifest.Apps[1].Health)
922922
}
923923

924+
func TestWorkspaceAgentPostLogSource(t *testing.T) {
925+
t.Parallel()
926+
927+
t.Run("OK", func(t *testing.T) {
928+
t.Parallel()
929+
client, db := coderdtest.NewWithDatabase(t, nil)
930+
user := coderdtest.CreateFirstUser(t, client)
931+
ctx := testutil.Context(t, testutil.WaitShort)
932+
933+
r := dbfake.WorkspaceBuild(t, db, database.Workspace{
934+
OrganizationID: user.OrganizationID,
935+
OwnerID: user.UserID,
936+
}).WithAgent().Do()
937+
938+
agentClient := agentsdk.New(client.URL)
939+
agentClient.SetSessionToken(r.AgentToken)
940+
941+
req := agentsdk.PostLogSourceRequest{
942+
ID: uuid.New(),
943+
DisplayName: "colin logs",
944+
Icon: "/emojis/1f42e.png",
945+
}
946+
947+
res, err := agentClient.PostLogSource(ctx, req)
948+
require.NoError(t, err)
949+
assert.Equal(t, req.ID, res.ID)
950+
assert.Equal(t, req.DisplayName, res.DisplayName)
951+
assert.Equal(t, req.Icon, res.Icon)
952+
assert.NotZero(t, res.WorkspaceAgentID)
953+
assert.NotZero(t, res.CreatedAt)
954+
955+
// should be idempotent
956+
res, err = agentClient.PostLogSource(ctx, req)
957+
require.NoError(t, err)
958+
assert.Equal(t, req.ID, res.ID)
959+
assert.Equal(t, req.DisplayName, res.DisplayName)
960+
assert.Equal(t, req.Icon, res.Icon)
961+
assert.NotZero(t, res.WorkspaceAgentID)
962+
assert.NotZero(t, res.CreatedAt)
963+
})
964+
}
965+
924966
// TestWorkspaceAgentReportStats tests the legacy (agent API v1) report stats endpoint.
925967
func TestWorkspaceAgentReportStats(t *testing.T) {
926968
t.Parallel()

coderd/workspaceapps/proxy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func (s *Server) proxyWorkspaceApp(rw http.ResponseWriter, r *http.Request, appT
573573
}
574574

575575
// This strips the session token from a workspace app request.
576-
cookieHeaders := r.Header.Values("Cookie")[:]
576+
cookieHeaders := r.Header.Values("Cookie")
577577
r.Header.Del("Cookie")
578578
for _, cookieHeader := range cookieHeaders {
579579
r.Header.Add("Cookie", httpapi.StripCoderCookies(cookieHeader))

codersdk/agentsdk/agentsdk.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ func (c *Client) PatchLogs(ctx context.Context, req PatchLogs) error {
533533
return nil
534534
}
535535

536-
type PostLogSource struct {
536+
type PostLogSourceRequest struct {
537537
// ID is a unique identifier for the log source.
538538
// It is scoped to a workspace agent, and can be statically
539539
// defined inside code to prevent duplicate sources from being
@@ -543,7 +543,7 @@ type PostLogSource struct {
543543
Icon string `json:"icon"`
544544
}
545545

546-
func (c *Client) PostLogSource(ctx context.Context, req PostLogSource) (codersdk.WorkspaceAgentLogSource, error) {
546+
func (c *Client) PostLogSource(ctx context.Context, req PostLogSourceRequest) (codersdk.WorkspaceAgentLogSource, error) {
547547
res, err := c.SDK.Request(ctx, http.MethodPost, "/api/v2/workspaceagents/me/log-source", req)
548548
if err != nil {
549549
return codersdk.WorkspaceAgentLogSource{}, err

docs/api/agents.md

+52
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/schemas.md

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)