Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ea09d5

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

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

coderd/apikey/apikey_test.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,47 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R
10841084
}
10851085
}
10861086

1087+
func (api *API) workspaceAgentPostLogSource(rw http.ResponseWriter, r *http.Request) {
1088+
ctx := r.Context()
1089+
var req agentsdk.PostLogSourceRequest
1090+
if !httpapi.Read(ctx, rw, r, &req) {
1091+
return
1092+
}
1093+
1094+
workspaceAgent := httpmw.WorkspaceAgent(r)
1095+
1096+
sources, err := api.Database.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{
1097+
WorkspaceAgentID: workspaceAgent.ID,
1098+
CreatedAt: dbtime.Now(),
1099+
ID: []uuid.UUID{req.ID},
1100+
DisplayName: []string{req.DisplayName},
1101+
Icon: []string{req.Icon},
1102+
})
1103+
if err != nil {
1104+
if database.IsUniqueViolation(err, "workspace_agent_log_sources_pkey") {
1105+
httpapi.Write(ctx, rw, http.StatusCreated, codersdk.WorkspaceAgentLogSource{
1106+
WorkspaceAgentID: workspaceAgent.ID,
1107+
CreatedAt: dbtime.Now(),
1108+
ID: req.ID,
1109+
DisplayName: req.DisplayName,
1110+
Icon: req.Icon,
1111+
})
1112+
return
1113+
}
1114+
httpapi.InternalServerError(rw, err)
1115+
return
1116+
}
1117+
1118+
if len(sources) != 1 {
1119+
httpapi.InternalServerError(rw, xerrors.Errorf("database should've returned 1 row, got %d", len(sources)))
1120+
return
1121+
}
1122+
1123+
apiSource := convertLogSources(sources)[0]
1124+
1125+
httpapi.Write(ctx, rw, http.StatusCreated, apiSource)
1126+
}
1127+
10871128
// convertProvisionedApps converts applications that are in the middle of provisioning process.
10881129
// It means that they may not have an agent or workspace assigned (dry-run job).
10891130
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {

coderd/workspaceagents_test.go

Lines changed: 42 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)