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

feat: add username to 'coder ws ls' #486

Merged
merged 3 commits into from
Jul 20, 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
4 changes: 2 additions & 2 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func pingAPI(ctx context.Context, workspaceURL *url.URL, token string) error {
return nil
}

// isWSL determines if coder-cli is running within Windows Subsystem for Linux
// isWSL determines if coder-cli is running within Windows Subsystem for Linux.
func isWSL() (bool, error) {
if runtime.GOOS == goosDarwin || runtime.GOOS == goosWindows {
return false, nil
Expand All @@ -130,7 +130,7 @@ func isWSL() (bool, error) {
return strings.Contains(strings.ToLower(string(data)), "microsoft"), nil
}

// openURL opens the provided URL via user's default browser
// openURL opens the provided URL via user's default browser.
func openURL(url string) error {
var cmd string
var args []string
Expand Down
81 changes: 64 additions & 17 deletions internal/coderutil/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ func DefaultWorkspaceProvider(ctx context.Context, c coder.Client) (*coder.Kuber
// WorkspaceTable defines an Workspace-like structure with associated entities composed in a human
// readable form.
type WorkspaceTable struct {
Name string `table:"Name"`
Image string `table:"Image"`
CPU float32 `table:"vCPU"`
MemoryGB float32 `table:"MemoryGB"`
DiskGB int `table:"DiskGB"`
Status string `table:"Status"`
Provider string `table:"Provider"`
CVM bool `table:"CVM"`
Name string `table:"Name" json:"name"`
Image string `table:"Image" json:"image"`
CPU float32 `table:"vCPU" json:"cpu"`
MemoryGB float32 `table:"MemoryGB" json:"memory_gb"`
DiskGB int `table:"DiskGB" json:"disk_gb"`
Status string `table:"Status" json:"status"`
Provider string `table:"Provider" json:"provider"`
CVM bool `table:"CVM" json:"cvm"`
Username string `table:"Username" json:"username"`
}

// WorkspacesHumanTable performs the composition of each Workspace with its associated ProviderName and ImageRepo.
Expand All @@ -96,6 +97,11 @@ func WorkspacesHumanTable(ctx context.Context, client coder.Client, workspaces [
return nil, err
}

userMap, err := MakeUserMap(ctx, client, workspaces)
if err != nil {
return nil, err
}

pooledWorkspaces := make([]WorkspaceTable, 0, len(workspaces))
providers, err := client.WorkspaceProviders(ctx)
if err != nil {
Expand All @@ -105,25 +111,66 @@ func WorkspacesHumanTable(ctx context.Context, client coder.Client, workspaces [
for _, p := range providers.Kubernetes {
providerMap[p.ID] = p
}
for _, e := range workspaces {
workspaceProvider, ok := providerMap[e.ResourcePoolID]
for _, ws := range workspaces {
workspaceProvider, ok := providerMap[ws.ResourcePoolID]
if !ok {
return nil, xerrors.Errorf("fetch workspace workspace provider: %w", coder.ErrNotFound)
}
pooledWorkspaces = append(pooledWorkspaces, WorkspaceTable{
Name: e.Name,
Image: fmt.Sprintf("%s:%s", imageMap[e.ImageID].Repository, e.ImageTag),
CPU: e.CPUCores,
MemoryGB: e.MemoryGB,
DiskGB: e.DiskGB,
Status: string(e.LatestStat.ContainerStatus),
Name: ws.Name,
Image: fmt.Sprintf("%s:%s", imageMap[ws.ImageID].Repository, ws.ImageTag),
CPU: ws.CPUCores,
MemoryGB: ws.MemoryGB,
DiskGB: ws.DiskGB,
Status: string(ws.LatestStat.ContainerStatus),
Provider: workspaceProvider.Name,
CVM: e.UseContainerVM,
CVM: ws.UseContainerVM,
Username: userMap[ws.UserID].Username,
})
}
return pooledWorkspaces, nil
}

func MakeUserMap(ctx context.Context, client coder.Client, workspaces []coder.Workspace) (map[string]*coder.User, error) {
var (
mu sync.Mutex
egroup = clog.LoggedErrGroup()
)

userMap := map[string]*coder.User{}

// Iterate over all the workspaces to get a list of unique User IDs.
for _, ws := range workspaces {
userMap[ws.UserID] = nil
}

fetchIds := make([]string, 0, len(userMap))
for id := range userMap {
fetchIds = append(fetchIds, id)
}

for _, id := range fetchIds {
id := id
egroup.Go(func() error {
user, err := client.UserByID(ctx, id)
if err != nil {
return xerrors.Errorf("get user by id: %w", err)
}
mu.Lock()
defer mu.Unlock()

userMap[id] = user
return nil
})
}

if err := egroup.Wait(); err != nil {
return nil, xerrors.Errorf("fetch all workspace users: %w", err)
}

return userMap, nil
}

// MakeImageMap fetches all image entities specified in the slice of workspaces, then places them into an ID map.
func MakeImageMap(ctx context.Context, client coder.Client, workspaces []coder.Workspace) (map[string]*coder.Image, error) {
var (
Expand Down
2 changes: 2 additions & 0 deletions wsnet/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ func (l *listener) dial(ctx context.Context) (<-chan error, error) {
_ = l.ws.Close(websocket.StatusNormalClosure, "new connection inbound")
}

// websocket lib documents that the response does not need to be closed.
// nolint
conn, resp, err := websocket.Dial(ctx, l.broker, nil)
if err != nil {
if resp != nil {
Expand Down