Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit166ed14

Browse files
committed
Initial prototype of resources command
1 parentb7cd44f commit166ed14

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

‎coder-sdk/env.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type Environment struct {
2121
UserIDstring`json:"user_id" tab:"-"`
2222
LastBuiltAt time.Time`json:"last_built_at" tab:"-"`
2323
CPUCoresfloat32`json:"cpu_cores" tab:"CPUCores"`
24-
MemoryGBint`json:"memory_gb" tab:"MemoryGB"`
24+
MemoryGBfloat32`json:"memory_gb" tab:"MemoryGB"`
2525
DiskGBint`json:"disk_gb" tab:"DiskGB"`
2626
GPUsint`json:"gpus" tab:"GPUs"`
2727
Updatingbool`json:"updating" tab:"Updating"`
@@ -93,6 +93,16 @@ func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateE
9393
return&env,nil
9494
}
9595

96+
// ListEnvironments lists environments returned by the given filter.
97+
// TODO: add the filter options
98+
func (cClient)ListEnvironments(ctx context.Context) ([]Environment,error) {
99+
varenvs []Environment
100+
iferr:=c.requestBody(ctx,http.MethodGet,"/api/environments",nil,&envs);err!=nil {
101+
returnnil,err
102+
}
103+
returnenvs,nil
104+
}
105+
96106
// EnvironmentsByOrganization gets the list of environments owned by the given user.
97107
func (cClient)EnvironmentsByOrganization(ctx context.Context,userID,orgIDstring) ([]Environment,error) {
98108
varenvs []Environment

‎coder-sdk/org.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,30 @@ package coder
33
import (
44
"context"
55
"net/http"
6+
"time"
67
)
78

89
// Org describes an Organization in Coder
910
typeOrgstruct {
1011
IDstring`json:"id"`
1112
Namestring`json:"name"`
12-
Members []User`json:"members"`
13+
Members []OrganizationUser`json:"members"`
1314
}
1415

16+
typeOrganizationUserstruct {
17+
User
18+
OrganizationRoles []Role`json:"organization_roles"`
19+
RolesUpdatedAt time.Time`json:"roles_updated_at"`
20+
}
21+
22+
typeRolestring
23+
24+
const (
25+
RoleOrgMemberRole="organization-member"
26+
RoleOrgAdminRole="organization-admin"
27+
RoleOrgManagerRole="organization-manager"
28+
)
29+
1530
// Orgs gets all Organizations
1631
func (cClient)Orgs(ctx context.Context) ([]Org,error) {
1732
varorgs []Org
@@ -20,3 +35,11 @@ func (c Client) Orgs(ctx context.Context) ([]Org, error) {
2035
}
2136
returnorgs,nil
2237
}
38+
39+
func (cClient)OrgMembers(ctx context.Context,orgIDstring) ([]OrganizationUser,error) {
40+
varmembers []OrganizationUser
41+
iferr:=c.requestBody(ctx,http.MethodGet,"/api/orgs/"+orgID+"/members",nil,&members);err!=nil {
42+
returnnil,err
43+
}
44+
returnmembers,nil
45+
}

‎internal/cmd/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func Make() *cobra.Command {
2424
makeEnvsCommand(),
2525
makeSyncCmd(),
2626
makeURLCmd(),
27+
makeResourceCmd(),
2728
completionCmd,
2829
genDocs(app),
2930
)

‎internal/cmd/resourcemanager.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"text/tabwriter"
7+
8+
"cdr.dev/coder-cli/coder-sdk"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
funcmakeResourceCmd()*cobra.Command {
13+
cmd:=&cobra.Command{
14+
Use:"resources",
15+
Short:"manager Coder resources with platform-level context (users, organizations, environments)",
16+
}
17+
cmd.AddCommand(resourceTop)
18+
returncmd
19+
}
20+
21+
varresourceTop=&cobra.Command{
22+
Use:"top",
23+
RunE:func(cmd*cobra.Command,args []string)error {
24+
ctx:=cmd.Context()
25+
26+
client,err:=newClient()
27+
iferr!=nil {
28+
returnerr
29+
}
30+
31+
envs,err:=client.ListEnvironments(ctx)
32+
iferr!=nil {
33+
returnerr
34+
}
35+
36+
userEnvs:=make(map[string][]coder.Environment)
37+
for_,e:=rangeenvs {
38+
userEnvs[e.UserID]=append(userEnvs[e.UserID],e)
39+
}
40+
41+
users,err:=client.Users(ctx)
42+
iferr!=nil {
43+
returnerr
44+
}
45+
46+
orgs:=make(map[string]coder.Org)
47+
orglist,err:=client.Orgs(ctx)
48+
iferr!=nil {
49+
returnerr
50+
}
51+
for_,o:=rangeorglist {
52+
orgs[o.ID]=o
53+
}
54+
55+
tabwriter:=tabwriter.NewWriter(os.Stdout,0,0,4,' ',0)
56+
for_,u:=rangeusers {
57+
_,_=fmt.Fprintf(tabwriter,"%s\t(%s)\t%s",u.Name,u.Email,aggregateEnvResources(userEnvs[u.ID]))
58+
iflen(userEnvs[u.ID])>0 {
59+
_,_=fmt.Fprintf(tabwriter,"\f")
60+
}
61+
for_,env:=rangeuserEnvs[u.ID] {
62+
_,_=fmt.Fprintf(tabwriter,"\t")
63+
_,_=fmt.Fprintln(tabwriter,fmtEnvResources(env,orgs))
64+
}
65+
fmt.Fprint(tabwriter,"\n")
66+
}
67+
_=tabwriter.Flush()
68+
69+
returnnil
70+
},
71+
}
72+
73+
funcresourcesFromEnv(env coder.Environment)resources {
74+
returnresources{
75+
cpuAllocation:env.CPUCores,
76+
cpuUtilization:env.LatestStat.CPUUsage,
77+
memAllocation:env.MemoryGB,
78+
memUtilization:env.LatestStat.MemoryUsage,
79+
}
80+
}
81+
82+
funcfmtEnvResources(env coder.Environment,orgsmap[string]coder.Org)string {
83+
returnfmt.Sprintf("%s\t%s\t[org: %s]",env.Name,resourcesFromEnv(env),orgs[env.OrganizationID].Name)
84+
}
85+
86+
funcaggregateEnvResources(envs []coder.Environment) (resources) {
87+
varaggregateresources
88+
for_,e:=rangeenvs {
89+
aggregate.cpuAllocation+=e.CPUCores
90+
aggregate.cpuUtilization+=e.LatestStat.CPUUsage
91+
aggregate.memAllocation+=e.MemoryGB
92+
aggregate.memUtilization+=e.LatestStat.MemoryUsage
93+
}
94+
returnaggregate
95+
}
96+
97+
typeresourcesstruct {
98+
cpuAllocationfloat32
99+
cpuUtilizationfloat32
100+
memAllocationfloat32
101+
memUtilizationfloat32
102+
}
103+
104+
func (aresources)String()string {
105+
returnfmt.Sprintf("[cpu: alloc=%.1fvCPU, util=%.1f]\t[mem: alloc=%.1fGB, util=%.1f]",a.cpuAllocation,a.cpuUtilization,a.memAllocation,a.memUtilization)
106+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp