From 918ce665b02e20787edb2078d937feca5e10a580 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 3 Sep 2021 09:46:06 -0500 Subject: [PATCH 1/7] Allow making a workspace for another user as an admin --- coder-sdk/workspace.go | 4 ++++ internal/cmd/workspaces.go | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/coder-sdk/workspace.go b/coder-sdk/workspace.go index cb726307..e30c5c58 100644 --- a/coder-sdk/workspace.go +++ b/coder-sdk/workspace.go @@ -89,6 +89,10 @@ type CreateWorkspaceRequest struct { Namespace string `json:"namespace"` EnableAutoStart bool `json:"autostart_enabled"` + // ForUserID is an optional param to create a workspace for another user + // other than the requester. This only works for admins and site managers. + ForUserID string `json:"for_user_id,omitempty"` + // TemplateID comes from the parse template route on cemanager. TemplateID string `json:"template_id,omitempty"` } diff --git a/internal/cmd/workspaces.go b/internal/cmd/workspaces.go index f4d744a0..d1338063 100644 --- a/internal/cmd/workspaces.go +++ b/internal/cmd/workspaces.go @@ -397,6 +397,7 @@ func createWorkspaceCmd() *cobra.Command { useCVM bool providerName string enableAutostart bool + forUser string // Optional ) cmd := &cobra.Command{ @@ -448,6 +449,19 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 } } + if forUser != "" { + // Making a workspace for another user, do they exist? + u, err := client.UserByEmail(ctx, forUser) + if err != nil { + // Try by ID? + u, err = client.UserByID(ctx, forUser) + if err != nil { + return xerrors.Errorf("the user %q was not found: %w", forUser, err) + } + } + forUser = u.ID + } + // ExactArgs(1) ensures our name value can't panic on an out of bounds. createReq := &coder.CreateWorkspaceRequest{ Name: args[0], @@ -462,6 +476,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 ResourcePoolID: provider.ID, Namespace: provider.DefaultNamespace, EnableAutoStart: enableAutostart, + ForUserID: forUser, } // if any of these defaulted to their zero value we provision @@ -507,6 +522,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild") cmd.Flags().BoolVar(&useCVM, "container-based-vm", false, "deploy the workspace as a Container-based VM") cmd.Flags().BoolVar(&enableAutostart, "enable-autostart", false, "automatically start this workspace at your preferred time.") + cmd.Flags().StringVar(&forUser, "for", "", "Optionally create a workspace for another user. This field should be the user's email.") _ = cmd.MarkFlagRequired("image") return cmd } From 2f51c63c1befd040f6cb5c9c711953888de25640 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 3 Sep 2021 09:52:53 -0500 Subject: [PATCH 2/7] Use standard flag --- internal/cmd/workspaces.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/cmd/workspaces.go b/internal/cmd/workspaces.go index d1338063..89c364bb 100644 --- a/internal/cmd/workspaces.go +++ b/internal/cmd/workspaces.go @@ -449,7 +449,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 } } - if forUser != "" { + if forUser != "" && forUser != coder.Me { // Making a workspace for another user, do they exist? u, err := client.UserByEmail(ctx, forUser) if err != nil { @@ -522,7 +522,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild") cmd.Flags().BoolVar(&useCVM, "container-based-vm", false, "deploy the workspace as a Container-based VM") cmd.Flags().BoolVar(&enableAutostart, "enable-autostart", false, "automatically start this workspace at your preferred time.") - cmd.Flags().StringVar(&forUser, "for", "", "Optionally create a workspace for another user. This field should be the user's email.") + cmd.Flags().StringVar(&forUser, "user", coder.Me, "Specify the user whose resources to target") _ = cmd.MarkFlagRequired("image") return cmd } From 2fdb9e89e1a3c7ae0b8e03889e809858de6c6047 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 3 Sep 2021 15:13:46 +0000 Subject: [PATCH 3/7] Make gendocs --- docs/coder_workspaces_create.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/coder_workspaces_create.md b/docs/coder_workspaces_create.md index 56732f89..c1bd266d 100644 --- a/docs/coder_workspaces_create.md +++ b/docs/coder_workspaces_create.md @@ -33,6 +33,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 -o, --org string name of the organization the workspace should be created under. --provider string name of Workspace Provider with which to create the workspace -t, --tag string tag of the image the workspace will be based off of. (default "latest") + --user string Specify the user whose resources to target (default "me") ``` ### Options inherited from parent commands From 08e2e817a855cff773dd286597a858aff5247e90 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 3 Sep 2021 10:27:09 -0500 Subject: [PATCH 4/7] Adjust the output to watch the build logs --- internal/cmd/workspaces.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/cmd/workspaces.go b/internal/cmd/workspaces.go index 89c364bb..b27abe04 100644 --- a/internal/cmd/workspaces.go +++ b/internal/cmd/workspaces.go @@ -449,6 +449,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 } } + var forEmail string if forUser != "" && forUser != coder.Me { // Making a workspace for another user, do they exist? u, err := client.UserByEmail(ctx, forUser) @@ -460,6 +461,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 } } forUser = u.ID + forEmail = u.Email } // ExactArgs(1) ensures our name value can't panic on an out of bounds. @@ -504,9 +506,13 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 return nil } + extraFlags := "" + if forEmail != coder.Me && forEmail != "" { + extraFlags = " --user " + forEmail + } clog.LogSuccess("creating workspace...", clog.BlankLine, - clog.Tipf(`run "coder workspaces watch-build %s" to trail the build logs`, workspace.Name), + clog.Tipf(`run "coder workspaces watch-build %s%s" to trail the build logs`, workspace.Name, extraFlags), ) return nil }, From f26d978964564f61a40da960e408fbd6b118e4f1 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 3 Sep 2021 15:31:25 -0500 Subject: [PATCH 5/7] Fix text for user flag --- internal/cmd/workspaces.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/workspaces.go b/internal/cmd/workspaces.go index b27abe04..950bfd60 100644 --- a/internal/cmd/workspaces.go +++ b/internal/cmd/workspaces.go @@ -528,7 +528,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild") cmd.Flags().BoolVar(&useCVM, "container-based-vm", false, "deploy the workspace as a Container-based VM") cmd.Flags().BoolVar(&enableAutostart, "enable-autostart", false, "automatically start this workspace at your preferred time.") - cmd.Flags().StringVar(&forUser, "user", coder.Me, "Specify the user whose resources to target") + cmd.Flags().StringVar(&forUser, "user", coder.Me, "Specify the user whose resources to target. This flag can only be used by admins and managers. Input an email or user id.") _ = cmd.MarkFlagRequired("image") return cmd } From 7a0b09b205db118c8ebc41430cdd2dc2632da266 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Wed, 8 Sep 2021 16:30:00 +0000 Subject: [PATCH 6/7] Make gendocs --- docs/coder_workspaces_create.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/coder_workspaces_create.md b/docs/coder_workspaces_create.md index c1bd266d..ab4f8731 100644 --- a/docs/coder_workspaces_create.md +++ b/docs/coder_workspaces_create.md @@ -33,7 +33,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 -o, --org string name of the organization the workspace should be created under. --provider string name of Workspace Provider with which to create the workspace -t, --tag string tag of the image the workspace will be based off of. (default "latest") - --user string Specify the user whose resources to target (default "me") + --user string Specify the user whose resources to target. This flag can only be used by admins and managers. Input an email or user id. (default "me") ``` ### Options inherited from parent commands From 48441f4a94d929c6939f8873b476f8e3d73ea3b7 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 9 Sep 2021 10:57:42 -0500 Subject: [PATCH 7/7] Fix forUser field --- internal/cmd/workspaces.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/cmd/workspaces.go b/internal/cmd/workspaces.go index 950bfd60..8ac30565 100644 --- a/internal/cmd/workspaces.go +++ b/internal/cmd/workspaces.go @@ -462,6 +462,8 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1 } forUser = u.ID forEmail = u.Email + } else if forUser == coder.Me { + forUser = "" // coder.Me means it's not for someone else, set blank } // ExactArgs(1) ensures our name value can't panic on an out of bounds.