Skip to content

feat: store and display template owner #2228

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 7 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add owner information in apis and ui
  • Loading branch information
AbhineetJain committed Jun 10, 2022
commit 02cb2930ce57d244c3d34fe12687e41d86451369
2 changes: 2 additions & 0 deletions coderd/audit/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestDiff(t *testing.T) {
ActiveVersionID: uuid.UUID{3},
MaxTtl: int64(time.Hour),
MinAutostartInterval: int64(time.Minute),
OwnerID: uuid.NullUUID{UUID: uuid.UUID{4}, Valid: true},
},
exp: audit.Map{
"id": uuid.UUID{1}.String(),
Expand All @@ -97,6 +98,7 @@ func TestDiff(t *testing.T) {
"active_version_id": uuid.UUID{3}.String(),
"max_ttl": int64(3600000000000),
"min_autostart_interval": int64(60000000000),
"owner_id": uuid.UUID{4}.String(),
},
},
})
Expand Down
1 change: 1 addition & 0 deletions coderd/audit/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var AuditableResources = auditMap(map[any]map[string]Action{
"description": ActionTrack,
"max_ttl": ActionTrack,
"min_autostart_interval": ActionTrack,
"owner_id": ActionTrack,
},
&database.TemplateVersion{}: {
"id": ActionTrack,
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 @@ -1316,6 +1316,7 @@ func (q *fakeQuerier) InsertTemplate(_ context.Context, arg database.InsertTempl
Description: arg.Description,
MaxTtl: arg.MaxTtl,
MinAutostartInterval: arg.MinAutostartInterval,
OwnerID: arg.OwnerID,
}
q.templates = append(q.templates, template)
return template, nil
Expand Down
33 changes: 24 additions & 9 deletions coderd/templates.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package coderd

import (
"context"
"database/sql"
"errors"
"fmt"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) {
count = uint32(workspaceCounts[0].Count)
}

httpapi.Write(rw, http.StatusOK, convertTemplate(template, count))
httpapi.Write(rw, http.StatusOK, convertTemplate(r.Context(), api.Database, template, count))
}

func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -97,6 +98,7 @@ func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) {
func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Request) {
var createTemplate codersdk.CreateTemplateRequest
organization := httpmw.OrganizationParam(r)
apiKey := httpmw.APIKey(r)
if !api.Authorize(rw, r, rbac.ActionCreate, rbac.ResourceTemplate.InOrg(organization.ID)) {
return
}
Expand Down Expand Up @@ -175,6 +177,10 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
Description: createTemplate.Description,
MaxTtl: int64(maxTTL),
MinAutostartInterval: int64(minAutostartInterval),
OwnerID: uuid.NullUUID{
UUID: apiKey.UserID,
Valid: true,
},
})
if err != nil {
return xerrors.Errorf("insert template: %s", err)
Expand Down Expand Up @@ -208,7 +214,7 @@ func (api *API) postTemplateByOrganization(rw http.ResponseWriter, r *http.Reque
}
}

template = convertTemplate(dbTemplate, 0)
template = convertTemplate(r.Context(), db, dbTemplate, 0)
return nil
})
if err != nil {
Expand Down Expand Up @@ -258,7 +264,7 @@ func (api *API) templatesByOrganization(rw http.ResponseWriter, r *http.Request)
return
}

httpapi.Write(rw, http.StatusOK, convertTemplates(templates, workspaceCounts))
httpapi.Write(rw, http.StatusOK, convertTemplates(r.Context(), api.Database, templates, workspaceCounts))
}

func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -304,7 +310,7 @@ func (api *API) templateByOrganizationAndName(rw http.ResponseWriter, r *http.Re
count = uint32(workspaceCounts[0].Count)
}

httpapi.Write(rw, http.StatusOK, convertTemplate(template, count))
httpapi.Write(rw, http.StatusOK, convertTemplate(r.Context(), api.Database, template, count))
}

func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -400,29 +406,36 @@ func (api *API) patchTemplateMeta(rw http.ResponseWriter, r *http.Request) {
return
}

httpapi.Write(rw, http.StatusOK, convertTemplate(updated, count))
httpapi.Write(rw, http.StatusOK, convertTemplate(r.Context(), api.Database, updated, count))
}

