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

Skip to content

Commit 23f9891

Browse files
authored
coderd: autostart: codersdk, http api, database plumbing (#879)
* feat: add columns autostart_schedule, autostop_schedule to database schema * feat: database: add UpdateWorkspaceAutostart and UpdateWorkspaceAutostop methods * feat: add AutostartSchedule/AutostopSchedule to api workspace struct * feat: codersdk: implement update workspace autostart and autostop methods * chore: add unit tests for workspace autostarat and autostop methods
1 parent c1ff537 commit 23f9891

File tree

12 files changed

+535
-41
lines changed

12 files changed

+535
-41
lines changed

coderd/coderd.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ func New(options *Options) (http.Handler, func()) {
184184
r.Post("/", api.postWorkspaceBuilds)
185185
r.Get("/{workspacebuildname}", api.workspaceBuildByName)
186186
})
187+
r.Route("/autostart", func(r chi.Router) {
188+
r.Put("/", api.putWorkspaceAutostart)
189+
})
190+
r.Route("/autostop", func(r chi.Router) {
191+
r.Put("/", api.putWorkspaceAutostop)
192+
})
187193
})
188194
r.Route("/workspacebuilds/{workspacebuild}", func(r chi.Router) {
189195
r.Use(

coderd/database/databasefake/databasefake.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func New() database.Store {
2727
provisionerDaemons: make([]database.ProvisionerDaemon, 0),
2828
provisionerJobs: make([]database.ProvisionerJob, 0),
2929
provisionerJobLog: make([]database.ProvisionerJobLog, 0),
30-
workspace: make([]database.Workspace, 0),
30+
workspaces: make([]database.Workspace, 0),
3131
provisionerJobResource: make([]database.WorkspaceResource, 0),
3232
workspaceBuild: make([]database.WorkspaceBuild, 0),
3333
provisionerJobAgent: make([]database.WorkspaceAgent, 0),
@@ -56,7 +56,7 @@ type fakeQuerier struct {
5656
provisionerJobAgent []database.WorkspaceAgent
5757
provisionerJobResource []database.WorkspaceResource
5858
provisionerJobLog []database.ProvisionerJobLog
59-
workspace []database.Workspace
59+
workspaces []database.Workspace
6060
workspaceBuild []database.WorkspaceBuild
6161
GitSSHKey []database.GitSSHKey
6262
}
@@ -169,7 +169,7 @@ func (q *fakeQuerier) GetWorkspacesByTemplateID(_ context.Context, arg database.
169169
defer q.mutex.RUnlock()
170170

171171
workspaces := make([]database.Workspace, 0)
172-
for _, workspace := range q.workspace {
172+
for _, workspace := range q.workspaces {
173173
if workspace.TemplateID.String() != arg.TemplateID.String() {
174174
continue
175175
}
@@ -188,7 +188,7 @@ func (q *fakeQuerier) GetWorkspaceByID(_ context.Context, id uuid.UUID) (databas
188188
q.mutex.RLock()
189189
defer q.mutex.RUnlock()
190190

191-
for _, workspace := range q.workspace {
191+
for _, workspace := range q.workspaces {
192192
if workspace.ID.String() == id.String() {
193193
return workspace, nil
194194
}
@@ -200,7 +200,7 @@ func (q *fakeQuerier) GetWorkspaceByUserIDAndName(_ context.Context, arg databas
200200
q.mutex.RLock()
201201
defer q.mutex.RUnlock()
202202

203-
for _, workspace := range q.workspace {
203+
for _, workspace := range q.workspaces {
204204
if workspace.OwnerID != arg.OwnerID {
205205
continue
206206
}
@@ -222,7 +222,7 @@ func (q *fakeQuerier) GetWorkspaceOwnerCountsByTemplateIDs(_ context.Context, te
222222
counts := map[uuid.UUID]map[uuid.UUID]struct{}{}
223223
for _, templateID := range templateIDs {
224224
found := false
225-
for _, workspace := range q.workspace {
225+
for _, workspace := range q.workspaces {
226226
if workspace.TemplateID != templateID {
227227
continue
228228
}
@@ -350,7 +350,7 @@ func (q *fakeQuerier) GetWorkspacesByUserID(_ context.Context, req database.GetW
350350
defer q.mutex.RUnlock()
351351

352352
workspaces := make([]database.Workspace, 0)
353-
for _, workspace := range q.workspace {
353+
for _, workspace := range q.workspaces {
354354
if workspace.OwnerID != req.OwnerID {
355355
continue
356356
}
@@ -1040,7 +1040,7 @@ func (q *fakeQuerier) InsertWorkspace(_ context.Context, arg database.InsertWork
10401040
TemplateID: arg.TemplateID,
10411041
Name: arg.Name,
10421042
}
1043-
q.workspace = append(q.workspace, workspace)
1043+
q.workspaces = append(q.workspaces, workspace)
10441044
return workspace, nil
10451045
}
10461046

@@ -1210,6 +1210,38 @@ func (q *fakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
12101210
return sql.ErrNoRows
12111211
}
12121212

1213+
func (q *fakeQuerier) UpdateWorkspaceAutostart(_ context.Context, arg database.UpdateWorkspaceAutostartParams) error {
1214+
q.mutex.Lock()
1215+
defer q.mutex.Unlock()
1216+
1217+
for index, workspace := range q.workspaces {
1218+
if workspace.ID.String() != arg.ID.String() {
1219+
continue
1220+
}
1221+
workspace.AutostartSchedule = arg.AutostartSchedule
1222+
q.workspaces[index] = workspace
1223+
return nil
1224+
}
1225+
1226+
return sql.ErrNoRows
1227+
}
1228+
1229+
func (q *fakeQuerier) UpdateWorkspaceAutostop(_ context.Context, arg database.UpdateWorkspaceAutostopParams) error {
1230+
q.mutex.Lock()
1231+
defer q.mutex.Unlock()
1232+
1233+
for index, workspace := range q.workspaces {
1234+
if workspace.ID.String() != arg.ID.String() {
1235+
continue
1236+
}
1237+
workspace.AutostopSchedule = arg.AutostopSchedule
1238+
q.workspaces[index] = workspace
1239+
return nil
1240+
}
1241+
1242+
return sql.ErrNoRows
1243+
}
1244+
12131245
func (q *fakeQuerier) UpdateWorkspaceBuildByID(_ context.Context, arg database.UpdateWorkspaceBuildByIDParams) error {
12141246
q.mutex.Lock()
12151247
defer q.mutex.Unlock()
@@ -1231,12 +1263,12 @@ func (q *fakeQuerier) UpdateWorkspaceDeletedByID(_ context.Context, arg database
12311263
q.mutex.Lock()
12321264
defer q.mutex.Unlock()
12331265

1234-
for index, workspace := range q.workspace {
1266+
for index, workspace := range q.workspaces {
12351267
if workspace.ID.String() != arg.ID.String() {
12361268
continue
12371269
}
12381270
workspace.Deleted = arg.Deleted
1239-
q.workspace[index] = workspace
1271+
q.workspaces[index] = workspace
12401272
return nil
12411273
}
12421274
return sql.ErrNoRows

coderd/database/dump.sql

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE ONLY workspaces
2+
DROP COLUMN IF EXISTS autostart_schedule,
3+
DROP COLUMN IF EXISTS autostop_schedule;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ALTER TABLE ONLY workspaces
2+
ADD COLUMN IF NOT EXISTS autostart_schedule text DEFAULT NULL,
3+
ADD COLUMN IF NOT EXISTS autostop_schedule text DEFAULT NULL;

coderd/database/models.go

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 53 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaces.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,19 @@ SET
6868
deleted = $2
6969
WHERE
7070
id = $1;
71+
72+
-- name: UpdateWorkspaceAutostart :exec
73+
UPDATE
74+
workspaces
75+
SET
76+
autostart_schedule = $2
77+
WHERE
78+
id = $1;
79+
80+
-- name: UpdateWorkspaceAutostop :exec
81+
UPDATE
82+
workspaces
83+
SET
84+
autostop_schedule = $2
85+
WHERE
86+
id = $1;

0 commit comments

Comments
 (0)