Skip to content

fix: don't override 0 ttl with template default #5151

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 1 commit into from
Nov 23, 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
11 changes: 2 additions & 9 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,15 +670,8 @@ func (api *API) putWorkspaceTTL(rw http.ResponseWriter, r *http.Request) {
var dbTTL sql.NullInt64

err := api.Database.InTx(func(s database.Store) error {
template, err := s.GetTemplateByID(ctx, workspace.TemplateID)
if err != nil {
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
Message: "Error fetching workspace template!",
})
return xerrors.Errorf("fetch workspace template: %w", err)
}

dbTTL, err = validWorkspaceTTLMillis(req.TTLMillis, template.DefaultTTL)
// don't override 0 ttl with template default here because it indicates disabled auto-stop
dbTTL, err := validWorkspaceTTLMillis(req.TTLMillis, 0)
if err != nil {
return codersdk.ValidationError{Field: "ttl_ms", Detail: err.Error()}
}
Expand Down
8 changes: 7 additions & 1 deletion coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestPostWorkspacesByOrganization(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.DefaultTTLMillis = ptr.Ref(int64(0))
})
// Given: the template has no max TTL set
// Given: the template has no default TTL set
require.Zero(t, template.DefaultTTLMillis)
coderdtest.AwaitTemplateVersionJob(t, client, version.ID)

Expand Down Expand Up @@ -1175,11 +1175,17 @@ func TestWorkspaceUpdateTTL(t *testing.T) {
name: "disable ttl",
ttlMillis: nil,
expectedError: "",
modifyTemplate: func(ctr *codersdk.CreateTemplateRequest) {
ctr.DefaultTTLMillis = ptr.Ref((8 * time.Hour).Milliseconds())
},
},
{
name: "update ttl",
ttlMillis: ptr.Ref(12 * time.Hour.Milliseconds()),
expectedError: "",
modifyTemplate: func(ctr *codersdk.CreateTemplateRequest) {
ctr.DefaultTTLMillis = ptr.Ref((8 * time.Hour).Milliseconds())
},
},
{
name: "below minimum ttl",
Expand Down