func convertTemplates(templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow) []codersdk.Template {
func convertTemplates(ctx context.Context, db database.Store, templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow) []codersdk.Template {
apiTemplates := make([]codersdk.Template, 0, len(templates))
for _, template := range templates {
found := false
for _, workspaceCount := range workspaceCounts {
if workspaceCount.TemplateID.String() != template.ID.String() {
continue
}
apiTemplates = append(apiTemplates, convertTemplate(template, uint32(workspaceCount.Count)))
apiTemplates = append(apiTemplates, convertTemplate(ctx, db, template, uint32(workspaceCount.Count)))
found = true
break
}
if !found {
apiTemplates = append(apiTemplates, convertTemplate(template, uint32(0)))
apiTemplates = append(apiTemplates, convertTemplate(ctx, db, template, uint32(0)))
}
}
return apiTemplates
}

func convertTemplate(template database.Template, workspaceOwnerCount uint32) codersdk.Template {
func convertTemplate(ctx context.Context, db database.Store, template database.Template, workspaceOwnerCount uint32) codersdk.Template {
var ownerName string
if template.OwnerID.Valid {
owner, err := db.GetUserByID(ctx, template.OwnerID.UUID)
if err == nil {
ownerName = owner.Username
}
}
return codersdk.Template{
ID: template.ID,
CreatedAt: template.CreatedAt,
Expand All @@ -435,5 +448,7 @@ func convertTemplate(template database.Template, workspaceOwnerCount uint32) cod
Description: template.Description,
MaxTTLMillis: time.Duration(template.MaxTtl).Milliseconds(),
MinAutostartIntervalMillis: time.Duration(template.MinAutostartInterval).Milliseconds(),
OwnerID: template.OwnerID,
OwnerName: ownerName,
}
}
2 changes: 2 additions & 0 deletions codersdk/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Template struct {
Description string `json:"description"`
MaxTTLMillis int64 `json:"max_ttl_ms"`
MinAutostartIntervalMillis int64 `json:"min_autostart_interval_ms"`
OwnerID uuid.NullUUID `json:"owner_id"`
OwnerName string `json:"owner_name"`
}

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 @@ -247,6 +247,8 @@ export interface Template {
readonly description: string
readonly max_ttl_ms: number
readonly min_autostart_interval_ms: number
readonly owner_id?: string
readonly owner_name: string
}

// From codersdk/templateversions.go:14:6
Expand Down Expand Up @@ -276,12 +278,12 @@ export interface TemplateVersionParameter {
readonly default_source_value: boolean
}

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

// From codersdk/templates.go:30:6
// From codersdk/templates.go:32:6
export interface UpdateActiveTemplateVersion {
readonly id: string
}
Expand All @@ -291,7 +293,7 @@ export interface UpdateRoles {
readonly roles: string[]
}

// From codersdk/templates.go:34:6
// From codersdk/templates.go:36:6
export interface UpdateTemplateMeta {
readonly description?: string
readonly max_ttl_ms?: number
Expand Down
9 changes: 9 additions & 0 deletions site/src/components/TemplateStats/TemplateStats.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ UsedByMany.args = {
},
activeVersion: Mocks.MockTemplateVersion,
}

export const UnknownOwner = Template.bind({})
UnknownOwner.args = {
template: {
...Mocks.MockTemplate,
owner_name: "",
},
activeVersion: Mocks.MockTemplateVersion,
}
5 changes: 3 additions & 2 deletions site/src/components/TemplateStats/TemplateStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const Language = {
lastUpdateLabel: "Last updated",
userPlural: "users",
userSingular: "user",
createdByLabel: "Created by"
createdByLabel: "Created by",
defaultTemplateCreator: "<unknown>",
}

export interface TemplateStatsProps {
Expand Down Expand Up @@ -49,7 +50,7 @@ export const TemplateStats: FC<TemplateStatsProps> = ({ template, activeVersion
<div className={styles.statsDivider} />
<div className={styles.statItem}>
<span className={styles.statsLabel}>{Language.createdByLabel}</span>
<span className={styles.statsValue}>admin</span>
<span className={styles.statsValue}>{template.owner_name || Language.defaultTemplateCreator}</span>
</div>
</div>
)
Expand Down
3 changes: 2 additions & 1 deletion site/src/pages/TemplatesPage/TemplatesPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const Language = {
templateTooltipText: "With templates you can create a common configuration for your workspaces using Terraform.",
templateTooltipLink: "Manage templates",
createdByLabel: "Created by",
defaultTemplateOwner: "<unknown>",
}

const TemplateHelpTooltip: React.FC = () => {
Expand Down Expand Up @@ -139,7 +140,7 @@ export const TemplatesPageView: FC<TemplatesPageViewProps> = (props) => {
<TableCell>{Language.developerCount(template.workspace_owner_count)}</TableCell>

<TableCell data-chromatic="ignore">{dayjs().to(dayjs(template.updated_at))}</TableCell>
<TableCell>admin</TableCell>
<TableCell>{template.owner_name || Language.defaultTemplateOwner}</TableCell>
<TableCell>
<div className={styles.arrowCell}>
<KeyboardArrowRight className={styles.arrowRight} />
Expand Down
2 changes: 2 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export const MockTemplate: TypesGen.Template = {
description: "This is a test description.",
max_ttl_ms: 604800000,
min_autostart_interval_ms: 3600000,
owner_id: "test-owner-id",
owner_name: "test_owner",
Copy link
Member

Choose a reason for hiding this comment

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

🥳

}

export const MockWorkspaceAutostartDisabled: TypesGen.UpdateWorkspaceAutostartRequest = {
Expand Down