Skip to content

feat: Move workspaces under organizations #1109

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 2 commits into from
Apr 25, 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
7 changes: 3 additions & 4 deletions cli/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
)
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestWorkspaceAgent(t *testing.T) {
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

cmd, _ := clitest.New(t, "agent", "--auth", "azure-instance-identity", "--url", client.URL.String())
Expand Down Expand Up @@ -98,7 +97,7 @@ func TestWorkspaceAgent(t *testing.T) {
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

cmd, _ := clitest.New(t, "agent", "--auth", "aws-instance-identity", "--url", client.URL.String())
Expand Down Expand Up @@ -152,7 +151,7 @@ func TestWorkspaceAgent(t *testing.T) {
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

cmd, _ := clitest.New(t, "agent", "--auth", "google-instance-identity", "--url", client.URL.String())
Expand Down
6 changes: 5 additions & 1 deletion cli/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ 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 @@ -55,7 +59,7 @@ func configSSH() *cobra.Command {
sshConfigContent = sshConfigContent[:startIndex-1] + sshConfigContent[endIndex+len(sshEndToken):]
}

workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me)
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cli/configssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestConfigSSH(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
agentClient := codersdk.New(client.URL)
agentClient.SessionToken = authToken
Expand Down
2 changes: 1 addition & 1 deletion cli/gitssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestGitSSH(t *testing.T) {
})
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

// start workspace agent
Expand Down
2 changes: 1 addition & 1 deletion cli/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func parseScopeAndID(ctx context.Context, client *codersdk.Client, organization
}
scopeID = user.ID
case codersdk.ParameterWorkspace:
workspace, err := client.WorkspaceByName(ctx, codersdk.Me, name)
workspace, err := client.WorkspaceByOwnerAndName(ctx, organization.ID, codersdk.Me, name)
if err != nil {
return scope, uuid.Nil, err
}
Expand Down
6 changes: 5 additions & 1 deletion cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ func server() *cobra.Command {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), "\n\n"+cliui.Styles.Bold.Render("Interrupt caught. Gracefully exiting..."))

if dev {
workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me)
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)
if err != nil {
return xerrors.Errorf("get workspaces: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestServer(t *testing.T) {
version := coderdtest.CreateTemplateVersion(t, client, orgs[0].ID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, orgs[0].ID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, orgs[0].ID, template.ID)
coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)

require.NoError(t, err)
Expand Down
6 changes: 5 additions & 1 deletion cli/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,13 @@ func ssh() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspaceParts := strings.Split(args[0], ".")
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceParts[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceParts[0])
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cli/ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestSSH(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
cmd, root := clitest.New(t, "ssh", workspace.Name)
clitest.SetupConfig(t, client, root)
doneChan := make(chan struct{})
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestSSH(t *testing.T) {
})
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, codersdk.Me, template.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
go func() {
// Run this async so the SSH command has to wait for
// the build and agent to connect!
Expand Down
12 changes: 10 additions & 2 deletions cli/workspaceautostart.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ func workspaceAutostartEnable() *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.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down Expand Up @@ -92,8 +96,12 @@ func workspaceAutostartDisable() *cobra.Command {
if err != nil {
return err
}
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}

workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cli/workspaceautostart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
)

func TestWorkspaceAutostart(t *testing.T) {
Expand All @@ -28,7 +27,7 @@ func TestWorkspaceAutostart(t *testing.T) {
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
tz = "Europe/Dublin"
cmdArgs = []string{"workspaces", "autostart", "enable", workspace.Name, "--minute", "30", "--hour", "9", "--days", "1-5", "--tz", tz}
sched = "CRON_TZ=Europe/Dublin 30 9 * * 1-5"
Expand Down Expand Up @@ -110,7 +109,7 @@ func TestWorkspaceAutostart(t *testing.T) {
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
)

// check current TZ env var
Expand Down
12 changes: 10 additions & 2 deletions cli/workspaceautostop.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ func workspaceAutostopEnable() *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", autostopTimezone, autostopMinute, autostopHour, autostopDayOfWeek)
validSchedule, err := schedule.Weekly(spec)
if err != nil {
return err
}

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

workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions cli/workspaceautostop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/codersdk"
)

func TestWorkspaceAutostop(t *testing.T) {
Expand All @@ -28,7 +27,7 @@ func TestWorkspaceAutostop(t *testing.T) {
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
cmdArgs = []string{"workspaces", "autostop", "enable", workspace.Name, "--minute", "30", "--hour", "17", "--days", "1-5", "--tz", "Europe/Dublin"}
sched = "CRON_TZ=Europe/Dublin 30 17 * * 1-5"
stdoutBuf = &bytes.Buffer{}
Expand Down Expand Up @@ -109,7 +108,7 @@ func TestWorkspaceAutostop(t *testing.T) {
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
)

// check current TZ env var
Expand Down
6 changes: 3 additions & 3 deletions cli/workspacecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func workspaceCreate() *cobra.Command {
workspaceName, err = cliui.Prompt(cmd, cliui.PromptOptions{
Text: "Specify a name for your workspace:",
Validate: func(workspaceName string) error {
_, err = client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceName)
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
if err == nil {
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
}
Expand All @@ -53,7 +53,7 @@ func workspaceCreate() *cobra.Command {
}
}

_, err = client.WorkspaceByName(cmd.Context(), codersdk.Me, workspaceName)
_, err = client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, workspaceName)
if err == nil {
return xerrors.Errorf("A workspace already exists named %q!", workspaceName)
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func workspaceCreate() *cobra.Command {
}

before := time.Now()
workspace, err := client.CreateWorkspace(cmd.Context(), codersdk.Me, codersdk.CreateWorkspaceRequest{
workspace, err := client.CreateWorkspace(cmd.Context(), organization.ID, codersdk.CreateWorkspaceRequest{
TemplateID: template.ID,
Name: workspaceName,
ParameterValues: parameters,
Expand Down
6 changes: 5 additions & 1 deletion cli/workspacedelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ func workspaceDelete() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cli/workspacelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func workspaceList() *cobra.Command {
if err != nil {
return err
}
workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me)
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cli/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ func validArgsWorkspaceName(cmd *cobra.Command, _ []string, toComplete string) (
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
workspaces, err := client.WorkspacesByUser(cmd.Context(), codersdk.Me)
organization, err := currentOrganization(cmd, client)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
workspaces, err := client.WorkspacesByOwner(cmd.Context(), organization.ID, codersdk.Me)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
6 changes: 5 additions & 1 deletion cli/workspaceshow.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ func workspaceShow() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return xerrors.Errorf("get workspace: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion cli/workspacestart.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func workspaceStart() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cli/workspacestop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ func workspaceStop() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
6 changes: 5 additions & 1 deletion cli/workspaceupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ func workspaceUpdate() *cobra.Command {
if err != nil {
return err
}
workspace, err := client.WorkspaceByName(cmd.Context(), codersdk.Me, args[0])
organization, err := currentOrganization(cmd, client)
if err != nil {
return err
}
workspace, err := client.WorkspaceByOwnerAndName(cmd.Context(), organization.ID, codersdk.Me, args[0])
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/templater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func parse(cmd *cobra.Command, parameters []codersdk.CreateParameterRequest) err
return err
}

workspace, err := client.CreateWorkspace(cmd.Context(), created.UserID, codersdk.CreateWorkspaceRequest{
workspace, err := client.CreateWorkspace(cmd.Context(), created.OrganizationID, codersdk.CreateWorkspaceRequest{
TemplateID: template.ID,
Name: "example",
})
Expand Down
Loading