2022-01-25 19:52:58 +00:00
|
|
|
package codersdk
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-03-22 19:17:50 +00:00
|
|
|
"time"
|
2022-02-12 19:34:04 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-04-07 09:03:35 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-05-18 21:16:26 +00:00
|
|
|
"nhooyr.io/websocket"
|
|
|
|
"nhooyr.io/websocket/wsjson"
|
2022-01-25 19:52:58 +00:00
|
|
|
)
|
|
|
|
|
2022-04-25 21:11:03 +00:00
|
|
|
// Workspace is a deployment of a template. It references a specific
|
|
|
|
// version and can be updated.
|
2022-03-22 19:17:50 +00:00
|
|
|
type Workspace struct {
|
2022-04-07 09:03:35 +00:00
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
CreatedAt time.Time `json:"created_at"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
OwnerID uuid.UUID `json:"owner_id"`
|
2022-05-14 01:41:21 +00:00
|
|
|
OwnerName string `json:"owner_name"`
|
2022-04-07 09:03:35 +00:00
|
|
|
TemplateID uuid.UUID `json:"template_id"`
|
|
|
|
TemplateName string `json:"template_name"`
|
|
|
|
LatestBuild WorkspaceBuild `json:"latest_build"`
|
|
|
|
Outdated bool `json:"outdated"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
AutostartSchedule string `json:"autostart_schedule"`
|
|
|
|
AutostopSchedule string `json:"autostop_schedule"`
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWorkspaceBuildRequest provides options to update the latest workspace build.
|
|
|
|
type CreateWorkspaceBuildRequest struct {
|
2022-05-19 18:04:44 +00:00
|
|
|
TemplateVersionID uuid.UUID `json:"template_version_id,omitempty"`
|
|
|
|
Transition WorkspaceTransition `json:"transition" validate:"oneof=create start stop delete,required"`
|
|
|
|
DryRun bool `json:"dry_run,omitempty"`
|
|
|
|
ProvisionerState []byte `json:"state,omitempty"`
|
2022-03-22 19:17:50 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
// Workspace returns a single workspace.
|
2022-03-22 19:17:50 +00:00
|
|
|
func (c *Client) Workspace(ctx context.Context, id uuid.UUID) (Workspace, error) {
|
2022-05-19 14:29:10 +00:00
|
|
|
return c.getWorkspace(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletedWorkspace returns a single workspace that was deleted.
|
|
|
|
func (c *Client) DeletedWorkspace(ctx context.Context, id uuid.UUID) (Workspace, error) {
|
|
|
|
return c.getWorkspace(ctx, id, queryParam("deleted", "true"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) getWorkspace(ctx context.Context, id uuid.UUID, opts ...requestOption) (Workspace, error) {
|
|
|
|
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s", id), nil, opts...)
|
2022-01-25 19:52:58 +00:00
|
|
|
if err != nil {
|
2022-03-22 19:17:50 +00:00
|
|
|
return Workspace{}, err
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
2022-03-22 19:17:50 +00:00
|
|
|
return Workspace{}, readBodyAsError(res)
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
2022-03-22 19:17:50 +00:00
|
|
|
var workspace Workspace
|
2022-01-25 19:52:58 +00:00
|
|
|
return workspace, json.NewDecoder(res.Body).Decode(&workspace)
|
|
|
|
}
|
|
|
|
|
2022-05-18 16:33:33 +00:00
|
|
|
type WorkspaceBuildsRequest struct {
|
|
|
|
WorkspaceID uuid.UUID
|
|
|
|
Pagination
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) WorkspaceBuilds(ctx context.Context, req WorkspaceBuildsRequest) ([]WorkspaceBuild, error) {
|
|
|
|
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/builds", req.WorkspaceID),
|
|
|
|
nil, req.Pagination.asRequestOption())
|
2022-01-25 19:52:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, readBodyAsError(res)
|
|
|
|
}
|
2022-03-22 19:17:50 +00:00
|
|
|
var workspaceBuild []WorkspaceBuild
|
2022-03-07 17:40:54 +00:00
|
|
|
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
// CreateWorkspaceBuild queues a new build to occur for a workspace.
|
2022-03-22 19:17:50 +00:00
|
|
|
func (c *Client) CreateWorkspaceBuild(ctx context.Context, workspace uuid.UUID, request CreateWorkspaceBuildRequest) (WorkspaceBuild, error) {
|
2022-05-17 18:43:19 +00:00
|
|
|
res, err := c.Request(ctx, http.MethodPost, fmt.Sprintf("/api/v2/workspaces/%s/builds", workspace), request)
|
2022-01-25 19:52:58 +00:00
|
|
|
if err != nil {
|
2022-03-22 19:17:50 +00:00
|
|
|
return WorkspaceBuild{}, err
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusCreated {
|
2022-03-22 19:17:50 +00:00
|
|
|
return WorkspaceBuild{}, readBodyAsError(res)
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
2022-03-22 19:17:50 +00:00
|
|
|
var workspaceBuild WorkspaceBuild
|
2022-03-07 17:40:54 +00:00
|
|
|
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
|
2022-01-25 19:52:58 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 19:17:50 +00:00
|
|
|
func (c *Client) WorkspaceBuildByName(ctx context.Context, workspace uuid.UUID, name string) (WorkspaceBuild, error) {
|
2022-05-17 18:43:19 +00:00
|
|
|
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/builds/%s", workspace, name), nil)
|
2022-01-25 19:52:58 +00:00
|
|
|
if err != nil {
|
2022-03-22 19:17:50 +00:00
|
|
|
return WorkspaceBuild{}, err
|
2022-02-12 19:34:04 +00:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
2022-03-22 19:17:50 +00:00
|
|
|
return WorkspaceBuild{}, readBodyAsError(res)
|
2022-02-12 19:34:04 +00:00
|
|
|
}
|
2022-03-22 19:17:50 +00:00
|
|
|
var workspaceBuild WorkspaceBuild
|
2022-03-07 17:40:54 +00:00
|
|
|
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
|
2022-02-12 19:34:04 +00:00
|
|
|
}
|
2022-04-07 09:03:35 +00:00
|
|
|
|
2022-05-18 21:16:26 +00:00
|
|
|
func (c *Client) WatchWorkspace(ctx context.Context, id uuid.UUID) (<-chan Workspace, error) {
|
|
|
|
conn, err := c.dialWebsocket(ctx, fmt.Sprintf("/api/v2/workspaces/%s/watch", id))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
wc := make(chan Workspace, 256)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(wc)
|
|
|
|
defer conn.Close(websocket.StatusNormalClosure, "")
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
var ws Workspace
|
|
|
|
err := wsjson.Read(ctx, conn, &ws)
|
|
|
|
if err != nil {
|
|
|
|
conn.Close(websocket.StatusInternalError, "failed to read workspace")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wc <- ws
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return wc, nil
|
|
|
|
}
|
|
|
|
|
2022-04-07 09:03:35 +00:00
|
|
|
// UpdateWorkspaceAutostartRequest is a request to update a workspace's autostart schedule.
|
|
|
|
type UpdateWorkspaceAutostartRequest struct {
|
2022-04-14 00:35:47 +00:00
|
|
|
Schedule string `json:"schedule"`
|
2022-04-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWorkspaceAutostart sets the autostart schedule for workspace by id.
|
|
|
|
// If the provided schedule is empty, autostart is disabled for the workspace.
|
|
|
|
func (c *Client) UpdateWorkspaceAutostart(ctx context.Context, id uuid.UUID, req UpdateWorkspaceAutostartRequest) error {
|
|
|
|
path := fmt.Sprintf("/api/v2/workspaces/%s/autostart", id.String())
|
2022-05-17 18:43:19 +00:00
|
|
|
res, err := c.Request(ctx, http.MethodPut, path, req)
|
2022-04-07 09:03:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("update workspace autostart: %w", err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return readBodyAsError(res)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWorkspaceAutostopRequest is a request to update a workspace's autostop schedule.
|
|
|
|
type UpdateWorkspaceAutostopRequest struct {
|
2022-04-14 00:35:47 +00:00
|
|
|
Schedule string `json:"schedule"`
|
2022-04-07 09:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateWorkspaceAutostop sets the autostop schedule for workspace by id.
|
|
|
|
// If the provided schedule is empty, autostop is disabled for the workspace.
|
|
|
|
func (c *Client) UpdateWorkspaceAutostop(ctx context.Context, id uuid.UUID, req UpdateWorkspaceAutostopRequest) error {
|
|
|
|
path := fmt.Sprintf("/api/v2/workspaces/%s/autostop", id.String())
|
2022-05-17 18:43:19 +00:00
|
|
|
res, err := c.Request(ctx, http.MethodPut, path, req)
|
2022-04-07 09:03:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("update workspace autostop: %w", err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return readBodyAsError(res)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-18 15:09:07 +00:00
|
|
|
|
|
|
|
type WorkspaceFilter struct {
|
|
|
|
OrganizationID uuid.UUID
|
|
|
|
// Owner can be a user_id (uuid), "me", or a username
|
|
|
|
Owner string
|
|
|
|
}
|
|
|
|
|
|
|
|
// asRequestOption returns a function that can be used in (*Client).Request.
|
|
|
|
// It modifies the request query parameters.
|
|
|
|
func (f WorkspaceFilter) asRequestOption() requestOption {
|
|
|
|
return func(r *http.Request) {
|
|
|
|
q := r.URL.Query()
|
|
|
|
if f.OrganizationID != uuid.Nil {
|
|
|
|
q.Set("organization_id", f.OrganizationID.String())
|
|
|
|
}
|
|
|
|
if f.Owner != "" {
|
2022-05-19 16:08:55 +00:00
|
|
|
q.Set("owner", f.Owner)
|
2022-05-18 15:09:07 +00:00
|
|
|
}
|
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Workspaces returns all workspaces the authenticated user has access to.
|
|
|
|
func (c *Client) Workspaces(ctx context.Context, filter WorkspaceFilter) ([]Workspace, error) {
|
|
|
|
res, err := c.Request(ctx, http.MethodGet, "/api/v2/workspaces", nil, filter.asRequestOption())
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, readBodyAsError(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
var workspaces []Workspace
|
|
|
|
return workspaces, json.NewDecoder(res.Body).Decode(&workspaces)
|
|
|
|
}
|