Skip to content

Commit 1286749

Browse files
authored
chore: include organization name when fetching templates (#13751)
* chore: include organization name when fetching templates * chore: rename template_with_user to template_with_names
1 parent b87c12b commit 1286749

25 files changed

+177
-84
lines changed

cli/templatelist.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func (r *RootCmd) templateList() *serpent.Command {
1414
orgContext := NewOrganizationContext()
1515
formatter := cliui.NewOutputFormatter(
16-
cliui.TableFormat([]templateTableRow{}, []string{"name", "last updated", "used by"}),
16+
cliui.TableFormat([]templateTableRow{}, []string{"name", "organization name", "last updated", "used by"}),
1717
cliui.JSONFormat(),
1818
)
1919

cli/templates.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ type templateTableRow struct {
8383
Template codersdk.Template
8484

8585
// Used by table format:
86-
Name string `json:"-" table:"name,default_sort"`
87-
CreatedAt string `json:"-" table:"created at"`
88-
LastUpdated string `json:"-" table:"last updated"`
89-
OrganizationID uuid.UUID `json:"-" table:"organization id"`
90-
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
91-
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
92-
UsedBy string `json:"-" table:"used by"`
93-
DefaultTTL time.Duration `json:"-" table:"default ttl"`
86+
Name string `json:"-" table:"name,default_sort"`
87+
CreatedAt string `json:"-" table:"created at"`
88+
LastUpdated string `json:"-" table:"last updated"`
89+
OrganizationID uuid.UUID `json:"-" table:"organization id"`
90+
OrganizationName string `json:"-" table:"organization name"`
91+
Provisioner codersdk.ProvisionerType `json:"-" table:"provisioner"`
92+
ActiveVersionID uuid.UUID `json:"-" table:"active version id"`
93+
UsedBy string `json:"-" table:"used by"`
94+
DefaultTTL time.Duration `json:"-" table:"default ttl"`
9495
}
9596

9697
// templateToRows converts a list of templates to a list of templateTableRow for
@@ -99,15 +100,16 @@ func templatesToRows(templates ...codersdk.Template) []templateTableRow {
99100
rows := make([]templateTableRow, len(templates))
100101
for i, template := range templates {
101102
rows[i] = templateTableRow{
102-
Template: template,
103-
Name: template.Name,
104-
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
105-
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
106-
OrganizationID: template.OrganizationID,
107-
Provisioner: template.Provisioner,
108-
ActiveVersionID: template.ActiveVersionID,
109-
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
110-
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
103+
Template: template,
104+
Name: template.Name,
105+
CreatedAt: template.CreatedAt.Format("January 2, 2006"),
106+
LastUpdated: template.UpdatedAt.Format("January 2, 2006"),
107+
OrganizationID: template.OrganizationID,
108+
OrganizationName: template.OrganizationName,
109+
Provisioner: template.Provisioner,
110+
ActiveVersionID: template.ActiveVersionID,
111+
UsedBy: pretty.Sprint(cliui.DefaultStyles.Fuchsia, formatActiveDevelopers(template.ActiveUserCount)),
112+
DefaultTTL: (time.Duration(template.DefaultTTLMillis) * time.Millisecond),
111113
}
112114
}
113115

cli/testdata/coder_templates_list_--help.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ OPTIONS:
1111
-O, --org string, $CODER_ORGANIZATION
1212
Select which organization (uuid or name) to use.
1313

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

1919
-o, --output string (default: table)
2020
Output format. Available formats: table, json.

coderd/apidoc/docs.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmem/dbmem.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func (q *FakeQuerier) getLatestWorkspaceBuildByWorkspaceIDNoLock(_ context.Conte
515515
func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (database.Template, error) {
516516
for _, template := range q.templates {
517517
if template.ID == id {
518-
return q.templateWithUserNoLock(template), nil
518+
return q.templateWithNameNoLock(template), nil
519519
}
520520
}
521521
return database.Template{}, sql.ErrNoRows
@@ -524,26 +524,36 @@ func (q *FakeQuerier) getTemplateByIDNoLock(_ context.Context, id uuid.UUID) (da
524524
func (q *FakeQuerier) templatesWithUserNoLock(tpl []database.TemplateTable) []database.Template {
525525
cpy := make([]database.Template, 0, len(tpl))
526526
for _, t := range tpl {
527-
cpy = append(cpy, q.templateWithUserNoLock(t))
527+
cpy = append(cpy, q.templateWithNameNoLock(t))
528528
}
529529
return cpy
530530
}
531531

532-
func (q *FakeQuerier) templateWithUserNoLock(tpl database.TemplateTable) database.Template {
532+
func (q *FakeQuerier) templateWithNameNoLock(tpl database.TemplateTable) database.Template {
533533
var user database.User
534534
for _, _user := range q.users {
535535
if _user.ID == tpl.CreatedBy {
536536
user = _user
537537
break
538538
}
539539
}
540-
var withUser database.Template
540+
541+
var org database.Organization
542+
for _, _org := range q.organizations {
543+
if _org.ID == tpl.OrganizationID {
544+
org = _org
545+
break
546+
}
547+
}
548+
549+
var withNames database.Template
541550
// This is a cheeky way to copy the fields over without explicitly listing them all.
542551
d, _ := json.Marshal(tpl)
543-
_ = json.Unmarshal(d, &withUser)
544-
withUser.CreatedByUsername = user.Username
545-
withUser.CreatedByAvatarURL = user.AvatarURL
546-
return withUser
552+
_ = json.Unmarshal(d, &withNames)
553+
withNames.CreatedByUsername = user.Username
554+
withNames.CreatedByAvatarURL = user.AvatarURL
555+
withNames.OrganizationName = org.Name
556+
return withNames
547557
}
548558

549559
func (q *FakeQuerier) templateVersionWithUserNoLock(tpl database.TemplateVersionTable) database.TemplateVersion {
@@ -3675,7 +3685,7 @@ func (q *FakeQuerier) GetTemplateByOrganizationAndName(_ context.Context, arg da
36753685
if template.Deleted != arg.Deleted {
36763686
continue
36773687
}
3678-
return q.templateWithUserNoLock(template), nil
3688+
return q.templateWithNameNoLock(template), nil
36793689
}
36803690
return database.Template{}, sql.ErrNoRows
36813691
}
@@ -9323,7 +9333,7 @@ func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.G
93239333

93249334
var templates []database.Template
93259335
for _, templateTable := range q.templates {
9326-
template := q.templateWithUserNoLock(templateTable)
9336+
template := q.templateWithNameNoLock(templateTable)
93279337
if prepared != nil && prepared.Authorize(ctx, template.RBACObject()) != nil {
93289338
continue
93299339
}

coderd/database/dump.sql

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/gentest/models_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestViewSubsetTemplate(t *testing.T) {
3232
tableFields := allFields(table)
3333
joinedFields := allFields(joined)
3434
if !assert.Subset(t, fieldNames(joinedFields), fieldNames(tableFields), "table is not subset") {
35-
t.Log("Some fields were added to the Template Table without updating the 'template_with_users' view.")
35+
t.Log("Some fields were added to the Template Table without updating the 'template_with_names' view.")
3636
t.Log("See migration 000138_join_users.up.sql to create the view.")
3737
}
3838
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
DROP VIEW template_with_names;
2+
3+
CREATE VIEW
4+
template_with_users
5+
AS
6+
SELECT
7+
templates.*,
8+
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
9+
coalesce(visible_users.username, '') AS created_by_username
10+
FROM
11+
templates
12+
LEFT JOIN
13+
visible_users
14+
ON
15+
templates.created_by = visible_users.id;
16+
COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Update the template_with_users view by recreating it.
2+
DROP VIEW template_with_users;
3+
4+
-- Renaming template_with_users -> template_with_names
5+
CREATE VIEW
6+
template_with_names
7+
AS
8+
SELECT
9+
templates.*,
10+
coalesce(visible_users.avatar_url, '') AS created_by_avatar_url,
11+
coalesce(visible_users.username, '') AS created_by_username,
12+
coalesce(organizations.name, '') AS organization_name
13+
FROM
14+
templates
15+
LEFT JOIN
16+
visible_users
17+
ON
18+
templates.created_by = visible_users.id
19+
LEFT JOIN
20+
organizations
21+
ON templates.organization_id = organizations.id
22+
;
23+
24+
COMMENT ON VIEW template_with_names IS 'Joins in the display name information such as username, avatar, and organization name.';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy