mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: allow disabling autostart and custom autostop for template (#6933)
API only, frontend in upcoming PR.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// package schedule provides utilities for managing template and workspace
|
||||
// auto-start and auto-stop schedules. This includes utilities for parsing and
|
||||
// autostart and autostop schedules. This includes utilities for parsing and
|
||||
// deserializing cron-style expressions.
|
||||
package schedule
|
||||
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package schedule
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/coder/coder/coderd/database"
|
||||
)
|
||||
|
||||
type MockTemplateScheduleStore struct {
|
||||
GetFn func(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error)
|
||||
SetFn func(ctx context.Context, db database.Store, template database.Template, options TemplateScheduleOptions) (database.Template, error)
|
||||
}
|
||||
|
||||
var _ TemplateScheduleStore = MockTemplateScheduleStore{}
|
||||
|
||||
func (m MockTemplateScheduleStore) GetTemplateScheduleOptions(ctx context.Context, db database.Store, templateID uuid.UUID) (TemplateScheduleOptions, error) {
|
||||
if m.GetFn != nil {
|
||||
return m.GetFn(ctx, db, templateID)
|
||||
}
|
||||
|
||||
return NewAGPLTemplateScheduleStore().GetTemplateScheduleOptions(ctx, db, templateID)
|
||||
}
|
||||
|
||||
func (m MockTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.Context, db database.Store, template database.Template, options TemplateScheduleOptions) (database.Template, error) {
|
||||
if m.SetFn != nil {
|
||||
return m.SetFn(ctx, db, template, options)
|
||||
}
|
||||
|
||||
return NewAGPLTemplateScheduleStore().SetTemplateScheduleOptions(ctx, db, template, options)
|
||||
}
|
||||
@@ -10,8 +10,9 @@ import (
|
||||
)
|
||||
|
||||
type TemplateScheduleOptions struct {
|
||||
UserSchedulingEnabled bool `json:"user_scheduling_enabled"`
|
||||
DefaultTTL time.Duration `json:"default_ttl"`
|
||||
UserAutostartEnabled bool `json:"user_autostart_enabled"`
|
||||
UserAutostopEnabled bool `json:"user_autostop_enabled"`
|
||||
DefaultTTL time.Duration `json:"default_ttl"`
|
||||
// If MaxTTL is set, the workspace must be stopped before this time or it
|
||||
// will be stopped automatically.
|
||||
//
|
||||
@@ -41,8 +42,11 @@ func (*agplTemplateScheduleStore) GetTemplateScheduleOptions(ctx context.Context
|
||||
}
|
||||
|
||||
return TemplateScheduleOptions{
|
||||
UserSchedulingEnabled: true,
|
||||
DefaultTTL: time.Duration(tpl.DefaultTTL),
|
||||
// Disregard the values in the database, since user scheduling is an
|
||||
// enterprise feature.
|
||||
UserAutostartEnabled: true,
|
||||
UserAutostopEnabled: true,
|
||||
DefaultTTL: time.Duration(tpl.DefaultTTL),
|
||||
// Disregard the value in the database, since MaxTTL is an enterprise
|
||||
// feature.
|
||||
MaxTTL: 0,
|
||||
@@ -50,12 +54,19 @@ func (*agplTemplateScheduleStore) GetTemplateScheduleOptions(ctx context.Context
|
||||
}
|
||||
|
||||
func (*agplTemplateScheduleStore) SetTemplateScheduleOptions(ctx context.Context, db database.Store, tpl database.Template, opts TemplateScheduleOptions) (database.Template, error) {
|
||||
if int64(opts.DefaultTTL) == tpl.DefaultTTL {
|
||||
// Avoid updating the UpdatedAt timestamp if nothing will be changed.
|
||||
return tpl, nil
|
||||
}
|
||||
|
||||
return db.UpdateTemplateScheduleByID(ctx, database.UpdateTemplateScheduleByIDParams{
|
||||
ID: tpl.ID,
|
||||
UpdatedAt: database.Now(),
|
||||
DefaultTTL: int64(opts.DefaultTTL),
|
||||
// Don't allow changing it, but keep the value in the DB (to avoid
|
||||
// clearing settings if the license has an issue).
|
||||
MaxTTL: tpl.MaxTTL,
|
||||
AllowUserAutostart: tpl.AllowUserAutostart,
|
||||
AllowUserAutostop: tpl.AllowUserAutostop,
|
||||
MaxTTL: tpl.MaxTTL,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user