Skip to content

Commit c844462

Browse files
committed
Fix legacy endpoint without specified log_source
1 parent b744c9f commit c844462

File tree

10 files changed

+100
-16
lines changed

10 files changed

+100
-16
lines changed

coderd/apidoc/docs.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dump.sql

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000155_workspace_agent_script.up.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
BEGIN;
22
CREATE TABLE workspace_agent_log_sources (
33
workspace_agent_id uuid NOT NULL,
4-
id uuid NOT NULL UNIQUE,
4+
id uuid NOT NULL,
55
created_at timestamptz NOT NULL,
66
display_name varchar(127) NOT NULL,
77
icon text NOT NULL,
88
PRIMARY KEY (workspace_agent_id, id)
99
);
1010

1111
CREATE TABLE workspace_agent_scripts (
12-
workspace_agent_id uuid NOT NULL,
13-
log_source_id uuid NOT NULL REFERENCES workspace_agent_log_sources(id) ON DELETE CASCADE,
12+
workspace_agent_id uuid NOT NULL REFERENCES workspace_agents(id) ON DELETE CASCADE,
13+
log_source_id uuid NOT NULL,
1414
log_path text NOT NULL,
1515
created_at timestamptz NOT NULL,
1616
script text NOT NULL,

coderd/database/unique_constraint.go

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/workspaceagents.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,33 @@ func (api *API) patchWorkspaceAgentLogs(rw http.ResponseWriter, r *http.Request)
308308
})
309309
return
310310
}
311+
// This is to support the legacy API where the log source ID was
312+
// not provided in the request body. We default to the external
313+
// log source in this case.
314+
if req.LogSourceID == uuid.Nil {
315+
// Use the external log source
316+
externalSources, err := api.Database.InsertWorkspaceAgentLogSources(ctx, database.InsertWorkspaceAgentLogSourcesParams{
317+
WorkspaceAgentID: workspaceAgent.ID,
318+
CreatedAt: dbtime.Now(),
319+
ID: []uuid.UUID{agentsdk.ExternalLogSourceID},
320+
DisplayName: []string{"External"},
321+
Icon: []string{"/emojis/1f310.png"},
322+
})
323+
if database.IsUniqueViolation(err, database.UniqueWorkspaceAgentLogSourcesPkey) {
324+
err = nil
325+
req.LogSourceID = agentsdk.ExternalLogSourceID
326+
}
327+
if err != nil {
328+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
329+
Message: "Failed to create external log source.",
330+
Detail: err.Error(),
331+
})
332+
return
333+
}
334+
if len(externalSources) == 1 {
335+
req.LogSourceID = externalSources[0].ID
336+
}
337+
}
311338
output := make([]string, 0)
312339
level := make([]database.LogLevel, 0)
313340
outputLength := 0

coderd/workspaceagents_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func TestWorkspaceAgent(t *testing.T) {
268268
})
269269
}
270270

271-
func TestWorkspaceAgentStartupLogs(t *testing.T) {
271+
func TestWorkspaceAgentLogs(t *testing.T) {
272272
t.Parallel()
273273
t.Run("Success", func(t *testing.T) {
274274
t.Parallel()

codersdk/agentsdk/agentsdk.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ import (
2323
"github.com/coder/retry"
2424
)
2525

26+
var (
27+
// ExternalLogSourceID is the statically-defined ID of a log-source that
28+
// appears as "External" in the dashboard.
29+
//
30+
// This is to support legacy API-consumers that do not create their own
31+
// log-source. This should be removed in the future.
32+
ExternalLogSourceID = uuid.MustParse("3b579bf4-1ed8-4b99-87a8-e9a1e3410410")
33+
)
34+
2635
// New returns a client that is used to interact with the
2736
// Coder API from a workspace agent.
2837
func New(serverURL *url.URL) *Client {
@@ -644,7 +653,7 @@ type Log struct {
644653
}
645654

646655
type PatchLogs struct {
647-
LogSourceID uuid.UUID `json:"log_source_id" validate:"required"`
656+
LogSourceID uuid.UUID `json:"log_source_id"`
648657
Logs []Log `json:"logs"`
649658
}
650659

@@ -662,6 +671,29 @@ func (c *Client) PatchLogs(ctx context.Context, req PatchLogs) error {
662671
return nil
663672
}
664673

674+
type PostLogSource struct {
675+
// ID is a unique identifier for the log source.
676+
// It is scoped to a workspace agent, and can be statically
677+
// defined inside code to prevent duplicate sources from being
678+
// created for the same agent.
679+
ID uuid.UUID `json:"id"`
680+
DisplayName string `json:"display_name"`
681+
Icon string `json:"icon"`
682+
}
683+
684+
func (c *Client) PostLogSource(ctx context.Context, req PostLogSource) (codersdk.WorkspaceAgentLogSource, error) {
685+
res, err := c.SDK.Request(ctx, http.MethodPost, "/api/v2/workspaceagents/me/log-source", req)
686+
if err != nil {
687+
return codersdk.WorkspaceAgentLogSource{}, err
688+
}
689+
defer res.Body.Close()
690+
if res.StatusCode != http.StatusCreated {
691+
return codersdk.WorkspaceAgentLogSource{}, codersdk.ReadBodyAsError(res)
692+
}
693+
var logSource codersdk.WorkspaceAgentLogSource
694+
return logSource, json.NewDecoder(res.Body).Decode(&logSource)
695+
}
696+
665697
// GetServiceBanner relays the service banner config.
666698
func (c *Client) GetServiceBanner(ctx context.Context) (codersdk.ServiceBannerConfig, error) {
667699
res, err := c.SDK.Request(ctx, http.MethodGet, "/api/v2/appearance", nil)

docs/api/schemas.md

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)