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

Commit84a4c71

Browse files
committed
Add more coder-sdk update, create, and delete operations
1 parent7a9addf commit84a4c71

File tree

7 files changed

+157
-59
lines changed

7 files changed

+157
-59
lines changed

‎coder-sdk/env.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"time"
77

8-
"cdr.dev/coder-cli/internal/x/xjson"
98
"golang.org/x/xerrors"
109
"nhooyr.io/websocket"
1110
"nhooyr.io/websocket/wsjson"
@@ -31,14 +30,14 @@ type Environment struct {
3130
UpdatedAt time.Time`json:"updated_at" table:"-"`
3231
LastOpenedAt time.Time`json:"last_opened_at" table:"-"`
3332
LastConnectionAt time.Time`json:"last_connection_at" table:"-"`
34-
AutoOffThresholdxjson.MSDuration`json:"auto_off_threshold" table:"-"`
33+
AutoOffThresholdDuration`json:"auto_off_threshold" table:"-"`
3534
}
3635

3736
// RebuildMessage defines the message shown when an Environment requires a rebuild for it can be accessed.
3837
typeRebuildMessagestruct {
39-
Textstring`json:"text"`
40-
Requiredbool`json:"required"`
41-
AutoOffThresholdxjson.MSDuration`json:"auto_off_threshold" table:"-"`
38+
Textstring`json:"text"`
39+
Requiredbool`json:"required"`
40+
AutoOffThresholdDuration`json:"auto_off_threshold"`
4241
}
4342

4443
// EnvironmentStat represents the state of an environment

‎coder-sdk/error.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ var ErrNotFound = xerrors.Errorf("resource not found")
1414

1515
// APIError is the expected payload format for our errors.
1616
typeAPIErrorstruct {
17-
Errstruct {
18-
Msgstring`json:"msg"`
19-
}`json:"error"`
17+
ErrAPIErrorMsg`json:"error"`
18+
}
19+
20+
// APIErrorMsg contains the rich error information returned by API errors.
21+
typeAPIErrorMsgstruct {
22+
Msgstring`json:"msg"`
2023
}
2124

2225
// HTTPError represents an error from the Coder API.

‎coder-sdk/image.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ type NewRegistryRequest struct {
2626
Passwordstring`json:"password"`
2727
}
2828

