This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
feature: support filtering by workspace provider when listing environ… #282
Merged
fuskovic
merged 6 commits into
master
from
faris/ch7505/add-provider-flag-to-coder-envs-ls
Mar 11, 2021
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
suggested changes
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ type resourceTopOptions struct { | |
user string | ||
org string | ||
sortBy string | ||
provider string | ||
showEmptyGroups bool | ||
} | ||
|
||
|
@@ -41,11 +42,13 @@ func resourceTop() *cobra.Command { | |
Example: `coder resources top --group org | ||
coder resources top --group org --verbose --org DevOps | ||
coder resources top --group user --verbose --user [email protected] | ||
coder resources top --group provider --verbose --provider myprovider | ||
coder resources top --sort-by memory --show-empty`, | ||
} | ||
cmd.Flags().StringVar(&options.group, "group", "user", "the grouping parameter (user|org)") | ||
cmd.Flags().StringVar(&options.group, "group", "user", "the grouping parameter (user|org|provider)") | ||
cmd.Flags().StringVar(&options.user, "user", "", "filter by a user email") | ||
cmd.Flags().StringVar(&options.org, "org", "", "filter by the name of an organization") | ||
cmd.Flags().StringVar(&options.provider, "provider", "", "filter by the name of a workspace provider") | ||
cmd.Flags().StringVar(&options.sortBy, "sort-by", "cpu", "field to sort aggregate groups and environments by (cpu|memory)") | ||
cmd.Flags().BoolVar(&options.showEmptyGroups, "show-empty", false, "show groups with zero active environments") | ||
|
||
|
@@ -84,13 +87,20 @@ func runResourceTop(options *resourceTopOptions) func(cmd *cobra.Command, args [ | |
return xerrors.Errorf("get organizations: %w", err) | ||
} | ||
|
||
providers, err := client.WorkspaceProviders(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("get workspace providers: %w", err) | ||
} | ||
|
||
var groups []groupable | ||
var labeler envLabeler | ||
switch options.group { | ||
case "user": | ||
groups, labeler = aggregateByUser(users, orgs, envs, *options) | ||
case "org": | ||
groups, labeler = aggregateByOrg(users, orgs, envs, *options) | ||
case "provider": | ||
groups, labeler = aggregateByProvider(providers.Kubernetes, orgs, envs, *options) | ||
default: | ||
return xerrors.Errorf("unknown --group %q", options.group) | ||
} | ||
|
@@ -143,6 +153,28 @@ func aggregateByOrg(users []coder.User, orgs []coder.Organization, envs []coder. | |
return groups, userLabeler{userIDMap} | ||
} | ||
|
||
func aggregateByProvider(providers []coder.KubernetesProvider, orgs []coder.Organization, envs []coder.Environment, options resourceTopOptions) ([]groupable, envLabeler) { | ||
var groups []groupable | ||
providerIDMap := make(map[string]coder.KubernetesProvider) | ||
for _, p := range providers { | ||
providerIDMap[p.ID] = p | ||
} | ||
providerEnvs := make(map[string][]coder.Environment, len(orgs)) | ||
for _, e := range envs { | ||
if options.user != "" && providerIDMap[e.ResourcePoolID].Name != options.provider { | ||
continue | ||
} | ||
providerEnvs[e.OrganizationID] = append(providerEnvs[e.OrganizationID], e) | ||
} | ||
for _, o := range orgs { | ||
if options.org != "" && o.Name != options.org { | ||
continue | ||
} | ||
groups = append(groups, orgGrouping{org: o, envs: providerEnvs[o.ID]}) | ||
} | ||
return groups, providerLabeler{providerIDMap} | ||
} | ||
|
||
// groupable specifies a structure capable of being an aggregation group of environments (user, org, all). | ||
type groupable interface { | ||
header() string | ||
|
@@ -287,6 +319,14 @@ func (u userLabeler) label(e coder.Environment) string { | |
return fmt.Sprintf("[user: %s]", u.userMap[e.UserID].Email) | ||
} | ||
|
||
type providerLabeler struct { | ||
providerMap map[string]coder.KubernetesProvider | ||
} | ||
|
||
func (p providerLabeler) label(e coder.Environment) string { | ||
return fmt.Sprintf("[provider %s]", p.providerMap[e.ResourcePoolID].Name) | ||
} | ||
|
||
func aggregateEnvResources(envs []coder.Environment) resources { | ||
var aggregate resources | ||
for _, e := range envs { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.