Skip to content

Commit e925818

Browse files
authored
feat: add template description (#1489)
1 parent b55d83c commit e925818

16 files changed

+57
-23
lines changed

coderd/audit/table.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
6969
"name": ActionTrack,
7070
"provisioner": ActionTrack,
7171
"active_version_id": ActionTrack,
72+
"description": ActionTrack,
7273
},
7374
&database.TemplateVersion{}: {
7475
"id": ActionTrack,

coderd/coderd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func New(options *Options) (http.Handler, func()) {
144144
r.Get("/provisionerdaemons", api.provisionerDaemonsByOrganization)
145145
r.Post("/templateversions", api.postTemplateVersionsByOrganization)
146146
r.Route("/templates", func(r chi.Router) {
147-
r.Post("/", api.postTemplatesByOrganization)
147+
r.Post("/", api.postTemplateByOrganization)
148148
r.Get("/", api.templatesByOrganization)
149149
r.Get("/{templatename}", api.templateByOrganizationAndName)
150150
})

coderd/coderdtest/coderdtest.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,9 @@ func CreateTemplateVersion(t *testing.T, client *codersdk.Client, organizationID
291291
// compatibility with testing. The name assigned is randomly generated.
292292
func CreateTemplate(t *testing.T, client *codersdk.Client, organization uuid.UUID, version uuid.UUID) codersdk.Template {
293293
template, err := client.CreateTemplate(context.Background(), organization, codersdk.CreateTemplateRequest{
294-
Name: randomUsername(),
295-
VersionID: version,
294+
Name: randomUsername(),
295+
Description: randomUsername(),
296+
VersionID: version,
296297
})
297298
require.NoError(t, err)
298299
return template

coderd/database/databasefake/databasefake.go

+1
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
11711171
Name: arg.Name,
11721172
Provisioner: arg.Provisioner,
11731173
ActiveVersionID: arg.ActiveVersionID,
1174+
Description: arg.Description,
11741175
}
11751176
q.templates = append(q.templates, template)
11761177
return template, nil

coderd/database/dump.sql

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE templates DROP COLUMN description;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE templates ADD COLUMN description VARCHAR(128) NOT NULL DEFAULT '';

coderd/database/models.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+14-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/templates.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ INSERT INTO
4646
organization_id,
4747
"name",
4848
provisioner,
49-
active_version_id
49+
active_version_id,
50+
description
5051
)
5152
VALUES
52-
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
53+
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
5354

5455
-- name: UpdateTemplateActiveVersionByID :exec
5556
UPDATE

coderd/templates.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (api *api) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
7575
}
7676

7777
// Create a new template in an organization.
78-
func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Request) {
78+
func (api *api) postTemplateByOrganization(rw http.ResponseWriter, r *http.Request) {
7979
var createTemplate codersdk.CreateTemplateRequest
8080
if !httpapi.Read(rw, r, &createTemplate) {
8181
return
@@ -90,7 +90,7 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ
9090
Message: fmt.Sprintf("template %q already exists", createTemplate.Name),
9191
Errors: []httpapi.Error{{
9292
Field: "name",
93-
Detail: "this value is already in use and should be unique",
93+
Detail: "This value is already in use and should be unique.",
9494
}},
9595
})
9696
return
@@ -133,6 +133,7 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ
133133
Name: createTemplate.Name,
134134
Provisioner: importJob.Provisioner,
135135
ActiveVersionID: templateVersion.ID,
136+
Description: createTemplate.Description,
136137
})
137138
if err != nil {
138139
return xerrors.Errorf("insert template: %s", err)
@@ -280,5 +281,6 @@ func convertTemplate(template database.Template, workspaceOwnerCount uint32) cod
280281
Provisioner: template.Provisioner,
281282
ActiveVersionID: template.ActiveVersionID,
282283
WorkspaceOwnerCount: workspaceOwnerCount,
284+
Description: template.Description,
283285
}
284286
}

coderd/templates_test.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/google/uuid"
9+
"github.com/stretchr/testify/assert"
910
"github.com/stretchr/testify/require"
1011

