Thanks to visit codestin.com
Credit goes to github.com

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
add tests
  • Loading branch information
f0ssel committed May 18, 2022
commit dbaeb2cf596fe8f4a1d58f35010d9848e87b9eaf
10 changes: 8 additions & 2 deletions coderd/httpmw/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ type OAuth2Configs struct {
func ExtractAPIKey(db database.Store, oauth *OAuth2Configs) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
var cookieValue string
cookie, err := r.Cookie(AuthCookie)
if err != nil {
cookieValue = r.URL.Query().Get(AuthCookie)
} else {
cookieValue = cookie.Value
}
if cookieValue == "" {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Message: fmt.Sprintf("%q cookie must be provided", AuthCookie),
Message: fmt.Sprintf("%q cookie or query parameter must be provided", AuthCookie),
})
return
}
parts := strings.Split(cookie.Value, "-")
parts := strings.Split(cookieValue, "-")
// APIKeys are formatted: ID-SECRET
if len(parts) != 2 {
httpapi.Write(rw, http.StatusUnauthorized, httpapi.Response{
Expand Down
25 changes: 25 additions & 0 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,28 @@ func mustLocation(t *testing.T, location string) *time.Location {

return loc
}

func TestWorkspaceWatcher(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
coderdtest.NewProvisionerDaemon(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
w, err := client.Workspace(context.Background(), workspace.ID)
require.NoError(t, err)

ww, err := client.WatchWorkspace(context.Background(), w.ID)
require.NoError(t, err)
defer ww.Close()
for i := 0; i < 5; i++ {
_, err := ww.Read(context.Background())
require.NoError(t, err)
}
err = ww.Close()
require.NoError(t, err)
_, err = ww.Read(context.Background())
require.Error(t, err)
}
16 changes: 5 additions & 11 deletions codersdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ 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.
// websocket opens a websocket connection on that path provided.
// The caller is responsible for closing the websocket.Conn.
func (c *Client) websocket(ctx context.Context, path string) (*websocket.Conn, error) {
serverURL, err := c.URL.Parse(path)
if err != nil {
Expand All @@ -95,15 +95,9 @@ func (c *Client) websocket(ctx context.Context, path string) (*websocket.Conn, e
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)
q := apiURL.Query()
q.Add(httpmw.AuthCookie, c.SessionToken)
apiURL.RawQuery = q.Encode()

//nolint:bodyclose
conn, _, err := websocket.Dial(context.Background(), apiURL.String(), &websocket.DialOptions{
Expand Down
2 changes: 1 addition & 1 deletion codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (w *WorkspaceWatcher) Close() error {
return nil
}

func (c *Client) WorkspaceWatcher(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (*WorkspaceWatcher, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this return a channel instead? <-chan Workspace could be nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing that sounds nice

conn, err := c.websocket(ctx, fmt.Sprintf("/api/v2/workspaces/%s/watch", id))
if err != nil {
return nil, err
Expand Down