Skip to content

Commit 9114f66

Browse files
committed
Cleanup routes
1 parent 07f21aa commit 9114f66

10 files changed

+80
-84
lines changed

coderd/coderd.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ func New(options *Options) (http.Handler, func()) {
6060
r.Post("/keys", api.postKeyForUser)
6161
})
6262
})
63+
64+
r.Route("/project/import/{organization}", func(r chi.Router) {
65+
r.Use(
66+
httpmw.ExtractAPIKey(options.Database, nil),
67+
httpmw.ExtractOrganizationParam(options.Database),
68+
)
69+
r.Post("/", api.postProjectImportByOrganization)
70+
r.Route("/{provisionerjob}", func(r chi.Router) {
71+
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
72+
r.Get("/", api.provisionerJobByID)
73+
r.Get("/schemas", api.projectImportJobSchemasByID)
74+
r.Get("/parameters", api.projectImportJobParametersByID)
75+
r.Get("/resources", api.projectImportJobResourcesByID)
76+
r.Get("/logs", api.provisionerJobLogsByID)
77+
})
78+
})
6379
r.Route("/projects", func(r chi.Router) {
6480
r.Use(
6581
httpmw.ExtractAPIKey(options.Database, nil),
@@ -89,8 +105,17 @@ func New(options *Options) (http.Handler, func()) {
89105
})
90106
})
91107

