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-01-25 19:52:58 +00:00
|
|
|
|
2022-03-25 21:07:45 +00:00
|
|
|
"github.com/coder/coder/coderd/database"
|
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-12 15:01:28 +00:00
|
|
|
TemplateVersionID uuid.UUID `json:"template_version_id,omitempty"`
|
2022-04-06 17:42:40 +00:00
|
|
|
Transition database.WorkspaceTransition `json:"transition" validate:"oneof=create start stop delete,required"`
|
2022-05-12 15:01:28 +00:00
|
|
|
DryRun bool `json:"dry_run,omitempty"`
|
2022-05-02 22:51:58 +00:00
|
|
|
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-03-07 17:40:54 +00:00
|
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s", id), nil)
|
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-03-22 19:17:50 +00:00
|
|
|
func (c *Client) WorkspaceBuilds(ctx context.Context, workspace uuid.UUID) ([]WorkspaceBuild, error) {
|
2022-03-07 17:40:54 +00:00
|
|
|
res, err := c.request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaces/%s/builds", workspace), nil)
|
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-03-07 17:40:54 +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-03-07 17:40:54 +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
|
|
|
|
|
|
|
// 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())
|
|
|
|
res, err := c.request(ctx, http.MethodPut, path, req)
|
|
|
|
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())
|
|
|
|
res, err := c.request(ctx, http.MethodPut, path, req)
|
|
|
|
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
|
|
|
|
}
|