Skip to content

Commit 6776c03

Browse files
committed
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 7a6665f commit 6776c03

26 files changed

+72
-424
lines changed

cli/autostart.go

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

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

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

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

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

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

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

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

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

Lines changed: 6 additions & 8 deletions
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
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy