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 all 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, args[0])
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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ func delete() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, args[0])
if err != nil {
return err
}
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
5 changes: 2 additions & 3 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"golang.org/x/xerrors"

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

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

return client.WorkspaceByOwnerAndName(cmd.Context(), orgID, owner, name)
return client.WorkspaceByOwnerAndName(cmd.Context(), owner, name)
}

// createConfig consumes the global configuration flag to produce a config root.
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 @@ -18,11 +18,7 @@ func show() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 = namedWorkspace(cmd, client, orgID, workspaceParts[0])
workspace, err = namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, 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 := namedWorkspace(cmd, client, organization.ID, args[0])
workspace, err := namedWorkspace(cmd, client, args[0])
if err != nil {
return err
}
Expand Down
11 changes: 2 additions & 9 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,7 @@ func New(options *Options) *API {
r.Get("/", api.templatesByOrganization)
r.Get("/{templatename}", api.templateByOrganizationAndName)
})
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)
r.Get("/", api.workspacesByOwner)
})
})
r.Post("/workspaces", api.postWorkspacesByOrganization)
r.Route("/members", func(r chi.Router) {
r.Get("/roles", api.assignableOrgRoles)
r.Route("/{user}", func(r chi.Router) {
Expand Down Expand Up @@ -259,6 +251,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