Skip to content

feat: add template description #1489

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 6 commits into from
May 16, 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
1 change: 1 addition & 0 deletions coderd/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
"name": ActionTrack,
"provisioner": ActionTrack,
"active_version_id": ActionTrack,
"description": ActionTrack,
},
&database.TemplateVersion{}: {
"id": ActionTrack,
Expand Down
2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func New(options *Options) (http.Handler, func()) {
r.Get("/provisionerdaemons", api.provisionerDaemonsByOrganization)
r.Post("/templateversions", api.postTemplateVersionsByOrganization)
r.Route("/templates", func(r chi.Router) {
r.Post("/", api.postTemplatesByOrganization)
r.Post("/", api.postTemplateByOrganization)
r.Get("/", api.templatesByOrganization)
r.Get("/{templatename}", api.templateByOrganizationAndName)
})
Expand Down
5 changes: 3 additions & 2 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ func CreateTemplateVersion(t *testing.T, client *codersdk.Client, organizationID
// compatibility with testing. The name assigned is randomly generated.
func CreateTemplate(t *testing.T, client *codersdk.Client, organization uuid.UUID, version uuid.UUID) codersdk.Template {
template, err := client.CreateTemplate(context.Background(), organization, codersdk.CreateTemplateRequest{
Name: randomUsername(),
VersionID: version,
Name: randomUsername(),
Description: randomUsername(),
VersionID: version,
})
require.NoError(t, err)
return template
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 @@ -1171,6 +1171,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
Name: arg.Name,
Provisioner: arg.Provisioner,
ActiveVersionID: arg.ActiveVersionID,
Description: arg.Description,
}
q.templates = append(q.templates, template)
return template, nil
Expand Down
3 changes: 2 additions & 1 deletion 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 @@
ALTER TABLE templates DROP COLUMN description;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE templates ADD COLUMN description VARCHAR(128) NOT NULL DEFAULT '';
1 change: 1 addition & 0 deletions coderd/database/models.go

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

20 changes: 14 additions & 6 deletions coderd/database/queries.sql.go

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

5 changes: 3 additions & 2 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ INSERT INTO
organization_id,
"name",
provisioner,
active_version_id
active_version_id,
description
)
VALUES
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;

-- name: UpdateTemplateActiveVersionByID :exec
UPDATE
Expand Down
6 changes: 4 additions & 2 deletions coderd/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (api *api) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
}

// Create a new template in an organization.
func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Request) {
func (api *api) postTemplateByOrganization(rw http.ResponseWriter, r *http.Request) {
var createTemplate codersdk.CreateTemplateRequest
if !httpapi.Read(rw, r, &createTemplate) {
return
Expand All @@ -90,7 +90,7 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ
Message: fmt.Sprintf("template %q already exists", createTemplate.Name),
Errors: []httpapi.Error{{
Field: "name",
Detail: "this value is already in use and should be unique",
Detail: "This value is already in use and should be unique.",
}},
})
return
Expand Down Expand Up @@ -133,6 +133,7 @@ func (api *api) postTemplatesByOrganization(rw http.ResponseWriter, r *http.Requ
Name: createTemplate.Name,
Provisioner: importJob.Provisioner,
ActiveVersionID: templateVersion.ID,
Description: createTemplate.Description,
})
if err != nil {
return xerrors.Errorf("insert template: %s", err)
Expand Down Expand Up @@ -280,5 +281,6 @@ func convertTemplate(template database.Template, workspaceOwnerCount uint32) cod
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
WorkspaceOwnerCount: workspaceOwnerCount,
Description: template.Description,
}
}
12 changes: 10 additions & 2 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestPostTemplatesByOrganization(t *testing.T) {
func TestPostTemplateByOrganization(t *testing.T) {
t.Parallel()
t.Run("Create", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
user := coderdtest.CreateFirstUser(t, client)
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

expected := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)

got, err := client.Template(context.Background(), expected.ID)
require.NoError(t, err)

assert.Equal(t, expected.Name, got.Name)
assert.Equal(t, expected.Description, got.Description)
})

t.Run("AlreadyExists", func(t *testing.T) {
Expand Down
12 changes: 8 additions & 4 deletions codersdk/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ type CreateTemplateVersionRequest struct {

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

// VersionID is an in-progress or completed job to use as
// an initial version of the template.
// VersionID is an in-progress or completed job to use as an initial version
// of the template.
//
// This is required on creation to enable a user-flow of validating a
// template works. There is no reason the data-model cannot support
// empty templates, but it doesn't make sense for users.
// template works. There is no reason the data-model cannot support empty
// templates, but it doesn't make sense for users.
VersionID uuid.UUID `json:"template_version_id" validate:"required"`
ParameterValues []CreateParameterRequest `json:"parameter_values,omitempty"`
}
Expand Down
1 change: 1 addition & 0 deletions codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Template struct {
Provisioner database.ProvisionerType `json:"provisioner"`
ActiveVersionID uuid.UUID `json:"active_version_id"`
WorkspaceOwnerCount uint32 `json:"workspace_owner_count"`
Description string `json:"description"`
}

type UpdateActiveTemplateVersion struct {
Expand Down
8 changes: 5 additions & 3 deletions site/src/api/typesGenerated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export interface CreateParameterRequest {
// From codersdk/organizations.go:38:6
export interface CreateTemplateRequest {
readonly name: string
readonly description?: string
readonly template_version_id: string
readonly parameter_values?: CreateParameterRequest[]
}
Expand Down Expand Up @@ -94,7 +95,7 @@ export interface CreateWorkspaceBuildRequest {
readonly state?: string
}

// From codersdk/organizations.go:52:6
// From codersdk/organizations.go:56:6
export interface CreateWorkspaceRequest {
readonly template_id: string
readonly name: string
Expand Down Expand Up @@ -219,6 +220,7 @@ export interface Template {
readonly provisioner: string
readonly active_version_id: string
readonly workspace_owner_count: number
readonly description: string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Here too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backend will always return a description, but it can be an empty string.

}

// From codersdk/templateversions.go:17:6
Expand Down Expand Up @@ -263,12 +265,12 @@ export interface TemplateVersionParameterSchema {
readonly validation_value_type: string
}

// From codersdk/templates.go:74:6
// From codersdk/templates.go:75:6
export interface TemplateVersionsByTemplateRequest extends Pagination {
readonly template_id: string
}

// From codersdk/templates.go:28:6
// From codersdk/templates.go:29:6
export interface UpdateActiveTemplateVersion {
readonly id: string
}
Expand Down
1 change: 1 addition & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const MockTemplate: TypesGen.Template = {
provisioner: MockProvisioner.id,
active_version_id: "",
workspace_owner_count: 1,
description: "This is a test description.",
}

export const MockWorkspaceAutostartDisabled: TypesGen.UpdateWorkspaceAutostartRequest = {
Expand Down