Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit edf70b9

Browse files
authored
feat: Allow making a workspace for another user as an admin (#427)
* Allow making a workspace for another user as an admin
1 parent 3582a0d commit edf70b9

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

coder-sdk/workspace.go

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ type CreateWorkspaceRequest struct {
8989
Namespace string `json:"namespace"`
9090
EnableAutoStart bool `json:"autostart_enabled"`
9191

92+
// ForUserID is an optional param to create a workspace for another user
93+
// other than the requester. This only works for admins and site managers.
94+
ForUserID string `json:"for_user_id,omitempty"`
95+
9296
// TemplateID comes from the parse template route on cemanager.
9397
TemplateID string `json:"template_id,omitempty"`
9498
}

docs/coder_workspaces_create.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1
3333
-o, --org string name of the organization the workspace should be created under.
3434
--provider string name of Workspace Provider with which to create the workspace
3535
-t, --tag string tag of the image the workspace will be based off of. (default "latest")
36+
--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")
3637
```
3738

3839
### Options inherited from parent commands

internal/cmd/workspaces.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ func createWorkspaceCmd() *cobra.Command {
397397
useCVM bool
398398
providerName string
399399
enableAutostart bool
400+
forUser string // Optional
400401
)
401402

402403
cmd := &cobra.Command{
@@ -448,6 +449,23 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1
448449
}
449450
}
450451

452+
var forEmail string
453+
if forUser != "" && forUser != coder.Me {
454+
// Making a workspace for another user, do they exist?
455+
u, err := client.UserByEmail(ctx, forUser)
456+
if err != nil {
457+
// Try by ID?
458+
u, err = client.UserByID(ctx, forUser)
459+
if err != nil {
460+
return xerrors.Errorf("the user %q was not found: %w", forUser, err)
461+
}
462+
}
463+
forUser = u.ID
464+
forEmail = u.Email
465+
} else if forUser == coder.Me {
466+
forUser = "" // coder.Me means it's not for someone else, set blank
467+
}
468+
451469
// ExactArgs(1) ensures our name value can't panic on an out of bounds.
452470
createReq := &coder.CreateWorkspaceRequest{
453471
Name: args[0],
@@ -462,6 +480,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1
462480
ResourcePoolID: provider.ID,
463481
Namespace: provider.DefaultNamespace,
464482
EnableAutoStart: enableAutostart,
483+
ForUserID: forUser,
465484
}
466485

467486
// if any of these defaulted to their zero value we provision
@@ -489,9 +508,13 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1
489508
return nil
490509
}
491510

511+
extraFlags := ""
512+
if forEmail != coder.Me && forEmail != "" {
513+
extraFlags = " --user " + forEmail
514+
}
492515
clog.LogSuccess("creating workspace...",
493516
clog.BlankLine,
494-
clog.Tipf(`run "coder workspaces watch-build %s" to trail the build logs`, workspace.Name),
517+
clog.Tipf(`run "coder workspaces watch-build %s%s" to trail the build logs`, workspace.Name, extraFlags),
495518
)
496519
return nil
497520
},
@@ -507,6 +530,7 @@ coder workspaces create my-new-powerful-workspace --cpu 12 --disk 100 --memory 1
507530
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
508531
cmd.Flags().BoolVar(&useCVM, "container-based-vm", false, "deploy the workspace as a Container-based VM")
509532
cmd.Flags().BoolVar(&enableAutostart, "enable-autostart", false, "automatically start this workspace at your preferred time.")
533+
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.")
510534
_ = cmd.MarkFlagRequired("image")
511535
return cmd
512536
}

0 commit comments

Comments
 (0)