Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions coder-sdk/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
1 change: 1 addition & 0 deletions docs/coder_workspaces_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion internal/cmd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ func createWorkspaceCmd() *cobra.Command {
useCVM bool
providerName string
enableAutostart bool
forUser string // Optional
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -448,6 +449,21 @@ 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)
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
forEmail = u.Email
}

// ExactArgs(1) ensures our name value can't panic on an out of bounds.
createReq := &coder.CreateWorkspaceRequest{
Name: args[0],
Expand All @@ -462,6 +478,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
Expand Down Expand Up @@ -489,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
},
Expand All @@ -507,6 +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.MarkFlagRequired("image")
return cmd
}
Expand Down