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

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 all commits
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
6 changes: 6 additions & 0 deletions coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ 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("/autostop", func(r chi.Router) {
r.Put("/", api.putWorkspaceAutostop)
})
})
r.Route("/workspacebuilds/{workspacebuild}", func(r chi.Router) {
r.Use(
Expand Down
52 changes: 42 additions & 10 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func New() database.Store {
provisionerDaemons: make([]database.ProvisionerDaemon, 0),
provisionerJobs: make([]database.ProvisionerJob, 0),
provisionerJobLog: make([]database.ProvisionerJobLog, 0),
workspace: make([]database.Workspace, 0),
workspaces: make([]database.Workspace, 0),
provisionerJobResource: make([]database.WorkspaceResource, 0),
workspaceBuild: make([]database.WorkspaceBuild, 0),
provisionerJobAgent: make([]database.WorkspaceAgent, 0),
Expand Down Expand Up @@ -56,7 +56,7 @@ type fakeQuerier struct {
provisionerJobAgent []database.WorkspaceAgent
provisionerJobResource []database.WorkspaceResource
provisionerJobLog []database.ProvisionerJobLog
workspace []database.Workspace
workspaces []database.Workspace
workspaceBuild []database.WorkspaceBuild
GitSSHKey []database.GitSSHKey
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func (q *fakeQuerier) GetWorkspacesByTemplateID(_ context.Context, arg database.
defer q.mutex.RUnlock()

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

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

for _, workspace := range q.workspace {
for _, workspace := range q.workspaces {
if workspace.OwnerID != arg.OwnerID {
continue
}
Expand All @@ -222,7 +222,7 @@ func (q *fakeQuerier) GetWorkspaceOwnerCountsByTemplateIDs(_ context.Context, te
counts := map[uuid.UUID]map[uuid.UUID]struct{}{}
for _, templateID := range templateIDs {
found := false
for _, workspace := range q.workspace {
for _, workspace := range q.workspaces {
if workspace.TemplateID != templateID {
continue
}
Expand Down Expand Up @@ -350,7 +350,7 @@ func (q *fakeQuerier) GetWorkspacesByUserID(_ context.Context, req database.GetW
defer q.mutex.RUnlock()

workspaces := make([]database.Workspace, 0)
for _, workspace := range q.workspace {
for _, workspace := range q.workspaces {
if workspace.OwnerID != req.OwnerID {
continue
}
Expand Down Expand Up @@ -1040,7 +1040,7 @@ func (q *fakeQuerier) InsertWorkspace(_ context.Context, arg database.InsertWork
TemplateID: arg.TemplateID,
Name: arg.Name,
}
q.workspace = append(q.workspace, workspace)
q.workspaces = append(q.workspaces, workspace)
return workspace, nil
}

Expand Down Expand Up @@ -1210,6 +1210,38 @@ func (q *fakeQuerier) UpdateProvisionerJobWithCompleteByID(_ context.Context, ar
return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateWorkspaceAutostart(_ context.Context, arg database.UpdateWorkspaceAutostartParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, workspace := range q.workspaces {
if workspace.ID.String() != arg.ID.String() {
continue
}
workspace.AutostartSchedule = arg.AutostartSchedule
q.workspaces[index] = workspace
return nil
}

return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateWorkspaceAutostop(_ context.Context, arg database.UpdateWorkspaceAutostopParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()

for index, workspace := range q.workspaces {
if workspace.ID.String() != arg.ID.String() {
continue
}
workspace.AutostopSchedule = arg.AutostopSchedule
q.workspaces[index] = workspace
return nil
}

return sql.ErrNoRows
}

func (q *fakeQuerier) UpdateWorkspaceBuildByID(_ context.Context, arg database.UpdateWorkspaceBuildByIDParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()
Expand All @@ -1231,12 +1263,12 @@ func (q *fakeQuerier) UpdateWorkspaceDeletedByID(_ context.Context, arg database
q.mutex.Lock()
defer q.mutex.Unlock()

for index, workspace := range q.workspace {
for index, workspace := range q.workspaces {
if workspace.ID.String() != arg.ID.String() {
continue
}
workspace.Deleted = arg.Deleted
q.workspace[index] = workspace
q.workspaces[index] = workspace
return nil
}
return sql.ErrNoRows
Expand Down
4 changes: 3 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE ONLY workspaces
DROP COLUMN IF EXISTS autostart_schedule,
DROP COLUMN IF EXISTS autostop_schedule;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE ONLY workspaces
ADD COLUMN IF NOT EXISTS autostart_schedule text DEFAULT NULL,
ADD COLUMN IF NOT EXISTS autostop_schedule text DEFAULT NULL;
16 changes: 9 additions & 7 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 53 additions & 5 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,19 @@ SET
deleted = $2
WHERE
id = $1;

-- name: UpdateWorkspaceAutostart :exec
UPDATE
workspaces
SET
autostart_schedule = $2
WHERE
id = $1;

-- name: UpdateWorkspaceAutostop :exec
UPDATE
workspaces
SET
autostop_schedule = $2
WHERE
id = $1;
Loading