Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit ac39d3c

Browse files
authored
Merge pull request #111 from cdr/stress-sdk-2
Add ws dials for resource load and ide status
2 parents 8022bfc + 89752df commit ac39d3c

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

coder-sdk/env.go

+59-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"time"
77

88
"cdr.dev/coder-cli/internal/x/xjson"
9+
"golang.org/x/xerrors"
910
"nhooyr.io/websocket"
11+
"nhooyr.io/websocket/wsjson"
1012
)
1113

1214
// Environment describes a Coder environment
@@ -81,14 +83,14 @@ type CreateEnvironmentRequest struct {
8183

8284
// CreateEnvironment sends a request to create an environment.
8385
func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateEnvironmentRequest) (*Environment, error) {
84-
var env *Environment
86+
var env Environment
8587
err := c.requestBody(
8688
ctx,
8789
http.MethodPost, "/api/orgs/"+orgID+"/environments",
8890
req,
89-
env,
91+
&env,
9092
)
91-
return env, err
93+
return &env, err
9294
}
9395

9496
// EnvironmentsByOrganization gets the list of environments owned by the given user.
@@ -119,6 +121,11 @@ func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn
119121
return c.dialWs(ctx, "/proxy/environments/"+env.ID+"/wsep")
120122
}
121123

124+
// DialIDEStatus opens a websocket connection for cpu load metrics on the environment
125+
func (c Client) DialIDEStatus(ctx context.Context, envID string) (*websocket.Conn, error) {
126+
return c.dialWs(ctx, "/proxy/environments/"+envID+"/ide/api/status")
127+
}
128+
122129
// DialEnvironmentBuildLog opens a websocket connection for the environment build log messages
123130
func (c Client) DialEnvironmentBuildLog(ctx context.Context, envID string) (*websocket.Conn, error) {
124131
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-update")
@@ -128,3 +135,52 @@ func (c Client) DialEnvironmentBuildLog(ctx context.Context, envID string) (*web
128135
func (c Client) DialEnvironmentStats(ctx context.Context, envID string) (*websocket.Conn, error) {
129136
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-stats")
130137
}
138+
139+
// DialResourceLoad opens a websocket connection for cpu load metrics on the environment
140+
func (c Client) DialResourceLoad(ctx context.Context, envID string) (*websocket.Conn, error) {
141+
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-resource-load")
142+
}
143+
144+
// BuildLogType describes the type of an event.
145+
type BuildLogType string
146+
147+
const (
148+
// BuildLogTypeStart signals that a new build log has begun.
149+
BuildLogTypeStart BuildLogType = "start"
150+
// BuildLogTypeStage is a stage-level event for an environment.
151+
// It can be thought of as a major step in the environment's
152+
// lifecycle.
153+
BuildLogTypeStage BuildLogType = "stage"
154+
// BuildLogTypeError describes an error that has occurred.
155+
BuildLogTypeError BuildLogType = "error"
156+
// BuildLogTypeSubstage describes a subevent that occurs as
157+
// part of a stage. This can be the output from a user's
158+
// personalization script, or a long running command.
159+
BuildLogTypeSubstage BuildLogType = "substage"
160+
// BuildLogTypeDone signals that the build has completed.
161+
BuildLogTypeDone BuildLogType = "done"
162+
)
163+
164+
type buildLogMsg struct {
165+
Type BuildLogType `json:"type"`
166+
}
167+
168+
// WaitForEnvironmentReady will watch the build log and return when done
169+
func (c Client) WaitForEnvironmentReady(ctx context.Context, env *Environment) error {
170+
conn, err := c.DialEnvironmentBuildLog(ctx, env.ID)
171+
if err != nil {
172+
return xerrors.Errorf("%s: dial build log: %w", env.Name, err)
173+
}
174+
175+
for {
176+
msg := buildLogMsg{}
177+
err := wsjson.Read(ctx, conn, &msg)
178+
if err != nil {
179+
return xerrors.Errorf("%s: reading build log msg: %w", env.Name, err)
180+
}
181+
182+
if msg.Type == BuildLogTypeDone {
183+
return nil
184+
}
185+
}
186+
}

0 commit comments

Comments
 (0)