Skip to content

chore: include organization name when fetching templates #13751

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
Jul 2, 2024
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
2 changes: 1 addition & 1 deletion cli/templatelist.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func (r *RootCmd) templateList() *serpent.Command {
orgContext := NewOrganizationContext()
formatter := cliui.NewOutputFormatter(
cliui.TableFormat([]templateTableRow{}, []string{"name", "last updated", "used by"}),
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
cliui.JSONFormat(),
)

Expand Down
36 changes: 19 additions & 17 deletions cli/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ type templateTableRow struct {
Template codersdk.Template

// Used by table format:
Name string `json:"-" table:"name,default_sort"`
CreatedAt string `json:"-" table:"created at"`
LastUpdated string `json:"-" table:"last updated"`
OrganizationID uuid.UUID `json:"-" table:"organization id"`
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
UsedBy string `json:"-" table:"used by"`
DefaultTTL time.Duration `json:"-" table:"default ttl"`
Name string `json:"-" table:"name,default_sort"`
CreatedAt string `json:"-" table:"created at"`
LastUpdated string `json:"-" table:"last updated"`
OrganizationID uuid.UUID `json:"-" table:"organization id"`
OrganizationName string `json:"-" table:"organization name"`
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
UsedBy string `json:"-" table:"used by"`
DefaultTTL time.Duration `json:"-" table:"default ttl"`
}

// templateToRows converts a list of templates to a list of templateTableRow for
Expand All @@ -99,15 +100,16 @@ func templatesToRows(templates ...codersdk.Template) []templateTableRow {
rows := make([]templateTableRow, len(templates))
for i, template := range templates {
rows[i] = templateTableRow{
Template: template,
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
Template: template,
Name: template.Name,
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
OrganizationID: template.OrganizationID,
OrganizationName: template.OrganizationName,
Provisioner: template.Provisioner,
ActiveVersionID: template.ActiveVersionID,
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
}
}

Expand Down
6 changes: 3 additions & 3 deletions cli/testdata/coder_templates_list_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ OPTIONS:
-O, --org string, $CODER_ORGANIZATION
Select which organization (uuid or name) to use.

-c, --column string-array (default: name,last updated,used by)
-c, --column string-array (default: name,organization name,last updated,used by)
Columns to display in table output. Available columns: name, created
at, last updated, organization id, provisioner, active version id,
used by, default ttl.
at, last updated, organization id, organization name, provisioner,
active version id, used by, default ttl.

-o, --output string (default: table)
Output format. Available formats: table, json.
Expand Down
4 changes: 4 additions & 0 deletions coderd/apidoc/docs.go

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

4 changes: 4 additions & 0 deletions coderd/apidoc/swagger.json

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

30 changes: 20 additions & 10 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func (q *FakeQuerier) getLatestWorkspaceBuildByWorkspaceIDNoLock(_ context.Conte
func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
for _, template := range q.templates {
if template.ID == id {
return q.templateWithUserNoLock(template), nil
return q.templateWithNameNoLock(template), nil
}
}
return database.Template{}, sql.ErrNoRows
Expand All @@ -524,26 +524,36 @@ func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (da
func (q *FakeQuerier) templatesWithUserNoLock(tpl []database.TemplateTable) []database.Template {
cpy := make([]database.Template, 0, len(tpl))
for _, t := range tpl {
cpy = append(cpy, q.templateWithUserNoLock(t))
cpy = append(cpy, q.templateWithNameNoLock(t))
}
return cpy
}

func (q *FakeQuerier) templateWithUserNoLock(tpl database.TemplateTable) database.Template {
func (q *FakeQuerier) templateWithNameNoLock(tpl database.TemplateTable) database.Template {
var user database.User
for _, _user := range q.users {
if _user.ID == tpl.CreatedBy {
user = _user
break
}
}
var withUser database.Template

var org database.Organization
for _, _org := range q.organizations {
if _org.ID == tpl.OrganizationID {
org = _org
break
}
}

var withNames database.Template
// This is a cheeky way to copy the fields over without explicitly listing them all.
d, _ := json.Marshal(tpl)
_ = json.Unmarshal(d, &withUser)
withUser.CreatedByUsername = user.Username
withUser.CreatedByAvatarURL = user.AvatarURL
return withUser
_ = json.Unmarshal(d, &withNames)
withNames.CreatedByUsername = user.Username
withNames.CreatedByAvatarURL = user.AvatarURL
withNames.OrganizationName = org.Name
return withNames
}

func (q *FakeQuerier) templateVersionWithUserNoLock(tpl database.TemplateVersionTable) database.TemplateVersion {
Expand Down Expand Up @@ -3675,7 +3685,7 @@ func (q *FakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
if template.Deleted != arg.Deleted {
continue
}
return q.templateWithUserNoLock(template), nil
return q.templateWithNameNoLock(template), nil
}
return database.Template{}, sql.ErrNoRows
}
Expand Down Expand Up @@ -9323,7 +9333,7 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G

var templates []database.Template
for _, templateTable := range q.templates {
template := q.templateWithUserNoLock(templateTable)
template := q.templateWithNameNoLock(templateTable)
if prepared != nil && prepared.Authorize(ctx, template.RBACObject()) != nil {
continue
}
Expand Down
12 changes: 7 additions & 5 deletions coderd/database/dump.sql

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

2 changes: 1 addition & 1 deletion coderd/database/gentest/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestViewSubsetTemplate(t *testing.T) {
tableFields := allFields(table)
joinedFields := allFields(joined)
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
t.Log("Some fields were added to the Template Table without updating the 'template_with_users' view.")
t.Log("Some fields were added to the Template Table without updating the 'template_with_names' view.")
t.Log("See migration 000138_join_users.up.sql to create the view.")
}
}
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

reminder: double-check migration number before merge!

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DROP VIEW template_with_names;

CREATE VIEW
template_with_users
AS
SELECT
templates.*,
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
coalesce(visible_users.username, '') AS created_by_username
FROM
templates
LEFT JOIN
visible_users
ON
templates.created_by = visible_users.id;
COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Update the template_with_users view by recreating it.
DROP VIEW template_with_users;

-- Renaming template_with_users -> template_with_names
CREATE VIEW
template_with_names
AS
SELECT
templates.*,
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
coalesce(visible_users.username, '') AS created_by_username,
coalesce(organizations.name, '') AS organization_name
FROM
templates
LEFT JOIN
visible_users
ON
templates.created_by = visible_users.id
LEFT JOIN
organizations
ON templates.organization_id = organizations.id
;

COMMENT ON VIEW template_with_names IS 'Joins in the display name information such as username, avatar, and organization name.';
1 change: 1 addition & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (q *sqlQuerier) GetAuthorizedTemplates(ctx context.Context, arg GetTemplate
&i.MaxPortSharingLevel,
&i.CreatedByAvatarURL,
&i.CreatedByUsername,
&i.OrganizationName,
); err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/models.go

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

18 changes: 11 additions & 7 deletions coderd/database/queries.sql.go

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

8 changes: 4 additions & 4 deletions coderd/database/queries/templates.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SELECT
*
FROM
template_with_users
template_with_names
WHERE
id = $1
LIMIT
Expand All @@ -12,7 +12,7 @@ LIMIT
SELECT
*
FROM
template_with_users AS templates
template_with_names AS templates
WHERE
-- Optionally include deleted templates
templates.deleted = @deleted
Expand Down Expand Up @@ -54,7 +54,7 @@ ORDER BY (name, id) ASC
SELECT
*
FROM
template_with_users AS templates
template_with_names AS templates
WHERE
organization_id = @organization_id
AND deleted = @deleted
Expand All @@ -63,7 +63,7 @@ LIMIT
1;

-- name: GetTemplates :many
SELECT * FROM template_with_users AS templates
SELECT * FROM template_with_names AS templates
ORDER BY (name, id) ASC
;

Expand Down
Loading
Loading