29-
//ImportImageRequest is used to import new images and registries into Coder
30-
typeImportImageRequeststruct {
29+
//ImportImageReq is used to import new images and registries into Coder
30+
typeImportImageReqstruct {
3131
RegistryID*string`json:"registry_id"`// Used to import images to existing registries.
3232
NewRegistry*NewRegistryRequest`json:"new_registry"`// Used when adding a new registry.
3333
Repositorystring`json:"repository"`// Refers to the image. Ex: "codercom/ubuntu".
@@ -40,7 +40,7 @@ type ImportImageRequest struct {
4040
}
4141

4242
// ImportImage creates a new image and optionally a new registry
43-
func (cClient)ImportImage(ctx context.Context,orgIDstring,reqImportImageRequest) (*Image,error) {
43+
func (cClient)ImportImage(ctx context.Context,orgIDstring,reqImportImageReq) (*Image,error) {
4444
varimgImage
4545
iferr:=c.requestBody(ctx,http.MethodPost,"/api/orgs/"+orgID+"/images",req,&img);err!=nil {
4646
returnnil,err

‎coder-sdk/org.go

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@ type Organization struct {
1616
// OrganizationUser user wraps the basic User type and adds data specific to the user's membership of an organization
1717
typeOrganizationUserstruct {
1818
User
19-
OrganizationRoles []OrganizationRole`json:"organization_roles"`
20-
RolesUpdatedAt time.Time`json:"roles_updated_at"`
19+
OrganizationRoles []Role`json:"organization_roles"`
20+
RolesUpdatedAt time.Time`json:"roles_updated_at"`
2121
}
2222

23-
// OrganizationRole defines an organization OrganizationRole
24-
typeOrganizationRolestring
25-
26-
// The OrganizationRole enum values
23+
// Organization Roles
2724
const (
28-
RoleOrgMemberOrganizationRole="organization-member"
29-
RoleOrgAdminOrganizationRole="organization-admin"
30-
RoleOrgManagerOrganizationRole="organization-manager"
25+
RoleOrgMemberRole="organization-member"
26+
RoleOrgAdminRole="organization-admin"
27+
RoleOrgManagerRole="organization-manager"
3128
)
3229

3330
// Organizations gets all Organizations
@@ -39,11 +36,51 @@ func (c Client) Organizations(ctx context.Context) ([]Organization, error) {
3936
returnorgs,nil
4037
}
4138

42-
// OrgMembers get all members of the given organization
43-
func (cClient)OrgMembers(ctx context.Context,orgIDstring) ([]OrganizationUser,error) {
39+
func (cClient)OrganizationByID(ctx context.Context,orgIDstring) (*Organization,error) {
40+
varorgOrganization
41+
err:=c.requestBody(ctx,http.MethodGet,"/api/orgs/"+orgID,nil,&org)
42+
iferr!=nil {
43+
returnnil,err
44+
}
45+
return&org,nil
46+
}
47+
48+
// OrganizationMembers get all members of the given organization
49+
func (cClient)OrganizationMembers(ctx context.Context,orgIDstring) ([]OrganizationUser,error) {
4450
varmembers []OrganizationUser
4551
iferr:=c.requestBody(ctx,http.MethodGet,"/api/orgs/"+orgID+"/members",nil,&members);err!=nil {
4652
returnnil,err
4753
}
4854
returnmembers,nil
4955
}
56+
57+
typeUpdateOrganizationReqstruct {
58+
Name*string`json:"name"`
59+
Description*string`json:"description"`
60+
Default*bool`json:"default"`
61+
AutoOffThreshold*Duration`json:"auto_off_threshold"`
62+
CPUProvisioningRate*float32`json:"cpu_provisioning_rate"`
63+
MemoryProvisioningRate*float32`json:"memory_provisioning_rate"`
64+
}
65+
66+
func (cClient)UpdateOrganization(ctx context.Context,orgIDstring,reqUpdateOrganizationReq)error {
67+
returnc.requestBody(ctx,http.MethodPatch,"/api/orgs/"+orgID,req,nil)
68+
}
69+
70+
typeCreateOrganizationReqstruct {
71+
Namestring`json:"name"`
72+
Descriptionstring`json:"description"`
73+
Defaultbool`json:"default"`
74+
ResourceNamespacestring`json:"resource_namespace"`
75+
AutoOffThresholdDuration`json:"auto_off_threshold"`
76+
CPUProvisioningRatefloat32`json:"cpu_provisioning_rate"`
77+
MemoryProvisioningRatefloat32`json:"memory_provisioning_rate"`
78+
}
79+
80+
func (cClient)CreateOrganization(ctx context.Context,reqCreateOrganizationReq)error {
81+
returnc.requestBody(ctx,http.MethodPost,"/api/orgs",req,nil)
82+
}
83+
84+
func (cClient)DeleteOrganization(ctx context.Context,orgIDstring)error {
85+
returnc.requestBody(ctx,http.MethodDelete,"/api/orgs/"+orgID,nil,nil)
86+
}

‎coder-sdk/users.go

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,38 @@ import (
88

99
// User describes a Coder user account.
1010
typeUserstruct {
11-
IDstring`json:"id" table:"-"`
12-
Emailstring`json:"email" table:"Email"`
13-
Usernamestring`json:"username" table:"Username"`
14-
Namestring`json:"name" table:"Name"`
15-
CreatedAt time.Time`json:"created_at" table:"CreatedAt"`
16-
UpdatedAt time.Time`json:"updated_at" table:"-"`
11+
IDstring`json:"id" table:"-"`
12+
Emailstring`json:"email" table:"Email"`
13+
Usernamestring`json:"username" table:"Username"`
14+
Namestring`json:"name" table:"Name"`
15+
Roles []Role`json:"roles" table:"-"`
16+
TemporaryPasswordbool`json:"temporary_password" table:"-"`
17+
LoginTypestring`json:"login_type" table:"-"`
18+
KeyRegeneratedAt time.Time`json:"key_regenerated_at" table:"-"`
19+
CreatedAt time.Time`json:"created_at" table:"CreatedAt"`
20+
UpdatedAt time.Time`json:"updated_at" table:"-"`
1721
}
1822

23+
typeRolestring
24+
25+
typeRoles []Role
26+
27+
// Site Roles
28+
const (
29+
SiteAdminRole="site-admin"
30+
SiteAuditorRole="site-auditor"
31+
SiteManagerRole="site-manager"
32+
SiteMemberRole="site-member"
33+
)
34+
35+
typeLoginTypestring
36+
37+
const (
38+
LoginTypeBuiltInLoginType="built-in"
39+
LoginTypeSAMLLoginType="saml"
40+
LoginTypeOIDCLoginType="oidc"
41+
)
42+
1943
// Me gets the details of the authenticated user.
2044
func (cClient)Me(ctx context.Context) (*User,error) {
2145
returnc.UserByID(ctx,Me)
@@ -70,3 +94,34 @@ func (c Client) UserByEmail(ctx context.Context, email string) (*User, error) {
7094
}
7195
returnnil,ErrNotFound
7296
}
97+
98+
// UpdateUserReq defines a modification to the user, updating the
99+
// value of all non-nil values.
100+
typeUpdateUserReqstruct {
101+
// TODO(@cmoog) add update password option
102+
Revoked*bool`json:"revoked,omitempty"`
103+
Roles*Roles`json:"roles,omitempty"`
104+
LoginType*LoginType`json:"login_type,omitempty"`
105+
Name*string`json:"name,omitempty"`
106+
Username*string`json:"username,omitempty"`
107+
Email*string`json:"email,omitempty"`
108+
DotfilesGitURL*string`json:"dotfiles_git_uri,omitempty"`
109+
}
110+
111+
func (cClient)UpdateUser(ctx context.Context,userIDstring,reqUpdateUserReq)error {
112+
returnc.requestBody(ctx,http.MethodPatch,"/api/users/"+userID,req,nil)
113+
}
114+
115+
typeCreateUserReqstruct {
116+
Namestring`json:"name"`
117+
Usernamestring`json:"username"`
118+
Emailstring`json:"email"`
119+
Passwordstring`json:"password"`
120+
TemporaryPasswordbool`json:"temporary_password"`
121+
LoginTypeLoginType`json:"login_type"`
122+
OrganizationsIDs []string`json:"organizations"`
123+
}
124+
125+
func (cClient)CreateUser(ctx context.Context,reqCreateUserReq)error {
126+
returnc.requestBody(ctx,http.MethodPost,"/api/users",req,nil)
127+
}

‎coder-sdk/util.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package coder
2+
3+
import (
4+
"encoding/json"
5+
"strconv"
6+
"time"
7+
)
8+
9+
funcString(sstring)*string {
10+
return&s
11+
}
12+
13+
// Duration is a time.Duration wrapper that marshals to millisecond precision.
14+
// While it looses precision, most javascript applications expect durations to be in milliseconds.
15+
typeDuration time.Duration
16+
17+
// MarshalJSON marshals the duration to millisecond precision.
18+
func (dDuration)MarshalJSON() ([]byte,error) {
19+
du:=time.Duration(d)
20+
returnjson.Marshal(du.Milliseconds())
21+
}
22+
23+
// UnmarshalJSON unmarshals a millisecond-precision integer to
24+
// a time.Duration.
25+
func (d*Duration)UnmarshalJSON(b []byte)error {
26+
i,err:=strconv.ParseInt(string(b),10,64)
27+
iferr!=nil {
28+
returnerr
29+
}
30+
31+
*d=Duration(time.Duration(i)*time.Millisecond)
32+
returnnil
33+
}
34+
35+
func (dDuration)String()string {returntime.Duration(d).String() }

‎internal/x/xjson/duration.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp