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

Commit40e39c7

Browse files
committed
Add ability to create new environments and images
1 parent2d52bb3 commit40e39c7

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

‎coder-sdk/env.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package coder
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"time"
78

9+
"nhooyr.io/websocket/wsjson"
10+
811
"cdr.dev/coder-cli/internal/x/xjson"
912
"nhooyr.io/websocket"
1013
)
@@ -75,3 +78,127 @@ func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn
7578
}
7679
returnconn,nil
7780
}
81+
82+
// CreateEnvironmentRequest is used to configure a new environment
83+
typeCreateEnvironmentRequeststruct {
84+
Namestring`json:"name"`
85+
ImageIDstring`json:"image_id"`
86+
ImageTagstring`json:"image_tag"`
87+
CPUCoresfloat32`json:"cpu_cores"`
88+
MemoryGBint`json:"memory_gb"`
89+
DiskGBint`json:"disk_gb"`
90+
GPUsint`json:"gpus"`
91+
Services []string`json:"services"`
92+
}
93+
94+
// CreateEnvironment sends a request to create an environment.
95+
func (cClient)CreateEnvironment(ctx context.Context,orgIDstring,reqCreateEnvironmentRequest) (Environment,error) {
96+
varenvEnvironment
97+
err:=c.requestBody(
98+
ctx,
99+
http.MethodPost,"/api/orgs/"+orgID+"/environments",
100+
req,
101+
&env,
102+
)
103+
returnenv,err
104+
}
105+
106+
typeenvUpdatestruct {
107+
Typestring`json:"type"`
108+
}
109+
110+
// WaitForEnvironmentReady watches the environment update websocket and waits for the "done" message type before returning.
111+
func (cClient)WaitForEnvironmentReady(ctx context.Context,envIDstring)error {
112+
u:=c.copyURL()
113+
ifc.BaseURL.Scheme=="https" {
114+
u.Scheme="wss"
115+
}else {
116+
u.Scheme="ws"
117+
}
118+
u.Path="/api/environments/"+envID+"/watch-update"
119+
120+
conn,resp,err:=websocket.Dial(ctx,u.String(),
121+
&websocket.DialOptions{
122+
HTTPHeader:map[string][]string{
123+
"Cookie": {"session_token="+c.Token},
124+
},
125+
},
126+
)
127+
iferr!=nil {
128+
ifresp!=nil {
129+
returnbodyError(resp)
130+
}
131+
returnerr
132+
}
133+
134+
for {
135+
m:=envUpdate{}
136+
err=wsjson.Read(ctx,conn,&m)
137+
iferr!=nil {
138+
returnfmt.Errorf("read ws json msg: %w",err)
139+
}
140+
ifm.Type=="done" {
141+
break
142+
}
143+
}
144+
145+
returnnil
146+
}
147+
148+
typestatsstruct {
149+
ContainerStatusstring`json:"container_status"`
150+
StatErrorstring`json:"stat_error"`
151+
Timestring`json:"time"`
152+
}
153+
154+
// WatchEnvironmentStats watches the environment update websocket for a given duration.
155+
func (cClient)WatchEnvironmentStats(ctx context.Context,envIDstring,duration time.Duration)error {
156+
u:=c.copyURL()
157+
ifc.BaseURL.Scheme=="https" {
158+
u.Scheme="wss"
159+
}else {
160+
u.Scheme="ws"
161+
}
162+
u.Path="/api/environments/"+envID+"/watch-stats"
163+
164+
conn,resp,err:=websocket.Dial(ctx,u.String(),
165+
&websocket.DialOptions{
166+
HTTPHeader:map[string][]string{
167+
"Cookie": {"session_token="+c.Token},
168+
},
169+
},
170+
)
171+
iferr!=nil {
172+
ifresp!=nil {
173+
returnbodyError(resp)
174+
}
175+
returnerr
176+
}
177+
178+
statsCtx,statsCancel:=context.WithTimeout(ctx,duration)
179+
deferstatsCancel()
180+
181+
for {
182+
select {
183+
case<-statsCtx.Done():
184+
returnnil
185+
default:
186+
m:=stats{}
187+
err=wsjson.Read(ctx,conn,&m)
188+
iferr!=nil {
189+
returnfmt.Errorf("read ws json msg: %w",err)
190+
}
191+
}
192+
}
193+
}
194+
195+
// DeleteEnvironment deletes the environment.
196+
func (cClient)DeleteEnvironment(ctx context.Context,envIDstring)error {
197+
err:=c.requestBody(
198+
ctx,
199+
http.MethodDelete,"/api/environments/"+envID,
200+
nil,
201+
nil,
202+
)
203+
returnerr
204+
}

‎coder-sdk/image.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package coder
2+
3+
import (
4+
"context"
5+
"net/http"
6+
)
7+
8+
// Image describes a Coder Image
9+
typeImagestruct {
10+
IDstring`json:"id"`
11+
OrganizationIDstring`json:"organization_id"`
12+
Repositorystring`json:"repository"`
13+
Descriptionstring`json:"description"`
14+
URLstring`json:"url"`// user-supplied URL for image
15+
DefaultCPUCoresfloat32`json:"default_cpu_cores"`
16+
DefaultMemoryGBint`json:"default_memory_gb"`
17+
DefaultDiskGBint`json:"default_disk_gb"`
18+
Deprecatedbool`json:"deprecated"`
19+
}
20+
21+
// NewRegistryRequest describes a docker registry used in importing an image
22+
typeNewRegistryRequeststruct {
23+
FriendlyNamestring`json:"friendly_name"`
24+
Registrystring`json:"registry"`
25+
Usernamestring`json:"username"`
26+
Passwordstring`json:"password"`
27+
}
28+
29+
// ImportImageRequest is used to import new images and registries into Coder
30+
typeImportImageRequeststruct {
31+
// RegistryID is used to import images to existing registries.
32+
RegistryID*string`json:"registry_id"`
33+
// NewRegistry is used when adding a new registry.
34+
NewRegistry*NewRegistryRequest`json:"new_registry"`
35+
// Repository refers to the image. For example: "codercom/ubuntu".
36+
Repositorystring`json:"repository"`
37+
Tagstring`json:"tag"`
38+
DefaultCPUCoresfloat32`json:"default_cpu_cores"`
39+
DefaultMemoryGBint`json:"default_memory_gb"`
40+
DefaultDiskGBint`json:"default_disk_gb"`
41+
Descriptionstring`json:"description"`
42+
URLstring`json:"url"`
43+
}
44+
45+
// ImportImage creates a new image and optionally a new registry
46+
func (cClient)ImportImage(ctx context.Context,orgIDstring,reqImportImageRequest) (Image,error) {
47+
varimgImage
48+
err:=c.requestBody(
49+
ctx,
50+
http.MethodPost,"/api/orgs/"+orgID+"/images",
51+
req,
52+
&img,
53+
)
54+
returnimg,err
55+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp