Skip to content

coderd: autostart: codersdk, http api, database plumbing #879

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 16 commits into from
Apr 7, 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
feat: implement update workspace autostart
  • Loading branch information
johnstcn committed Apr 6, 2022
commit f6895b7b7ffd2c58f8f0d758cbe8dee562f4fe9f
3 changes: 3 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ func New(options *Options) (http.Handler, func()) {
r.Post("/", api.postWorkspaceBuilds)
r.Get("/{workspacebuildname}", api.workspaceBuildByName)
})
r.Route("/autostart", func(r chi.Router) {
r.Put("/", api.putWorkspaceAutostart)
})
})
r.Route("/workspacebuilds/{workspacebuild}", func(r chi.Router) {
r.Use(
Expand Down
26 changes: 26 additions & 0 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,32 @@ func (api *api) workspaceBuildByName(rw http.ResponseWriter, r *http.Request) {
render.JSON(rw, r, convertWorkspaceBuild(workspaceBuild, convertProvisionerJob(job)))
}

func (api *api) putWorkspaceAutostart(rw http.ResponseWriter, r *http.Request) {
var spec codersdk.UpdateWorkspaceAutostartRequest
if !httpapi.Read(rw, r, &spec) {
return
}

workspace := httpmw.WorkspaceParam(r)
err := api.Database.UpdateWorkspaceAutostart(r.Context(), database.UpdateWorkspaceAutostartParams{
ID: workspace.ID,
AutostartSchedule: sql.NullString{
String: spec.Schedule,
Valid: true,
},
})
if err != nil {
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{
Message: fmt.Sprintf("update workspace autostart schedule: %s", err),
})
return
}

return
}

// TODO(cian): api.updateWorkspaceAutostop

func convertWorkspace(workspace database.Workspace, workspaceBuild codersdk.WorkspaceBuild, template database.Template) codersdk.Workspace {
return codersdk.Workspace{
ID: workspace.ID,
Expand Down
43 changes: 43 additions & 0 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"net/http"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/coder/coder/coderd/autostart/schedule"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/codersdk"
Expand Down Expand Up @@ -182,3 +184,44 @@ func TestWorkspaceBuildByName(t *testing.T) {
require.NoError(t, err)
})
}

func TestWorkspaceUpdateAutostart(t *testing.T) {
// fri -> monday
// TODO(cian): mon -> tue
// TODO(cian): CST -> CDT
// TODO(cian): CDT -> CST
t.Parallel()
var (
ctx = context.Background()
client = coderdtest.New(t, nil)
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateProjectVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitProjectVersionJob(t, client, version.ID)
project = coderdtest.CreateProject(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, codersdk.Me, project.ID)
)

require.Empty(t, workspace.AutostartSchedule, "expected newly-minted workspace to have no autostart schedule")

schedSpec := "CRON_TZ=Europe/Dublin 30 9 1-5"
err := client.UpdateWorkspaceAutostart(ctx, workspace.ID, codersdk.UpdateWorkspaceAutostartRequest{
Schedule: schedSpec,
})
require.NoError(t, err, "expected no error")

updated, err := client.Workspace(ctx, workspace.ID)
require.NoError(t, err, "fetch updated workspace")

require.Equal(t, schedSpec, updated.AutostartSchedule, "expected autostart schedule to equal")

sched, err := schedule.Weekly(updated.AutostartSchedule)
require.NoError(t, err, "parse returned schedule")

dublinLoc, err := time.LoadLocation("Europe/Dublin")
require.NoError(t, err, "failed to load timezone location")

timeAt := time.Date(2022, 5, 6, 9, 31, 0, 0, dublinLoc)
expectedNext := time.Date(2022, 5, 9, 9, 30, 0, 0, dublinLoc)
require.Equal(t, expectedNext, sched.Next(timeAt), "unexpected next scheduled autostart time")
}
21 changes: 21 additions & 0 deletions codersdk/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database"
)
Expand Down Expand Up @@ -88,3 +89,23 @@ func (c *Client) WorkspaceBuildByName(ctx context.Context, workspace uuid.UUID,
var workspaceBuild WorkspaceBuild
return workspaceBuild, json.NewDecoder(res.Body).Decode(&workspaceBuild)
}

type UpdateWorkspaceAutostartRequest struct {
Schedule string
}

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)
}
// TODO(cian): should we return the updated schedule?
return nil
}

// TODO(cian): client.UpdateWorkspaceAutostop