Skip to content

Commit

Permalink
feat: change template max_ttl to default_ttl (#4843)
Browse files Browse the repository at this point in the history
  • Loading branch information
f0ssel committed Nov 9, 2022
1 parent ffc24dc commit d277e28
Show file tree
Hide file tree
Showing 26 changed files with 317 additions and 517 deletions.
19 changes: 8 additions & 11 deletions cli/templatecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (

func templateCreate() *cobra.Command {
var (
directory string
provisioner string
parameterFile string
maxTTL time.Duration
minAutostartInterval time.Duration
directory string
provisioner string
parameterFile string
defaultTTL time.Duration
)
cmd := &cobra.Command{
Use: "create [name]",
Expand Down Expand Up @@ -108,10 +107,9 @@ func templateCreate() *cobra.Command {
}

createReq := codersdk.CreateTemplateRequest{
Name: templateName,
VersionID: job.ID,
MaxTTLMillis: ptr.Ref(maxTTL.Milliseconds()),
MinAutostartIntervalMillis: ptr.Ref(minAutostartInterval.Milliseconds()),
Name: templateName,
VersionID: job.ID,
DefaultTTLMillis: ptr.Ref(defaultTTL.Milliseconds()),
}

_, err = client.CreateTemplate(cmd.Context(), organization.ID, createReq)
Expand All @@ -133,8 +131,7 @@ func templateCreate() *cobra.Command {
cmd.Flags().StringVarP(&directory, "directory", "d", currentDirectory, "Specify the directory to create from")
cmd.Flags().StringVarP(&provisioner, "test.provisioner", "", "terraform", "Customize the provisioner backend")
cmd.Flags().StringVarP(&parameterFile, "parameter-file", "", "", "Specify a file path with parameter values.")
cmd.Flags().DurationVarP(&maxTTL, "max-ttl", "", 24*time.Hour, "Specify a maximum TTL for workspaces created from this template.")
cmd.Flags().DurationVarP(&minAutostartInterval, "min-autostart-interval", "", time.Hour, "Specify a minimum autostart interval for workspaces created from this template.")
cmd.Flags().DurationVarP(&defaultTTL, "default-ttl", "", 24*time.Hour, "Specify a default TTL for workspaces created from this template.")
// This is for testing!
err := cmd.Flags().MarkHidden("test.provisioner")
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cli/templatecreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func TestTemplateCreate(t *testing.T) {
"my-template",
"--directory", source,
"--test.provisioner", string(database.ProvisionerTypeEcho),
"--max-ttl", "24h",
"--min-autostart-interval", "2h",
"--default-ttl", "24h",
}
cmd, root := clitest.New(t, args...)
clitest.SetupConfig(t, client, root)
Expand Down
21 changes: 9 additions & 12 deletions cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (

func templateEdit() *cobra.Command {
var (
name string
description string
icon string
maxTTL time.Duration
minAutostartInterval time.Duration
name string
description string
icon string
defaultTTL time.Duration
)

cmd := &cobra.Command{
Expand All @@ -40,11 +39,10 @@ func templateEdit() *cobra.Command {

// NOTE: coderd will ignore empty fields.
req := codersdk.UpdateTemplateMeta{
Name: name,
Description: description,
Icon: icon,
MaxTTLMillis: maxTTL.Milliseconds(),
MinAutostartIntervalMillis: minAutostartInterval.Milliseconds(),
Name: name,
Description: description,
Icon: icon,
DefaultTTLMillis: defaultTTL.Milliseconds(),
}

_, err = client.UpdateTemplateMeta(cmd.Context(), template.ID, req)
Expand All @@ -59,8 +57,7 @@ func templateEdit() *cobra.Command {
cmd.Flags().StringVarP(&name, "name", "", "", "Edit the template name")
cmd.Flags().StringVarP(&description, "description", "", "", "Edit the template description")
cmd.Flags().StringVarP(&icon, "icon", "", "", "Edit the template icon path")
cmd.Flags().DurationVarP(&maxTTL, "max-ttl", "", 0, "Edit the template maximum time before shutdown - workspaces created from this template cannot stay running longer than this.")
cmd.Flags().DurationVarP(&minAutostartInterval, "min-autostart-interval", "", 0, "Edit the template minimum autostart interval - workspaces created from this template must wait at least this long between autostarts.")
cmd.Flags().DurationVarP(&defaultTTL, "default-ttl", "", 0, "Edit the template default time before shutdown - workspaces created from this template to this value.")
cliui.AllowSkipPrompt(cmd)

return cmd
Expand Down
21 changes: 7 additions & 14 deletions cli/templateedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,22 @@ func TestTemplateEdit(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.Description = "original description"
ctr.Icon = "/icons/default-icon.png"
ctr.MaxTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds())
ctr.MinAutostartIntervalMillis = ptr.Ref(time.Hour.Milliseconds())
ctr.DefaultTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds())
})

// Test the cli command.
name := "new-template-name"
desc := "lorem ipsum dolor sit amet et cetera"
icon := "/icons/new-icon.png"
maxTTL := 12 * time.Hour
minAutostartInterval := time.Minute
defaultTTL := 12 * time.Hour
cmdArgs := []string{
"templates",
"edit",
template.Name,
"--name", name,
"--description", desc,
"--icon", icon,
"--max-ttl", maxTTL.String(),
"--min-autostart-interval", minAutostartInterval.String(),
"--default-ttl", defaultTTL.String(),
}
cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)
Expand All @@ -59,8 +56,7 @@ func TestTemplateEdit(t *testing.T) {
assert.Equal(t, name, updated.Name)
assert.Equal(t, desc, updated.Description)
assert.Equal(t, icon, updated.Icon)
assert.Equal(t, maxTTL.Milliseconds(), updated.MaxTTLMillis)
assert.Equal(t, minAutostartInterval.Milliseconds(), updated.MinAutostartIntervalMillis)
assert.Equal(t, defaultTTL.Milliseconds(), updated.DefaultTTLMillis)
})

t.Run("NotModified", func(t *testing.T) {
Expand All @@ -72,8 +68,7 @@ func TestTemplateEdit(t *testing.T) {
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID, func(ctr *codersdk.CreateTemplateRequest) {
ctr.Description = "original description"
ctr.Icon = "/icons/default-icon.png"
ctr.MaxTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds())
ctr.MinAutostartIntervalMillis = ptr.Ref(time.Hour.Milliseconds())
ctr.DefaultTTLMillis = ptr.Ref(24 * time.Hour.Milliseconds())
})

// Test the cli command.
Expand All @@ -84,8 +79,7 @@ func TestTemplateEdit(t *testing.T) {
"--name", template.Name,
"--description", template.Description,
"--icon", template.Icon,
"--max-ttl", (time.Duration(template.MaxTTLMillis) * time.Millisecond).String(),
"--min-autostart-interval", (time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond).String(),
"--default-ttl", (time.Duration(template.DefaultTTLMillis) * time.Millisecond).String(),
}
cmd, root := clitest.New(t, cmdArgs...)
clitest.SetupConfig(t, client, root)
Expand All @@ -100,7 +94,6 @@ func TestTemplateEdit(t *testing.T) {
assert.Equal(t, template.Name, updated.Name)
assert.Equal(t, template.Description, updated.Description)
assert.Equal(t, template.Icon, updated.Icon)
assert.Equal(t, template.MaxTTLMillis, updated.MaxTTLMillis)
assert.Equal(t, template.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis)
assert.Equal(t, template.DefaultTTLMillis, updated.DefaultTTLMillis)
})
}
34 changes: 16 additions & 18 deletions cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ func templates() *cobra.Command {
}

type templateTableRow struct {
Name string `table:"name"`
CreatedAt string `table:"created at"`
LastUpdated string `table:"last updated"`
OrganizationID uuid.UUID `table:"organization id"`
Provisioner codersdk.ProvisionerType `table:"provisioner"`
ActiveVersionID uuid.UUID `table:"active version id"`
UsedBy string `table:"used by"`
MaxTTL time.Duration `table:"max ttl"`
MinAutostartInterval time.Duration `table:"min autostart"`
Name string `table:"name"`
CreatedAt string `table:"created at"`
LastUpdated string `table:"last updated"`
OrganizationID uuid.UUID `table:"organization id"`
Provisioner codersdk.ProvisionerType `table:"provisioner"`
ActiveVersionID uuid.UUID `table:"active version id"`
UsedBy string `table:"used by"`
DefaultTTL time.Duration `table:"default ttl"`
}

// displayTemplates will return a table displaying all templates passed in.
Expand All @@ -68,15 +67,14 @@ func displayTemplates(filterColumns []string, templates ...codersdk.Template) (s
rows := make([]templateTableRow, len(templates))
for i, template := range templates {
rows[i] = templateTableRow{
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: cliui.Styles.Fuchsia.Render(formatActiveDevelopers(template.ActiveUserCount)),
MaxTTL: (time.Duration(template.MaxTTLMillis) * time.Millisecond),
MinAutostartInterval: (time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond),
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: cliui.Styles.Fuchsia.Render(formatActiveDevelopers(template.ActiveUserCount)),
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
}
}

Expand Down
32 changes: 13 additions & 19 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,8 +1455,7 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
tpl.Name = arg.Name
tpl.Description = arg.Description
tpl.Icon = arg.Icon
tpl.MaxTtl = arg.MaxTtl
tpl.MinAutostartInterval = arg.MinAutostartInterval
tpl.DefaultTtl = arg.DefaultTtl
q.templates[idx] = tpl
return tpl, nil
}
Expand Down Expand Up @@ -2227,25 +2226,20 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
q.mutex.Lock()
defer q.mutex.Unlock()

if arg.MinAutostartInterval == 0 {
arg.MinAutostartInterval = int64(time.Hour)
}

//nolint:gosimple
template := database.Template{
ID: arg.ID,
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
OrganizationID: arg.OrganizationID,
Name: arg.Name,
Provisioner: arg.Provisioner,
ActiveVersionID: arg.ActiveVersionID,
Description: arg.Description,
MaxTtl: arg.MaxTtl,
MinAutostartInterval: arg.MinAutostartInterval,
CreatedBy: arg.CreatedBy,
UserACL: arg.UserACL,
GroupACL: arg.GroupACL,
ID: arg.ID,
CreatedAt: arg.CreatedAt,
UpdatedAt: arg.UpdatedAt,
OrganizationID: arg.OrganizationID,
Name: arg.Name,
Provisioner: arg.Provisioner,
ActiveVersionID: arg.ActiveVersionID,
Description: arg.Description,
DefaultTtl: arg.DefaultTtl,
CreatedBy: arg.CreatedBy,
UserACL: arg.UserACL,
GroupACL: arg.GroupACL,
}
q.templates = append(q.templates, template)
return template, nil
Expand Down
5 changes: 3 additions & 2 deletions 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,5 @@
-- add "slug" min_autostart_interval to "templates" table
ALTER TABLE "templates" ADD COLUMN "min_autostart_interval" int DEFAULT 0;

-- rename "default_ttl" to "max_ttl" on "templates" table
ALTER TABLE "templates" RENAME COLUMN "default_ttl" TO "max_ttl";
6 changes: 6 additions & 0 deletions coderd/database/migrations/000073_remove_min_autostart.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- drop "min_autostart_interval" column from "templates" table
ALTER TABLE "templates" DROP COLUMN "min_autostart_interval";

-- rename "max_ttl" to "default_ttl" on "templates" table
ALTER TABLE "templates" RENAME COLUMN "max_ttl" TO "default_ttl";
COMMENT ON COLUMN templates.default_ttl IS 'The default duration for auto-stop for workspaces created from this template.';
30 changes: 15 additions & 15 deletions coderd/database/models.go

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

0 comments on commit d277e28

Please sign in to comment.