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

Skip to content

feat: Make template name editable #3538

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 7 commits into from
Aug 17, 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
3 changes: 3 additions & 0 deletions cli/templateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

func templateEdit() *cobra.Command {
var (
name string
description string
maxTTL time.Duration
minAutostartInterval time.Duration
Expand All @@ -38,6 +39,7 @@ func templateEdit() *cobra.Command {

// NOTE: coderd will ignore empty fields.
req := codersdk.UpdateTemplateMeta{
Name: name,
Description: description,
MaxTTLMillis: maxTTL.Milliseconds(),
MinAutostartIntervalMillis: minAutostartInterval.Milliseconds(),
Expand All @@ -52,6 +54,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().DurationVarP(&maxTTL, "max-ttl", "", 0, "Edit the template maximum time before shutdown")
cmd.Flags().DurationVarP(&minAutostartInterval, "min-autostart-interval", "", 0, "Edit the template minimum autostart interval")
Expand Down
5 changes: 5 additions & 0 deletions cli/templateedit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func TestTemplateEdit(t *testing.T) {
})

// Test the cli command.
name := "new-template-name"
desc := "lorem ipsum dolor sit amet et cetera"
maxTTL := 12 * time.Hour
minAutostartInterval := time.Minute
cmdArgs := []string{
"templates",
"edit",
template.Name,
"--name", name,
"--description", desc,
"--max-ttl", maxTTL.String(),
"--min-autostart-interval", minAutostartInterval.String(),
Expand All @@ -51,6 +53,7 @@ func TestTemplateEdit(t *testing.T) {
// Assert that the template metadata changed.
updated, err := client.Template(context.Background(), template.ID)
require.NoError(t, err)
assert.Equal(t, name, updated.Name)
assert.Equal(t, desc, updated.Description)
assert.Equal(t, maxTTL.Milliseconds(), updated.MaxTTLMillis)
assert.Equal(t, minAutostartInterval.Milliseconds(), updated.MinAutostartIntervalMillis)
Expand All @@ -73,6 +76,7 @@ func TestTemplateEdit(t *testing.T) {
"templates",
"edit",
template.Name,
"--name", template.Name,
"--description", template.Description,
"--max-ttl", (time.Duration(template.MaxTTLMillis) * time.Millisecond).String(),
"--min-autostart-interval", (time.Duration(template.MinAutostartIntervalMillis) * time.Millisecond).String(),
Expand All @@ -87,6 +91,7 @@ func TestTemplateEdit(t *testing.T) {
// Assert that the template metadata did not change.
updated, err := client.Template(context.Background(), template.ID)
require.NoError(t, err)
assert.Equal(t, template.Name, updated.Name)
assert.Equal(t, template.Description, updated.Description)
assert.Equal(t, template.MaxTTLMillis, updated.MaxTTLMillis)
assert.Equal(t, template.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis)
Expand Down
1 change: 1 addition & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,7 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
continue
}
tpl.UpdatedAt = database.Now()
tpl.Name = arg.Name
tpl.Description = arg.Description
tpl.MaxTtl = arg.MaxTtl
tpl.MinAutostartInterval = arg.MinAutostartInterval
Expand Down
5 changes: 4 additions & 1 deletion coderd/database/queries.sql.go

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

3 changes: 2 additions & 1 deletion coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ SET
updated_at = $2,
description = $3,
max_ttl = $4,
min_autostart_interval = $5
min_autostart_interval = $5,
name = $6
WHERE
id = $1
RETURNING
Expand Down
8 changes: 7 additions & 1 deletion coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,22 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
count = uint32(workspaceCounts[0].Count)
}

if req.Description == template.Description &&
if req.Name == template.Name &&
req.Description == template.Description &&
req.MaxTTLMillis == time.Duration(template.MaxTtl).Milliseconds() &&
req.MinAutostartIntervalMillis == time.Duration(template.MinAutostartInterval).Milliseconds() {
return nil
}

// Update template metadata -- empty fields are not overwritten.
name := req.Name
desc := req.Description
maxTTL := time.Duration(req.MaxTTLMillis) * time.Millisecond
minAutostartInterval := time.Duration(req.MinAutostartIntervalMillis) * time.Millisecond

if name == "" {
name = template.Name
}
if desc == "" {
desc = template.Description
}
Expand All @@ -433,6 +438,7 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
if err := s.UpdateTemplateMetaByID(r.Context(), database.UpdateTemplateMetaByIDParams{
ID: template.ID,
UpdatedAt: database.Now(),
Name: name,
Description: desc,
MaxTtl: int64(maxTTL),
MinAutostartInterval: int64(minAutostartInterval),
Expand Down
6 changes: 6 additions & 0 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ func TestPatchTemplateMeta(t *testing.T) {
ctr.MinAutostartIntervalMillis = ptr.Ref(time.Hour.Milliseconds())
})
req := codersdk.UpdateTemplateMeta{
Name: "new-template-name",
Description: "lorem ipsum dolor sit amet et cetera",
MaxTTLMillis: 12 * time.Hour.Milliseconds(),
MinAutostartIntervalMillis: time.Minute.Milliseconds(),
Expand All @@ -251,6 +252,7 @@ func TestPatchTemplateMeta(t *testing.T) {
updated, err := client.UpdateTemplateMeta(ctx, template.ID, req)
require.NoError(t, err)
assert.Greater(t, updated.UpdatedAt, template.UpdatedAt)
assert.Equal(t, req.Name, updated.Name)
assert.Equal(t, req.Description, updated.Description)
assert.Equal(t, req.MaxTTLMillis, updated.MaxTTLMillis)
assert.Equal(t, req.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis)
Expand All @@ -259,6 +261,7 @@ func TestPatchTemplateMeta(t *testing.T) {
updated, err = client.Template(ctx, template.ID)
require.NoError(t, err)
assert.Greater(t, updated.UpdatedAt, template.UpdatedAt)
assert.Equal(t, req.Name, updated.Name)
assert.Equal(t, req.Description, updated.Description)
assert.Equal(t, req.MaxTTLMillis, updated.MaxTTLMillis)
assert.Equal(t, req.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis)
Expand All @@ -280,6 +283,7 @@ func TestPatchTemplateMeta(t *testing.T) {
defer cancel()

req := codersdk.UpdateTemplateMeta{
Name: template.Name,
Description: template.Description,
MaxTTLMillis: template.MaxTTLMillis,
MinAutostartIntervalMillis: template.MinAutostartIntervalMillis,
Expand All @@ -289,6 +293,7 @@ func TestPatchTemplateMeta(t *testing.T) {
updated, err := client.Template(ctx, template.ID)
require.NoError(t, err)
assert.Equal(t, updated.UpdatedAt, template.UpdatedAt)
assert.Equal(t, template.Name, updated.Name)
assert.Equal(t, template.Description, updated.Description)
assert.Equal(t, template.MaxTTLMillis, updated.MaxTTLMillis)
assert.Equal(t, template.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis)
Expand Down Expand Up @@ -324,6 +329,7 @@ func TestPatchTemplateMeta(t *testing.T) {
updated, err := client.Template(ctx, template.ID)
require.NoError(t, err)
assert.WithinDuration(t, template.UpdatedAt, updated.UpdatedAt, time.Minute)
assert.Equal(t, template.Name, updated.Name)
assert.Equal(t, template.Description, updated.Description)
assert.Equal(t, template.MaxTTLMillis, updated.MaxTTLMillis)
assert.Equal(t, template.MinAutostartIntervalMillis, updated.MinAutostartIntervalMillis)
Expand Down
1 change: 1 addition & 0 deletions codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type UpdateActiveTemplateVersion struct {
}

type UpdateTemplateMeta struct {
Name string `json:"name,omitempty" validate:"omitempty,username"`
Description string `json:"description,omitempty"`
MaxTTLMillis int64 `json:"max_ttl_ms,omitempty"`
MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ export interface UpdateRoles {

// From codersdk/templates.go
export interface UpdateTemplateMeta {
readonly name?: string
readonly description?: string
readonly max_ttl_ms?: number
readonly min_autostart_interval_ms?: number
Expand Down