From 7c3acd1768f58c32f05fb37ec6187bd7c0b62134 Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 17:02:40 +0000 Subject: [PATCH 1/7] feat: Make template name editable --- cli/templateedit.go | 3 +++ coderd/database/queries.sql.go | 5 ++++- coderd/database/queries/templates.sql | 3 ++- coderd/templates.go | 8 +++++++- coderd/templates_test.go | 3 +++ codersdk/templates.go | 1 + 6 files changed, 20 insertions(+), 3 deletions(-) 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/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..d7ada49bd9a5e 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: "My edited 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) diff --git a/codersdk/templates.go b/codersdk/templates.go index 266de9305c905..1cb1d0fd1dabf 100644 --- a/codersdk/templates.go +++ b/codersdk/templates.go @@ -34,6 +34,7 @@ type UpdateActiveTemplateVersion struct { } type UpdateTemplateMeta struct { + Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` MaxTTLMillis int64 `json:"max_ttl_ms,omitempty"` MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms,omitempty"` From 7c3b8fe1742b955239c49e88ce133254ccd57c5c Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 17:08:16 +0000 Subject: [PATCH 2/7] Add field validation --- coderd/templates_test.go | 2 +- codersdk/templates.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/coderd/templates_test.go b/coderd/templates_test.go index d7ada49bd9a5e..2edfc0dccc0cd 100644 --- a/coderd/templates_test.go +++ b/coderd/templates_test.go @@ -237,7 +237,7 @@ func TestPatchTemplateMeta(t *testing.T) { ctr.MinAutostartIntervalMillis = ptr.Ref(time.Hour.Milliseconds()) }) req := codersdk.UpdateTemplateMeta{ - Name: "My edited template name", + Name: "new-template-name", Description: "lorem ipsum dolor sit amet et cetera", MaxTTLMillis: 12 * time.Hour.Milliseconds(), MinAutostartIntervalMillis: time.Minute.Milliseconds(), diff --git a/codersdk/templates.go b/codersdk/templates.go index 1cb1d0fd1dabf..2027663c4f033 100644 --- a/codersdk/templates.go +++ b/codersdk/templates.go @@ -34,7 +34,7 @@ type UpdateActiveTemplateVersion struct { } type UpdateTemplateMeta struct { - Name string `json:"name,omitempty"` + Name string `json:"name,omitempty" validate:"username"` Description string `json:"description,omitempty"` MaxTTLMillis int64 `json:"max_ttl_ms,omitempty"` MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms,omitempty"` From e9bbcaa6d075ec6250cf1cfa44085c2ea6eb5dc2 Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 17:12:44 +0000 Subject: [PATCH 3/7] Update databasefake --- coderd/database/databasefake/databasefake.go | 1 + 1 file changed, 1 insertion(+) 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 From ec0bdab255e5535a9146d2ca2c70a465e137fd6d Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 17:23:13 +0000 Subject: [PATCH 4/7] Fix test --- cli/templateedit_test.go | 5 +++++ 1 file changed, 5 insertions(+) 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) From 9356ec6d3c5889aee327a526bddb21714d53510e Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 18:10:06 +0000 Subject: [PATCH 5/7] Fix tests --- coderd/templates_test.go | 3 +++ codersdk/templates.go | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/coderd/templates_test.go b/coderd/templates_test.go index 2edfc0dccc0cd..c4cfae7f16ac6 100644 --- a/coderd/templates_test.go +++ b/coderd/templates_test.go @@ -283,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, @@ -292,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) @@ -327,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 2027663c4f033..474b373e035ef 100644 --- a/codersdk/templates.go +++ b/codersdk/templates.go @@ -34,7 +34,7 @@ type UpdateActiveTemplateVersion struct { } type UpdateTemplateMeta struct { - Name string `json:"name,omitempty" validate:"username"` + Name string `json:"name,omitempty" validate:"username,omitempty"` Description string `json:"description,omitempty"` MaxTTLMillis int64 `json:"max_ttl_ms,omitempty"` MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms,omitempty"` From 7ddc70c59cae61181f0a06d3ae145fa08c3489cc Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 18:46:49 +0000 Subject: [PATCH 6/7] Fix things with magic --- codersdk/templates.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codersdk/templates.go b/codersdk/templates.go index 474b373e035ef..6e8a0668f69c6 100644 --- a/codersdk/templates.go +++ b/codersdk/templates.go @@ -34,7 +34,7 @@ type UpdateActiveTemplateVersion struct { } type UpdateTemplateMeta struct { - Name string `json:"name,omitempty" validate:"username,omitempty"` + 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"` From 403996ef67c37ede97778057a793050c94044c7b Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Wed, 17 Aug 2022 18:51:31 +0000 Subject: [PATCH 7/7] Generate types --- site/src/api/typesGenerated.ts | 1 + 1 file changed, 1 insertion(+) 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