8000 chore: Add watch workspace endpoint by f0ssel · Pull Request #1493 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

chore: Add watch workspace endpoint #1493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make WorkspaceWatcher
  • Loading branch information
f0ssel committed May 18, 2022
commit da9c744fafed381c45f499354b1458288ab4961a
36 changes: 36 additions & 0 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"golang.org/x/xerrors"
"nhooyr.io/websocket"

"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/httpmw"
Expand Down Expand Up @@ -80,6 +81,41 @@ func (c *Client) Request(ctx context.Context, method, path string, body interfac
return resp, err
}

// request performs an HTTP request with the body provided.
// The caller is responsible for closing the response body.
func (c *Client) websocket(ctx context.Context, path string) (*websocket.Conn, error) {
serverURL, err := c.URL.Parse(path)
if err != nil {
return nil, xerrors.Errorf("parse url: %w", err)
}

apiURL, err := url.Parse(serverURL.String())
apiURL.Scheme = "ws"
if serverURL.Scheme == "https" {
apiURL.Scheme = "wss"
}
apiURL.Path = path

client := &http.Client{
Jar: c.HTTPClient.Jar,
}
cookies := append(client.Jar.Cookies(c.URL), &http.Cookie{
Name: httpmw.AuthCookie,
Value: c.SessionToken,
})
client.Jar.SetCookies(c.URL, cookies)

//nolint:bodyclose
conn, _, err := websocket.Dial(context.Background(), apiURL.String(), &websocket.DialOptions{
HTTPClient: c.HTTPClient,
})
if err != nil {
return nil, xerrors.Errorf("dial websocket: %w", err)
}

return conn, nil
}

// readBodyAsError reads the response as an httpapi.Message, and
// wraps it in a codersdk.Error type for easy marshaling.
func readBodyAsError(res *http.Response) error {
Expand Down
36 changes: 36 additions & 0 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

"github.com/google/uuid"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"

"github.com/coder/coder/coderd/database"
)
Expand Down Expand Up @@ -98,6 +100,40 @@ func (c *Client) WorkspaceBuildByName(ctx context.Context, workspace uuid.UUID,
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
}

type WorkspaceWatcher struct {
conn *websocket.Conn
}

func (w *WorkspaceWatcher) Read(ctx context.Context) (Workspace, error) {
var ws Workspace
err := wsjson.Read(ctx, w.conn, &ws)
if err != nil {
return ws, xerrors.Errorf("read workspace: %w")
}

return ws, nil
}

func (w *WorkspaceWatcher) Close() error {
err := w.conn.Close(websocket.StatusNormalClosure, "")
if err != nil {
return xerrors.Errorf("closing workspace watcher: %w", err)
}

return nil
}

func (c *Client) WorkspaceWatcher(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
conn, err := c.websocket(ctx, fmt.Sprintf("/api/v2/workspaces/%s/watch", id))
if err != nil {
return nil, err
}

return &WorkspaceWatcher{
conn: conn,
}, nil
}

// UpdateWorkspaceAutostartRequest is a request to update a workspace's autostart schedule.
type UpdateWorkspaceAutostartRequest struct {
Schedule string `json:"schedule"`
Expand Down
0