1112
"github.com/coder/coder/coderd/coderdtest"
@@ -26,14 +27,21 @@ func TestTemplate(t *testing.T) {
2627
})
2728
}
2829

29-
func TestPostTemplatesByOrganization(t *testing.T) {
30+
func TestPostTemplateByOrganization(t *testing.T) {
3031
t.Parallel()
3132
t.Run("Create", func(t *testing.T) {
3233
t.Parallel()
3334
client := coderdtest.New(t, nil)
3435
user := coderdtest.CreateFirstUser(t, client)
3536
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
36-
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
37+
38+
expected := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
39+
40+
got, err := client.Template(context.Background(), expected.ID)
41+
require.NoError(t, err)
42+
43+
assert.Equal(t, expected.Name, got.Name)
44+
assert.Equal(t, expected.Description, got.Description)
3745
})
3846

3947
t.Run("AlreadyExists", func(t *testing.T) {

codersdk/organizations.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ type CreateTemplateVersionRequest struct {
3636

3737
// CreateTemplateRequest provides options when creating a template.
3838
type CreateTemplateRequest struct {
39+
// Name is the name of the template.
3940
Name string `json:"name" validate:"username,required"`
41+
// Description is a description of what the template contains. It must be
42+
// less than 128 bytes.
43+
Description string `json:"description,omitempty" validate:"lt=128"`
4044

41-
// VersionID is an in-progress or completed job to use as
42-
// an initial version of the template.
45+
// VersionID is an in-progress or completed job to use as an initial version
46+
// of the template.
4347
//
4448
// This is required on creation to enable a user-flow of validating a
45-
// template works. There is no reason the data-model cannot support
46-
// empty templates, but it doesn't make sense for users.
49+
// template works. There is no reason the data-model cannot support empty
50+
// templates, but it doesn't make sense for users.
4751
VersionID uuid.UUID `json:"template_version_id" validate:"required"`
4852
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
4953
}

codersdk/templates.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Template struct {
2323
Provisioner database.ProvisionerType `json:"provisioner"`
2424
ActiveVersionID uuid.UUID `json:"active_version_id"`
2525
WorkspaceOwnerCount uint32 `json:"workspace_owner_count"`
26+
Description string `json:"description"`
2627
}
2728

2829
type UpdateActiveTemplateVersion struct {

site/src/api/typesGenerated.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface CreateParameterRequest {
6262
// From codersdk/organizations.go:38:6
6363
export interface CreateTemplateRequest {
6464
readonly name: string
65+
readonly description?: string
6566
readonly template_version_id: string
6667
readonly parameter_values?: CreateParameterRequest[]
6768
}
@@ -94,7 +95,7 @@ export interface CreateWorkspaceBuildRequest {
9495
readonly state?: string
9596
}
9697

97-
// From codersdk/organizations.go:52:6
98+
// From codersdk/organizations.go:56:6
9899
export interface CreateWorkspaceRequest {
99100
readonly template_id: string
100101
readonly name: string
@@ -219,6 +220,7 @@ export interface Template {
219220
readonly provisioner: string
220221
readonly active_version_id: string
221222
readonly workspace_owner_count: number
223+
readonly description: string
222224
}
223225

224226
// From codersdk/templateversions.go:17:6
@@ -263,12 +265,12 @@ export interface TemplateVersionParameterSchema {
263265
readonly validation_value_type: string
264266
}
265267

266-
// From codersdk/templates.go:74:6
268+
// From codersdk/templates.go:75:6
267269
export interface TemplateVersionsByTemplateRequest extends Pagination {
268270
readonly template_id: string
269271
}
270272

271-
// From codersdk/templates.go:28:6
273+
// From codersdk/templates.go:29:6
272274
export interface UpdateActiveTemplateVersion {
273275
readonly id: string
274276
}

site/src/testHelpers/entities.ts

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export const MockTemplate: TypesGen.Template = {
8787
provisioner: MockProvisioner.id,
8888
active_version_id: "",
8989
workspace_owner_count: 1,
90+
description: "This is a test description.",
9091
}
9192

9293
export const MockWorkspaceAutostartDisabled: TypesGen.UpdateWorkspaceAutostartRequest = {

0 commit comments

Comments
 (0)