92-
// Listing operations specific to resources should go under
93-
// their respective routes. eg. /orgs/<name>/workspaces
108+
r.Route("/workspace/provision/{organization}", func(r chi.Router) {
109+
r.Use(
110+
httpmw.ExtractAPIKey(options.Database, nil),
111+
httpmw.ExtractOrganizationParam(options.Database),
112+
)
113+
r.Route("/{provisionerjob}", func(r chi.Router) {
114+
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
115+
r.Get("/", api.provisionerJobByID)
116+
r.Get("/logs", api.provisionerJobLogsByID)
117+
})
118+
})
94119
r.Route("/workspaces", func(r chi.Router) {
95120
r.Use(httpmw.ExtractAPIKey(options.Database, nil))
96121
r.Get("/", api.workspaces)
@@ -100,7 +125,7 @@ func New(options *Options) (http.Handler, func()) {
100125
r.Route("/{workspace}", func(r chi.Router) {
101126
r.Use(httpmw.ExtractWorkspaceParam(options.Database))
102127
r.Get("/", api.workspaceByUser)
103-
r.Route("/version", func(r chi.Router) {
128+
r.Route("/history", func(r chi.Router) {
104129
r.Post("/", api.postWorkspaceHistoryByUser)
105130
r.Get("/", api.workspaceHistoryByUser)
106131
r.Route("/{workspacehistory}", func(r chi.Router) {
@@ -113,8 +138,11 @@ func New(options *Options) (http.Handler, func()) {
113138
})
114139

115140
r.Route("/agent", func(r chi.Router) {
116-
r.Route("/authenticate", func(r chi.Router) {
117-
r.Post("/google-instance-identity", api.postAuthenticateAgentUsingGoogleInstanceIdentity)
141+
r.Route("/auth", func(r chi.Router) {
142+
r.Post("/google-instance-identity", api.postAuthWorkspaceAgentUsingGoogleInstanceIdentity)
143+
})
144+
r.Route("/{agent}", func(r chi.Router) {
145+
118146
})
119147
})
120148

@@ -123,34 +151,6 @@ func New(options *Options) (http.Handler, func()) {
123151
r.Post("/", api.postUpload)
124152
})
125153

126-
r.Route("/projectimport/{organization}", func(r chi.Router) {
127-
r.Use(
128-
httpmw.ExtractAPIKey(options.Database, nil),
129-
httpmw.ExtractOrganizationParam(options.Database),
130-
)
131-
r.Post("/", api.postProjectImportByOrganization)
132-
r.Route("/{provisionerjob}", func(r chi.Router) {
133-
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
134-
r.Get("/", api.provisionerJobByID)
135-
r.Get("/schemas", api.projectImportJobSchemasByID)
136-
r.Get("/parameters", api.projectImportJobParametersByID)
137-
r.Get("/resources", api.projectImportJobResourcesByID)
138-
r.Get("/logs", api.provisionerJobLogsByID)
139-
})
140-
})
141-
142-
r.Route("/workspaceprovision/{organization}", func(r chi.Router) {
143-
r.Use(
144-
httpmw.ExtractAPIKey(options.Database, nil),
145-
httpmw.ExtractOrganizationParam(options.Database),
146-
)
147-
r.Route("/{provisionerjob}", func(r chi.Router) {
148-
r.Use(httpmw.ExtractProvisionerJobParam(options.Database))
149-
r.Get("/", api.provisionerJobByID)
150-
r.Get("/logs", api.provisionerJobLogsByID)
151-
})
152-
})
153-
154154
r.Route("/provisioners/daemons", func(r chi.Router) {
155155
r.Get("/", api.provisionerDaemons)
156156
r.Get("/serve", api.provisionerDaemonsServe)

coderd/agent.go renamed to coderd/workspaceagent.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ import (
1515
"github.com/coder/coder/provisionersdk"
1616
)
1717

18-
type AgentResourceMetadata struct {
18+
type WorkspaceAgentResourceMetadata struct {
1919
MemoryTotal uint64 `json:"memory_total"`
2020
DiskTotal uint64 `json:"disk_total"`
2121
CPUCores uint64 `json:"cpu_cores"`
2222
CPUModel string `json:"cpu_model"`
2323
CPUMhz float64 `json:"cpu_mhz"`
2424
}
2525

26-
type AgentInstanceMetadata struct {
26+
type WorkspaceAgentInstanceMetadata struct {
2727
JailOrchestrator string `json:"jail_orchestrator"`
2828
OperatingSystem string `json:"operating_system"`
2929
Platform string `json:"platform"`
@@ -35,15 +35,11 @@ type AgentInstanceMetadata struct {
3535
VNC bool `json:"vnc"`
3636
}
3737

38-
func (api *api) agentUpdate() {
39-
40-
}
41-
42-
func (api *api) agentConnectByResource(rw http.ResponseWriter, r *http.Request) {
38+
func (api *api) workspaceAgentConnectByResource(rw http.ResponseWriter, r *http.Request) {
4339
api.websocketWaitGroup.Add(1)
4440
defer api.websocketWaitGroup.Done()
4541

46-
agent := httpmw.Agent(r)
42+
agent := httpmw.WorkspaceAgent(r)
4743
if !agent.UpdatedAt.Valid {
4844
httpapi.Write(rw, http.StatusPreconditionRequired, httpapi.Response{
4945
Message: "Agent hasn't connected yet!",
@@ -81,11 +77,11 @@ func (api *api) agentConnectByResource(rw http.ResponseWriter, r *http.Request)
8177
}
8278
}
8379

84-
func (api *api) agentServe(rw http.ResponseWriter, r *http.Request) {
80+
func (api *api) workspaceAgentServe(rw http.ResponseWriter, r *http.Request) {
8581
api.websocketWaitGroup.Add(1)
8682
defer api.websocketWaitGroup.Done()
8783

88-
agent := httpmw.Agent(r)
84+
agent := httpmw.WorkspaceAgent(r)
8985
conn, err := websocket.Accept(rw, r, &websocket.AcceptOptions{
9086
CompressionMode: websocket.CompressionDisabled,
9187
})

coderd/agentauth.go renamed to coderd/workspaceagentauth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ type GoogleInstanceIdentityToken struct {
1919
JSONWebToken string `json:"json_web_token" validate:"required"`
2020
}
2121

22-
// AgentAuthResponse is returned when an instance ID
22+
// WorkspaceAgentAuthResponse is returned when an instance ID
2323
// has been exchanged for a session token.
24-
type AgentAuthResponse struct {
24+
type WorkspaceAgentAuthResponse struct {
2525
SessionToken string `json:"session_token"`
2626
}
2727

2828
// Google Compute Engine supports instance identity verification:
2929
// https://cloud.google.com/compute/docs/instances/verifying-instance-identity
3030
// Using this, we can exchange a signed instance payload for an agent token.
31-
func (api *api) postAuthenticateAgentUsingGoogleInstanceIdentity(rw http.ResponseWriter, r *http.Request) {
31+
func (api *api) postAuthWorkspaceAgentUsingGoogleInstanceIdentity(rw http.ResponseWriter, r *http.Request) {
3232
var req GoogleInstanceIdentityToken
3333
if !httpapi.Read(rw, r, &req) {
3434
return
@@ -121,7 +121,7 @@ func (api *api) postAuthenticateAgentUsingGoogleInstanceIdentity(rw http.Respons
121121
return
122122
}
123123
render.Status(r, http.StatusOK)
124-
render.JSON(rw, r, AgentAuthResponse{
124+
render.JSON(rw, r, WorkspaceAgentAuthResponse{
125125
SessionToken: agent.AuthToken.String(),
126126
})
127127
}

coderd/agentauth_test.go renamed to coderd/workspaceagentauth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/coder/coder/provisionersdk/proto"
2929
)
3030

31-
func TestPostAgentAuthenticateGoogleInstanceIdentity(t *testing.T) {
31+
func TestPostWorkspaceAgentAuthenticateGoogleInstanceIdentity(t *testing.T) {
3232
t.Parallel()
3333
t.Run("Expired", func(t *testing.T) {
3434
t.Parallel()
@@ -38,7 +38,7 @@ func TestPostAgentAuthenticateGoogleInstanceIdentity(t *testing.T) {
3838
client := coderdtest.New(t, &coderdtest.Options{
3939
GoogleTokenValidator: validator,
4040
})
41-
_, err := client.AuthenticateAgentWithGoogleCloudIdentity(context.Background(), "", createMetadataClient(signedKey))
41+
_, err := client.AuthWorkspaceAgentWithGoogleCloudIdentity(context.Background(), "", createMetadataClient(signedKey))
4242
var apiErr *codersdk.Error
4343
require.ErrorAs(t, err, &apiErr)
4444
require.Equal(t, http.StatusUnauthorized, apiErr.StatusCode())
@@ -52,7 +52,7 @@ func TestPostAgentAuthenticateGoogleInstanceIdentity(t *testing.T) {
5252
client := coderdtest.New(t, &coderdtest.Options{
5353
GoogleTokenValidator: validator,
5454
})
55-
_, err := client.AuthenticateAgentWithGoogleCloudIdentity(context.Background(), "", createMetadataClient(signedKey))
55+
_, err := client.AuthWorkspaceAgentWithGoogleCloudIdentity(context.Background(), "", createMetadataClient(signedKey))
5656
var apiErr *codersdk.Error
5757
require.ErrorAs(t, err, &apiErr)
5858
require.Equal(t, http.StatusNotFound, apiErr.StatusCode())
@@ -98,7 +98,7 @@ func TestPostAgentAuthenticateGoogleInstanceIdentity(t *testing.T) {
9898
require.NoError(t, err)
9999
coderdtest.AwaitWorkspaceProvisionJob(t, client, user.Organization, firstHistory.ProvisionJobID)
100100

101-
_, err = client.AuthenticateAgentWithGoogleCloudIdentity(context.Background(), "", createMetadataClient(signedKey))
101+
_, err = client.AuthWorkspaceAgentWithGoogleCloudIdentity(context.Background(), "", createMetadataClient(signedKey))
102102
require.NoError(t, err)
103103
})
104104
}

codersdk/projectimport.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// ProjectImportJob is not associated with a project by default. Projects
1717
// are created from import.
1818
func (c *Client) CreateProjectImportJob(ctx context.Context, organization string, req coderd.CreateProjectImportJobRequest) (coderd.ProvisionerJob, error) {
19-
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/projectimport/%s", organization), req)
19+
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/project/import/%s", organization), req)
2020
if err != nil {
2121
return coderd.ProvisionerJob{}, err
2222
}
@@ -30,7 +30,7 @@ func (c *Client) CreateProjectImportJob(ctx context.Context, organization string
3030

3131
// ProjectImportJob returns an import job by ID.
3232
func (c *Client) ProjectImportJob(ctx context.Context, organization string, job uuid.UUID) (coderd.ProvisionerJob, error) {
33-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s", organization, job), nil)
33+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/import/%s/%s", organization, job), nil)
3434
if err != nil {
3535
return coderd.ProvisionerJob{}, nil
3636
}
@@ -44,17 +44,17 @@ func (c *Client) ProjectImportJob(ctx context.Context, organization string, job
4444

4545
// ProjectImportJobLogsBefore returns logs that occurred before a specific time.
4646
func (c *Client) ProjectImportJobLogsBefore(ctx context.Context, organization string, job uuid.UUID, before time.Time) ([]coderd.ProvisionerJobLog, error) {
47-
return c.provisionerJobLogsBefore(ctx, "projectimport", organization, job, before)
47+
return c.provisionerJobLogsBefore(ctx, "project/import", organization, job, before)
4848
}
4949

5050
// ProjectImportJobLogsAfter streams logs for a project import operation that occurred after a specific time.
5151
func (c *Client) ProjectImportJobLogsAfter(ctx context.Context, organization string, job uuid.UUID, after time.Time) (<-chan coderd.ProvisionerJobLog, error) {
52-
return c.provisionerJobLogsAfter(ctx, "projectimport", organization, job, after)
52+
return c.provisionerJobLogsAfter(ctx, "project/import", organization, job, after)
5353
}
5454

5555
// ProjectImportJobSchemas returns schemas for an import job by ID.
5656
func (c *Client) ProjectImportJobSchemas(ctx context.Context, organization string, job uuid.UUID) ([]coderd.ParameterSchema, error) {
57-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s/schemas", organization, job), nil)
57+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/import/%s/%s/schemas", organization, job), nil)
5858
if err != nil {
5959
return nil, err
6060
}
@@ -68,7 +68,7 @@ func (c *Client) ProjectImportJobSchemas(ctx context.Context, organization strin
6868

6969
// ProjectImportJobParameters returns computed parameters for a project import job.
7070
func (c *Client) ProjectImportJobParameters(ctx context.Context, organization string, job uuid.UUID) ([]coderd.ComputedParameterValue, error) {
71-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s/parameters", organization, job), nil)
71+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/import/%s/%s/parameters", organization, job), nil)
7272
if err != nil {
7373
return nil, err
7474
}
@@ -82,7 +82,7 @@ func (c *Client) ProjectImportJobParameters(ctx context.Context, organization st
8282

8383
// ProjectImportJobResources returns resources for a project import job.
8484
func (c *Client) ProjectImportJobResources(ctx context.Context, organization string, job uuid.UUID) ([]coderd.ProvisionerJobResource, error) {
85-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/projectimport/%s/%s/resources", organization, job), nil)
85+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/project/import/%s/%s/resources", organization, job), nil)
8686
if err != nil {
8787
return nil, err
8888
}

codersdk/agent.go renamed to codersdk/workspaceagent.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
"github.com/coder/coder/coderd"
1313
)
1414

15-
// AuthenticateAgentWithGoogleCloudIdentity uses the Google Compute Engine Metadata API to
15+
// AuthWorkspaceAgentWithGoogleCloudIdentity uses the Google Compute Engine Metadata API to
1616
// fetch a signed JWT, and exchange it for a session token for a workspace agent.
1717
//
1818
// The requesting instance must be registered as a resource in the latest history for a workspace.
19-
func (c *Client) AuthenticateAgentWithGoogleCloudIdentity(ctx context.Context, serviceAccount string, gcpClient *metadata.Client) (coderd.AgentAuthResponse, error) {
19+
func (c *Client) AuthWorkspaceAgentWithGoogleCloudIdentity(ctx context.Context, serviceAccount string, gcpClient *metadata.Client) (coderd.WorkspaceAgentAuthResponse, error) {
2020
if serviceAccount == "" {
2121
// This is the default name specified by Google.
2222
serviceAccount = "default"
@@ -27,18 +27,18 @@ func (c *Client) AuthenticateAgentWithGoogleCloudIdentity(ctx context.Context, s
2727
// "format=full" is required, otherwise the responding payload will be missing "instance_id".
2828
jwt, err := gcpClient.Get(fmt.Sprintf("instance/service-accounts/%s/identity?audience=coder&format=full", serviceAccount))
2929
if err != nil {
30-
return coderd.AgentAuthResponse{}, xerrors.Errorf("get metadata identity: %w", err)
30+
return coderd.WorkspaceAgentAuthResponse{}, xerrors.Errorf("get metadata identity: %w", err)
3131
}
32-
res, err := c.request(ctx, http.MethodPost, "/api/v2/agent/authenticate/google-instance-identity", coderd.GoogleInstanceIdentityToken{
32+
res, err := c.request(ctx, http.MethodPost, "/api/v2/agent/auth/google-instance-identity", coderd.GoogleInstanceIdentityToken{
3333
JSONWebToken: jwt,
3434
})
3535
if err != nil {
36-
return coderd.AgentAuthResponse{}, err
36+
return coderd.WorkspaceAgentAuthResponse{}, err
3737
}
3838
defer res.Body.Close()
3939
if res.StatusCode != http.StatusOK {
40-
return coderd.AgentAuthResponse{}, readBodyAsError(res)
40+
return coderd.WorkspaceAgentAuthResponse{}, readBodyAsError(res)
4141
}
42-
var resp coderd.AgentAuthResponse
42+
var resp coderd.WorkspaceAgentAuthResponse
4343
return resp, json.NewDecoder(res.Body).Decode(&resp)
4444
}

codersdk/agent_test.go renamed to codersdk/workspaceagent_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import (
1313
"github.com/coder/coder/coderd/coderdtest"
1414
)
1515

16-
func TestAuthenticateAgentUsingGoogleCloudIdentity(t *testing.T) {
16+
func TestAuthWorkspaceAgentUsingGoogleCloudIdentity(t *testing.T) {
1717
t.Parallel()
1818
t.Run("Error", func(t *testing.T) {
1919
t.Parallel()
2020
client := coderdtest.New(t, nil)
21-
_, err := client.AuthenticateAgentWithGoogleCloudIdentity(context.Background(), "", metadata.NewClient(&http.Client{
21+
_, err := client.AuthWorkspaceAgentWithGoogleCloudIdentity(context.Background(), "", metadata.NewClient(&http.Client{
2222
Transport: roundTripper(func(req *http.Request) (*http.Response, error) {
2323
return &http.Response{
2424
StatusCode: http.StatusOK,

codersdk/workspaces.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (c *Client) ListWorkspaceHistory(ctx context.Context, owner, workspace stri
6868
if owner == "" {
6969
owner = "me"
7070
}
71-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/%s/version", owner, workspace), nil)
71+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/%s/history", owner, workspace), nil)
7272
if err != nil {
7373
return nil, err
7474
}
@@ -89,7 +89,7 @@ func (c *Client) WorkspaceHistory(ctx context.Context, owner, workspace, history
8989
if history == "" {
9090
history = "latest"
9191
}
92-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/%s/version/%s", owner, workspace, history), nil)
92+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/%s/history/%s", owner, workspace, history), nil)
9393
if err != nil {
9494
return coderd.WorkspaceHistory{}, err
9595
}
@@ -123,7 +123,7 @@ func (c *Client) CreateWorkspaceHistory(ctx context.Context, owner, workspace st
123123
if owner == "" {
124124
owner = "me"
125125
}
126-
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/workspaces/%s/%s/version", owner, workspace), request)
126+
res, err := c.request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/workspaces/%s/%s/history", owner, workspace), request)
127127
if err != nil {
128128
return coderd.WorkspaceHistory{}, err
129129
}
@@ -136,7 +136,7 @@ func (c *Client) CreateWorkspaceHistory(ctx context.Context, owner, workspace st
136136
}
137137

138138
func (c *Client) WorkspaceProvisionJob(ctx context.Context, organization string, job uuid.UUID) (coderd.ProvisionerJob, error) {
139-
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaceprovision/%s/%s", organization, job), nil)
139+
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspace/provision/%s/%s", organization, job), nil)
140140
if err != nil {
141141
return coderd.ProvisionerJob{}, nil
142142
}
@@ -150,10 +150,10 @@ func (c *Client) WorkspaceProvisionJob(ctx context.Context, organization string,
150150

151151
// WorkspaceProvisionJobLogsBefore returns logs that occurred before a specific time.
152152
func (c *Client) WorkspaceProvisionJobLogsBefore(ctx context.Context, organization string, job uuid.UUID, before time.Time) ([]coderd.ProvisionerJobLog, error) {
153-
return c.provisionerJobLogsBefore(ctx, "workspaceprovision", organization, job, before)
153+
return c.provisionerJobLogsBefore(ctx, "workspace/provision", organization, job, before)
154154
}
155155

156156
// WorkspaceProvisionJobLogsAfter streams logs for a workspace provision operation that occurred after a specific time.
157157
func (c *Client) WorkspaceProvisionJobLogsAfter(ctx context.Context, organization string, job uuid.UUID, after time.Time) (<-chan coderd.ProvisionerJobLog, error) {
158-
return c.provisionerJobLogsAfter(ctx, "workspaceprovision", organization, job, after)
158+
return c.provisionerJobLogsAfter(ctx, "workspace/provision", organization, job, after)
159159
}

0 commit comments

Comments
 (0)