diff --git a/cli/templateedit.go b/cli/templateedit.go index 083df3fb7dd80..0a84f04f69283 100644 --- a/cli/templateedit.go +++ b/cli/templateedit.go @@ -13,6 +13,7 @@ import ( func templateEdit() *cobra.Command { var ( + name string description string maxTTL time.Duration minAutostartInterval time.Duration @@ -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(), @@ -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") diff --git a/cli/templateedit_test.go b/cli/templateedit_test.go index 1bc6a3b89da69..6acaabcbbf64d 100644 --- a/cli/templateedit_test.go +++ b/cli/templateedit_test.go @@ -30,6 +30,7 @@ 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 @@ -37,6 +38,7 @@ func TestTemplateEdit(t *testing.T) { "templates", "edit", template.Name, + "--name", name, "--description", desc, "--max-ttl", maxTTL.String(), "--min-autostart-interval", minAutostartInterval.String(), @@ -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) @@ -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(), @@ -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) diff --git a/coderd/database/databasefake/databasefake.go b/coderd/database/databasefake/databasefake.go index 6623d338af7c4..521b174ee1069 100644 --- a/coderd/database/databasefake/databasefake.go +++ b/coderd/database/databasefake/databasefake.go @@ -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 diff --git a/coderd/database/queries.sql.go b/coderd/database/queries.sql.go index d36262a121ee2..f61e74e085ccd 100644 --- a/coderd/database/queries.sql.go +++ b/coderd/database/queries.sql.go @@ -2119,7 +2119,8 @@ SET updated_at = $2, description = $3, max_ttl = $4, - min_autostart_interval = $5 + min_autostart_interval = $5, + name = $6 WHERE id = $1 RETURNING @@ -2132,6 +2133,7 @@ type UpdateTemplateMetaByIDParams struct { Description string `db:"description" json:"description"` MaxTtl int64 `db:"max_ttl" json:"max_ttl"` MinAutostartInterval int64 `db:"min_autostart_interval" json:"min_autostart_interval"` + Name string `db:"name" json:"name"` } func (q *sqlQuerier) UpdateTemplateMetaByID(ctx context.Context, arg UpdateTemplateMetaByIDParams) error { @@ -2141,6 +2143,7 @@ func (q *sqlQuerier) UpdateTemplateMetaByID(ctx context.Context, arg UpdateTempl arg.Description, arg.MaxTtl, arg.MinAutostartInterval, + arg.Name, ) return err } diff --git a/coderd/database/queries/templates.sql b/coderd/database/queries/templates.sql index 5d10ae185da09..f59d899597414 100644 --- a/coderd/database/queries/templates.sql +++ b/coderd/database/queries/templates.sql @@ -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 diff --git a/coderd/templates.go b/coderd/templates.go index e6bab1c25c927..321b9872a5f46 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -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 } @@ -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), diff --git a/coderd/templates_test.go b/coderd/templates_test.go index 00e8aed712b95..c4cfae7f16ac6 100644 --- a/coderd/templates_test.go +++ b/coderd/templates_test.go @@ -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(), @@ -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) @@ -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) @@ -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, @@ -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) @@ -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) diff --git a/codersdk/templates.go b/codersdk/templates.go index 266de9305c905..6e8a0668f69c6 100644 --- a/codersdk/templates.go +++ b/codersdk/templates.go @@ -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"` diff --git a/site/src/api/typesGenerated.ts b/site/src/api/typesGenerated.ts index e849b1a1f051f..cb5a9b5460c6b 100644 --- a/site/src/api/typesGenerated.ts +++ b/site/src/api/typesGenerated.ts @@ -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