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

Skip to content

Commit 43f622a

Browse files
authored
fix: Remove unused workspace routes in favor of list with filter (#2038)
* fix: Remove unused workspace routes in favor of list with filter This consolidates the workspace routes into a single place. It allows users to fetch a workspace by their username and workspace name, which will be used by the frontend for routing. * Fix RBAC * Fix CLI usages
1 parent d8c4401 commit 43f622a

26 files changed

+72
-424
lines changed

cli/autostart.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,8 @@ func autostartShow() *cobra.Command {
4141
if err != nil {
4242
return err
4343
}
44-
organization, err := currentOrganization(cmd, client)
45-
if err != nil {
46-
return err
47-
}
4844

49-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
45+
workspace, err := namedWorkspace(cmd, client, args[0])
5046
if err != nil {
5147
return err
5248
}
@@ -93,18 +89,14 @@ func autostartEnable() *cobra.Command {
9389
if err != nil {
9490
return err
9591
}
96-
organization, err := currentOrganization(cmd, client)
97-
if err != nil {
98-
return err
99-
}
10092

10193
spec := fmt.Sprintf("CRON_TZ=%s %s %s * * %s", autostartTimezone, autostartMinute, autostartHour, autostartDayOfWeek)
10294
validSchedule, err := schedule.Weekly(spec)
10395
if err != nil {
10496
return err
10597
}
10698

107-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
99+
workspace, err := namedWorkspace(cmd, client, args[0])
108100
if err != nil {
109101
return err
110102
}
@@ -142,12 +134,8 @@ func autostartDisable() *cobra.Command {
142134
if err != nil {
143135
return err
144136
}
145-
organization, err := currentOrganization(cmd, client)
146-
if err != nil {
147-
return err
148-
}
149137

150-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
138+
workspace, err := namedWorkspace(cmd, client, args[0])
151139
if err != nil {
152140
return err
153141
}

cli/bump.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,7 @@ func bump() *cobra.Command {
4343
if err != nil {
4444
return xerrors.Errorf("create client: %w", err)
4545
}
46-
organization, err := currentOrganization(cmd, client)
47-
if err != nil {
48-
return xerrors.Errorf("get current org: %w", err)
49-
}
50-
51-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
46+
workspace, err := namedWorkspace(cmd, client, args[0])
5247
if err != nil {
5348
return xerrors.Errorf("get workspace: %w", err)
5449
}

cli/configssh.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ func configSSH() *cobra.Command {
4848
if err != nil {
4949
return err
5050
}
51-
organization, err := currentOrganization(cmd, client)
52-
if err != nil {
53-
return err
54-
}
5551
if strings.HasPrefix(sshConfigFile, "~/") {
5652
dirname, _ := os.UserHomeDir()
5753
sshConfigFile = filepath.Join(dirname, sshConfigFile[2:])
@@ -65,7 +61,9 @@ func configSSH() *cobra.Command {
6561
sshConfigContent = sshConfigContent[:startIndex-1] + sshConfigContent[endIndex+len(sshEndToken):]
6662
}
6763

68-
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
64+
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
65+
Owner: codersdk.Me,
66+
})
6967
if err != nil {
7068
return err
7169
}

cli/create.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func create() *cobra.Command {
4949
workspaceName, err = cliui.Prompt(cmd, cliui.PromptOptions{
5050
Text: "Specify a name for your workspace:",
5151
Validate: func(workspaceName string) error {
52-
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
52+
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
5353
if err == nil {
5454
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
5555
}
@@ -75,7 +75,7 @@ func create() *cobra.Command {
7575
return xerrors.Errorf("TTL must be at least 1 minute")
7676
}
7777

78-
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
78+
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
7979
if err == nil {
8080
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
8181
}

cli/delete.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ func delete() *cobra.Command {
3030
if err != nil {
3131
return err
3232
}
33-
organization, err := currentOrganization(cmd, client)
34-
if err != nil {
35-
return err
36-
}
37-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
33+
workspace, err := namedWorkspace(cmd, client, args[0])
3834
if err != nil {
3935
return err
4036
}

cli/portforward.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@ func portForward() *cobra.Command {
7070
if err != nil {
7171
return err
7272
}
73-
organization, err := currentOrganization(cmd, client)
74-
if err != nil {
75-
return err
76-
}
7773

78-
workspace, agent, err := getWorkspaceAndAgent(cmd, client, organization.ID, codersdk.Me, args[0], false)
74+
workspace, agent, err := getWorkspaceAndAgent(cmd, client, codersdk.Me, args[0], false)
7975
if err != nil {
8076
return err
8177
}

cli/root.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"golang.org/x/xerrors"
1111

12-
"github.com/google/uuid"
1312
"github.com/kirsle/configdir"
1413
"github.com/mattn/go-isatty"
1514
"github.com/spf13/cobra"
@@ -180,7 +179,7 @@ func currentOrganization(cmd *cobra.Command, client *codersdk.Client) (codersdk.
180179
// namedWorkspace fetches and returns a workspace by an identifier, which may be either
181180
// a bare name (for a workspace owned by the current user) or a "user/workspace" combination,
182181
// where user is either a username or UUID.
183-
func namedWorkspace(cmd *cobra.Command, client *codersdk.Client, orgID uuid.UUID, identifier string) (codersdk.Workspace, error) {
182+
func namedWorkspace(cmd *cobra.Command, client *codersdk.Client, identifier string) (codersdk.Workspace, error) {
184183
parts := strings.Split(identifier, "/")
185184

186185
var owner, name string
@@ -195,7 +194,7 @@ func namedWorkspace(cmd *cobra.Command, client *codersdk.Client, orgID uuid.UUID
195194
return codersdk.Workspace{}, xerrors.Errorf("invalid workspace name: %q", identifier)
196195
}
197196

198-
return client.WorkspaceByOwnerAndName(cmd.Context(), orgID, owner, name)
197+
return client.WorkspaceByOwnerAndName(cmd.Context(), owner, name)
199198
}
200199

201200
// createConfig consumes the global configuration flag to produce a config root.

cli/server.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,9 @@ func server() *cobra.Command {
412412
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit"))
413413

414414
if dev {
415-
organizations, err := client.OrganizationsByUser(cmd.Context(), codersdk.Me)
416-
if err != nil {
417-
return xerrors.Errorf("get organizations: %w", err)
418-
}
419-
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organizations[0].ID, codersdk.Me)
415+
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
416+
Owner: codersdk.Me,
417+
})
420418
if err != nil {
421419
return xerrors.Errorf("get workspaces: %w", err)
422420
}

cli/show.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ func show() *cobra.Command {
1818
if err != nil {
1919
return err
2020
}
21-
organization, err := currentOrganization(cmd, client)
22-
if err != nil {
23-
return err
24-
}
25-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
21+
workspace, err := namedWorkspace(cmd, client, args[0])
2622
if err != nil {
2723
return xerrors.Errorf("get workspace: %w", err)
2824
}

cli/ssh.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ func ssh() *cobra.Command {
4848
if err != nil {
4949
return err
5050
}
51-
organization, err := currentOrganization(cmd, client)
52-
if err != nil {
53-
return err
54-
}
5551

5652
if shuffle {
5753
err := cobra.ExactArgs(0)(cmd, args)
@@ -65,7 +61,7 @@ func ssh() *cobra.Command {
6561
}
6662
}
6763

68-
workspace, agent, err := getWorkspaceAndAgent(cmd, client, organization.ID, codersdk.Me, args[0], shuffle)
64+
workspace, agent, err := getWorkspaceAndAgent(cmd, client, codersdk.Me, args[0], shuffle)
6965
if err != nil {
7066
return err
7167
}
@@ -185,7 +181,7 @@ func ssh() *cobra.Command {
185181
// getWorkspaceAgent returns the workspace and agent selected using either the
186182
// `<workspace>[.<agent>]` syntax via `in` or picks a random workspace and agent
187183
// if `shuffle` is true.
188-
func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, orgID uuid.UUID, userID string, in string, shuffle bool) (codersdk.Workspace, codersdk.WorkspaceAgent, error) { //nolint:revive
184+
func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, userID string, in string, shuffle bool) (codersdk.Workspace, codersdk.WorkspaceAgent, error) { //nolint:revive
189185
ctx := cmd.Context()
190186

191187
var (
@@ -194,7 +190,9 @@ func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, orgID uui
194190
err error
195191
)
196192
if shuffle {
197-
workspaces, err := client.WorkspacesByOwner(cmd.Context(), orgID, userID)
193+
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
194+
Owner: codersdk.Me,
195+
})
198196
if err != nil {
199197
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
200198
}
@@ -207,7 +205,7 @@ func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, orgID uui
207205
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
208206
}
209207
} else {
210-
workspace, err = namedWorkspace(cmd, client, orgID, workspaceParts[0])
208+
workspace, err = namedWorkspace(cmd, client, workspaceParts[0])
211209
if err != nil {
212210
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
213211
}

cli/start.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ func start() *cobra.Command {
2828
if err != nil {
2929
return err
3030
}
31-
organization, err := currentOrganization(cmd, client)
32-
if err != nil {
33-
return err
34-
}
35-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
31+
workspace, err := namedWorkspace(cmd, client, args[0])
3632
if err != nil {
3733
return err
3834
}

cli/state.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ func statePull() *cobra.Command {
3030
if err != nil {
3131
return err
3232
}
33-
organization, err := currentOrganization(cmd, client)
34-
if err != nil {
35-
return err
36-
}
37-
38-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
33+
workspace, err := namedWorkspace(cmd, client, args[0])
3934
if err != nil {
4035
return err
4136
}
@@ -76,12 +71,7 @@ func statePush() *cobra.Command {
7671
if err != nil {
7772
return err
7873
}
79-
organization, err := currentOrganization(cmd, client)
80-
if err != nil {
81-
return err
82-
}
83-
84-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
74+
workspace, err := namedWorkspace(cmd, client, args[0])
8575
if err != nil {
8676
return err
8777
}

cli/stop.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ func stop() *cobra.Command {
2828
if err != nil {
2929
return err
3030
}
31-
organization, err := currentOrganization(cmd, client)
32-
if err != nil {
33-
return err
34-
}
35-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
31+
workspace, err := namedWorkspace(cmd, client, args[0])
3632
if err != nil {
3733
return err
3834
}

cli/ttl.go

+3-15
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ func ttlShow() *cobra.Command {
3939
if err != nil {
4040
return xerrors.Errorf("create client: %w", err)
4141
}
42-
organization, err := currentOrganization(cmd, client)
43-
if err != nil {
44-
return xerrors.Errorf("get current org: %w", err)
45-
}
4642

47-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
43+
workspace, err := namedWorkspace(cmd, client, args[0])
4844
if err != nil {
4945
return xerrors.Errorf("get workspace: %w", err)
5046
}
@@ -72,12 +68,8 @@ func ttlset() *cobra.Command {
7268
if err != nil {
7369
return xerrors.Errorf("create client: %w", err)
7470
}
75-
organization, err := currentOrganization(cmd, client)
76-
if err != nil {
77-
return xerrors.Errorf("get current org: %w", err)
78-
}
7971

80-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
72+
workspace, err := namedWorkspace(cmd, client, args[0])
8173
if err != nil {
8274
return xerrors.Errorf("get workspace: %w", err)
8375
}
@@ -120,12 +112,8 @@ func ttlunset() *cobra.Command {
120112
if err != nil {
121113
return xerrors.Errorf("create client: %w", err)
122114
}
123-
organization, err := currentOrganization(cmd, client)
124-
if err != nil {
125-
return xerrors.Errorf("get current org: %w", err)
126-
}
127115

128-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
116+
workspace, err := namedWorkspace(cmd, client, args[0])
129117
if err != nil {
130118
return xerrors.Errorf("get workspace: %w", err)
131119
}

cli/update.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ func update() *cobra.Command {
1919
if err != nil {
2020
return err
2121
}
22-
organization, err := currentOrganization(cmd, client)
23-
if err != nil {
24-
return err
25-
}
26-
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
22+
workspace, err := namedWorkspace(cmd, client, args[0])
2723
if err != nil {
2824
return err
2925
}

coderd/coderd.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,7 @@ func New(options *Options) *API {
152152
r.Get("/", api.templatesByOrganization)
153153
r.Get("/{templatename}", api.templateByOrganizationAndName)
154154
})
155-
r.Route("/workspaces", func(r chi.Router) {
156-
r.Post("/", api.postWorkspacesByOrganization)
157-
r.Get("/", api.workspacesByOrganization)
158-
r.Route("/{user}", func(r chi.Router) {
159-
r.Use(httpmw.ExtractUserParam(options.Database))
160-
r.Get("/{workspacename}", api.workspaceByOwnerAndName)
161-
r.Get("/", api.workspacesByOwner)
162-
})
163-
})
155+
r.Post("/workspaces", api.postWorkspacesByOrganization)
164156
r.Route("/members", func(r chi.Router) {
165157
r.Get("/roles", api.assignableOrgRoles)
166158
r.Route("/{user}", func(r chi.Router) {
@@ -259,6 +251,7 @@ func New(options *Options) *API {
259251
r.Get("/", api.organizationsByUser)
260252
r.Get("/{organizationname}", api.organizationByUserAndName)
261253
})
254+
r.Get("/workspace/{workspacename}", api.workspaceByOwnerAndName)
262255
r.Get("/gitsshkey", api.gitSSHKey)
263256
r.Put("/gitsshkey", api.regenerateGitSSHKey)
264257
})

coderd/coderd_test.go

+5-11
Original file line numberDiff line numberDiff line change
@@ -147,22 +147,16 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
147147
"GET:/api/v2/workspaceagents/{workspaceagent}/turn": {NoAuthorize: true},
148148

149149
// These endpoints have more assertions. This is good, add more endpoints to assert if you can!
150-
"GET:/api/v2/organizations/{organization}": {AssertObject: rbac.ResourceOrganization.InOrg(admin.OrganizationID)},
151-
"GET:/api/v2/users/{user}/organizations": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceOrganization},
152-
"GET:/api/v2/users/{user}/workspaces": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceWorkspace},
153-
"GET:/api/v2/organizations/{organization}/workspaces/{user}": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceWorkspace},
154-
"GET:/api/v2/organizations/{organization}/workspaces/{user}/{workspace}": {
155-
AssertObject: rbac.ResourceWorkspace.InOrg(organization.ID).WithID(workspace.ID.String()).WithOwner(workspace.OwnerID.String()),
156-
},
157-
"GET:/api/v2/workspaces/{workspace}/builds/{workspacebuildname}": {
150+
"GET:/api/v2/organizations/{organization}": {AssertObject: rbac.ResourceOrganization.InOrg(admin.OrganizationID)},
151+
"GET:/api/v2/users/{user}/organizations": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceOrganization},
152+
"GET:/api/v2/users/{user}/workspace/{workspacename}": {
153+
AssertObject: rbac.ResourceWorkspace,
158154
AssertAction: rbac.ActionRead,
159-
AssertObject: workspaceRBACObj,
160155
},
161-
"GET:/api/v2/organizations/{organization}/workspaces/{user}/{workspacename}": {
156+
"GET:/api/v2/workspaces/{workspace}/builds/{workspacebuildname}": {
162157
AssertAction: rbac.ActionRead,
163158
AssertObject: workspaceRBACObj,
164159
},
165-
"GET:/api/v2/organizations/{organization}/workspaces": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceWorkspace},
166160
"GET:/api/v2/workspacebuilds/{workspacebuild}": {
167161
AssertAction: rbac.ActionRead,
168162
AssertObject: workspaceRBACObj,

0 commit comments

Comments
 (0)