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

Commit0f0160a

Browse files
authored
Merge pull request#100 from cdr/stress-sdk
Add ability to create new environments and images to the coder-sdk
2 parentsddaeba7 +af6e77c commit0f0160a

File tree

3 files changed

+131
-23
lines changed

3 files changed

+131
-23
lines changed

‎coder-sdk/env.go

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ type Environment struct {
3434
AutoOffThreshold xjson.Duration`json:"auto_off_threshold" tab:"-"`
3535
}
3636

37+
// CreateEnvironmentRequest is used to configure a new environment
38+
typeCreateEnvironmentRequeststruct {
39+
Namestring`json:"name"`
40+
ImageIDstring`json:"image_id"`
41+
ImageTagstring`json:"image_tag"`
42+
CPUCoresfloat32`json:"cpu_cores"`
43+
MemoryGBint`json:"memory_gb"`
44+
DiskGBint`json:"disk_gb"`
45+
GPUsint`json:"gpus"`
46+
Services []string`json:"services"`
47+
}
48+
49+
// CreateEnvironment sends a request to create an environment.
50+
func (cClient)CreateEnvironment(ctx context.Context,orgIDstring,reqCreateEnvironmentRequest) (*Environment,error) {
51+
varenv*Environment
52+
err:=c.requestBody(
53+
ctx,
54+
http.MethodPost,"/api/orgs/"+orgID+"/environments",
55+
req,
56+
env,
57+
)
58+
returnenv,err
59+
}
60+
3761
// EnvironmentsByOrganization gets the list of environments owned by the given user.
3862
func (cClient)EnvironmentsByOrganization(ctx context.Context,userID,orgIDstring) ([]Environment,error) {
3963
varenvs []Environment
@@ -46,32 +70,28 @@ func (c Client) EnvironmentsByOrganization(ctx context.Context, userID, orgID st
4670
returnenvs,err
4771
}
4872

73+
// DeleteEnvironment deletes the environment.
74+
func (cClient)DeleteEnvironment(ctx context.Context,envIDstring)error {
75+
returnc.requestBody(
76+
ctx,
77+
http.MethodDelete,"/api/environments/"+envID,
78+
nil,
79+
nil,
80+
)
81+
}
82+
4983
// DialWsep dials an environments command execution interface
5084
// See github.com/cdr/wsep for details
5185
func (cClient)DialWsep(ctx context.Context,env*Environment) (*websocket.Conn,error) {
52-
u:=c.copyURL()
53-
ifc.BaseURL.Scheme=="https" {
54-
u.Scheme="wss"
55-
}else {
56-
u.Scheme="ws"
57-
}
58-
u.Path="/proxy/environments/"+env.ID+"/wsep"
86+
returnc.dialWs(ctx,"/proxy/environments/"+env.ID+"/wsep")
87+
}
5988

60-
ctx,cancel:=context.WithTimeout(ctx,time.Second*15)
61-
defercancel()
89+
// DialEnvironmentBuildLog opens a websocket connection for the environment build log messages
90+
func (cClient)DialEnvironmentBuildLog(ctx context.Context,envIDstring) (*websocket.Conn,error) {
91+
returnc.dialWs(ctx,"/api/environments/"+envID+"/watch-update")
92+
}
6293

63-
conn,resp,err:=websocket.Dial(ctx,u.String(),
64-
&websocket.DialOptions{
65-
HTTPHeader:map[string][]string{
66-
"Cookie": {"session_token="+c.Token},
67-
},
68-
},
69-
)
70-
iferr!=nil {
71-
ifresp!=nil {
72-
returnnil,bodyError(resp)
73-
}
74-
returnnil,err
75-
}
76-
returnconn,nil
94+
// DialEnvironmentStats opens a websocket connection for environment stats
95+
func (cClient)DialEnvironmentStats(ctx context.Context,envIDstring) (*websocket.Conn,error) {
96+
returnc.dialWs(ctx,"/api/environments/"+envID+"/watch-stats")
7797
}

‎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+
varimg*Image
48+
err:=c.requestBody(
49+
ctx,
50+
http.MethodPost,"/api/orgs/"+orgID+"/images",
51+
req,
52+
img,
53+
)
54+
returnimg,err
55+
}

‎coder-sdk/ws.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package coder
2+
3+
import (
4+
"context"
5+
6+
"nhooyr.io/websocket"
7+
)
8+
9+
func (cClient)dialWs(ctx context.Context,pathstring) (*websocket.Conn,error) {
10+
u:=c.copyURL()
11+
ifc.BaseURL.Scheme=="https" {
12+
u.Scheme="wss"
13+
}else {
14+
u.Scheme="ws"
15+
}
16+
u.Path=path
17+
18+
conn,resp,err:=websocket.Dial(ctx,u.String(),
19+
&websocket.DialOptions{
20+
HTTPHeader:map[string][]string{
21+
"Session-Token": {c.Token},
22+
},
23+
},
24+
)
25+
iferr!=nil {
26+
ifresp!=nil {
27+
returnnil,bodyError(resp)
28+
}
29+
returnnil,err
30+
}
31+
32+
returnconn,nil
33+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp