Skip to content

fix: Remove unused workspace routes in favor of list with filter #2038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions cli/autostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,8 @@ func autostartShow() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might conflict with #2036!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, but in a great way. We'll no longer need organization IDs to query workspaces since they are namespaced!

if err != nil {
return err
}
Expand Down Expand Up @@ -93,18 +89,14 @@ func autostartEnable() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

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

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -142,12 +134,8 @@ func autostartDisable() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
7 changes: 1 addition & 6 deletions cli/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ func bump() *cobra.Command {
if err != nil {
return xerrors.Errorf("create client: %w", err)
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
8 changes: 3 additions & 5 deletions cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ func configSSH() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
if strings.HasPrefix(sshConfigFile, "~/") {
dirname, _ := os.UserHomeDir()
sshConfigFile = filepath.Join(dirname, sshConfigFile[2:])
Expand All @@ -65,7 +61,9 @@ func configSSH() *cobra.Command {
sshConfigContent = sshConfigContent[:startIndex-1] + sshConfigContent[endIndex+len(sshEndToken):]
}

workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
Owner: codersdk.Me,
})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func create() *cobra.Command {
workspaceName, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Specify a name for your workspace:",
Validate: func(workspaceName string) error {
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
if err == nil {
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
}
Expand All @@ -75,7 +75,7 @@ func create() *cobra.Command {
return xerrors.Errorf("TTL must be at least 1 minute")
}

_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, workspaceName)
if err == nil {
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
}
Expand Down
6 changes: 1 addition & 5 deletions cli/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,8 @@ func portForward() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, agent, err := getWorkspaceAndAgent(cmd, client, organization.ID, codersdk.Me, args[0], false)
workspace, agent, err := getWorkspaceAndAgent(cmd, client, codersdk.Me, args[0], false)
if err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,9 @@ func server() *cobra.Command {
"Interrupt caught, gracefully exiting. Use ctrl+\\ to force quit"))

if dev {
organizations, err := client.OrganizationsByUser(cmd.Context(), codersdk.Me)
if err != nil {
return xerrors.Errorf("get organizations: %w", err)
}
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organizations[0].ID, codersdk.Me)
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
Owner: codersdk.Me,
})
if err != nil {
return xerrors.Errorf("get workspaces: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions cli/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ func show() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
14 changes: 6 additions & 8 deletions cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ func ssh() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

if shuffle {
err := cobra.ExactArgs(0)(cmd, args)
Expand All @@ -65,7 +61,7 @@ func ssh() *cobra.Command {
}
}

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

var (
Expand All @@ -194,7 +190,9 @@ func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, orgID uui
err error
)
if shuffle {
workspaces, err := client.WorkspacesByOwner(cmd.Context(), orgID, userID)
workspaces, err := client.Workspaces(cmd.Context(), codersdk.WorkspaceFilter{
Owner: codersdk.Me,
})
if err != nil {
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
}
Expand All @@ -207,7 +205,7 @@ func getWorkspaceAndAgent(cmd *cobra.Command, client *codersdk.Client, orgID uui
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
}
} else {
workspace, err = client.WorkspaceByOwnerAndName(cmd.Context(), orgID, userID, workspaceParts[0])
workspace, err = client.WorkspaceByOwnerAndName(cmd.Context(), userID, workspaceParts[0])
if err != nil {
return codersdk.Workspace{}, codersdk.WorkspaceAgent{}, err
}
Expand Down
6 changes: 1 addition & 5 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ func start() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
14 changes: 2 additions & 12 deletions cli/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ func statePull() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -76,12 +71,7 @@ func statePush() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
6 changes: 1 addition & 5 deletions cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ func stop() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
18 changes: 3 additions & 15 deletions cli/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,8 @@ func ttlShow() *cobra.Command {
if err != nil {
return xerrors.Errorf("create client: %w", err)
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down Expand Up @@ -72,12 +68,8 @@ func ttlset() *cobra.Command {
if err != nil {
return xerrors.Errorf("create client: %w", err)
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down Expand Up @@ -120,12 +112,8 @@ func ttlunset() *cobra.Command {
if err != nil {
return xerrors.Errorf("create client: %w", err)
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return xerrors.Errorf("get current org: %w", err)
}

workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
6 changes: 1 addition & 5 deletions cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ func update() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ func New(options *Options) *API {
})
r.Route("/workspaces", func(r chi.Router) {
r.Post("/", api.postWorkspacesByOrganization)
r.Get("/", api.workspacesByOrganization)
r.Route("/{user}", func(r chi.Router) {
r.Use(httpmw.ExtractUserParam(options.Database))
r.Get("/{workspacename}", api.workspaceByOwnerAndName)
Expand Down Expand Up @@ -259,6 +258,7 @@ func New(options *Options) *API {
r.Get("/", api.organizationsByUser)
r.Get("/{organizationname}", api.organizationByUserAndName)
})
r.Get("/workspace/{workspacename}", api.workspaceByOwnerAndName)
r.Get("/gitsshkey", api.gitSSHKey)
r.Put("/gitsshkey", api.regenerateGitSSHKey)
})
Expand Down
16 changes: 5 additions & 11 deletions coderd/coderd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,16 @@ func TestAuthorizeAllEndpoints(t *testing.T) {
"GET:/api/v2/workspaceagents/{workspaceagent}/turn": {NoAuthorize: true},

// These endpoints have more assertions. This is good, add more endpoints to assert if you can!
"GET:/api/v2/organizations/{organization}": {AssertObject: rbac.ResourceOrganization.InOrg(admin.OrganizationID)},
"GET:/api/v2/users/{user}/organizations": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceOrganization},
"GET:/api/v2/users/{user}/workspaces": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceWorkspace},
"GET:/api/v2/organizations/{organization}/workspaces/{user}": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceWorkspace},
"GET:/api/v2/organizations/{organization}/workspaces/{user}/{workspace}": {
AssertObject: rbac.ResourceWorkspace.InOrg(organization.ID).WithID(workspace.ID.String()).WithOwner(workspace.OwnerID.String()),
},
"GET:/api/v2/workspaces/{workspace}/builds/{workspacebuildname}": {
"GET:/api/v2/organizations/{organization}": {AssertObject: rbac.ResourceOrganization.InOrg(admin.OrganizationID)},
"GET:/api/v2/users/{user}/organizations": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceOrganization},
"GET:/api/v2/users/{user}/workspace/{workspacename}": {
AssertObject: rbac.ResourceWorkspace,
AssertAction: rbac.ActionRead,
AssertObject: workspaceRBACObj,
},
"GET:/api/v2/organizations/{organization}/workspaces/{user}/{workspacename}": {
"GET:/api/v2/workspaces/{workspace}/builds/{workspacebuildname}": {
AssertAction: rbac.ActionRead,
AssertObject: workspaceRBACObj,
},
"GET:/api/v2/organizations/{organization}/workspaces": {StatusCode: http.StatusOK, AssertObject: rbac.ResourceWorkspace},
"GET:/api/v2/workspacebuilds/{workspacebuild}": {
AssertAction: rbac.ActionRead,
AssertObject: workspaceRBACObj,
Expand Down
Loading