- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: add template delete notification#14250
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from3 commits
5915daa76ee1f61f7046c30faf4f1e11cb799c1950be31ed89e6462d7fb0fa292c11de450235eeaf87c54688765File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| INSERT INTO | ||
| notification_templates ( | ||
| id, | ||
| name, | ||
| title_template, | ||
| body_template, | ||
| "group", | ||
| actions | ||
| ) | ||
| VALUES ( | ||
| '29a09665-2a4c-403f-9648-54301670e7be', | ||
| 'Template Deleted', | ||
| E'Template "{{.Labels.name}}" deleted', | ||
| E'Hi {{.UserName}}\n\nThe template **{{.Labels.name}}** was deleted by **{{ .Labels.initiator }}**.', | ||
| 'Template Events', | ||
| '[ | ||
| { | ||
| "label": "View templates", | ||
| "url": "{{ base_url }}/templates" | ||
| } | ||
| ]'::jsonb | ||
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package coderd | ||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "errors" | ||
| "fmt" | ||
| @@ -12,12 +13,15 @@ import ( | ||
| "github.com/google/uuid" | ||
| "golang.org/x/xerrors" | ||
| "cdr.dev/slog" | ||
| "github.com/coder/coder/v2/coderd/audit" | ||
| "github.com/coder/coder/v2/coderd/database" | ||
| "github.com/coder/coder/v2/coderd/database/dbauthz" | ||
| "github.com/coder/coder/v2/coderd/database/dbtime" | ||
| "github.com/coder/coder/v2/coderd/httpapi" | ||
| "github.com/coder/coder/v2/coderd/httpmw" | ||
| "github.com/coder/coder/v2/coderd/notifications" | ||
| "github.com/coder/coder/v2/coderd/rbac" | ||
| "github.com/coder/coder/v2/coderd/rbac/policy" | ||
| "github.com/coder/coder/v2/coderd/schedule" | ||
| @@ -56,6 +60,7 @@ func (api *API) template(rw http.ResponseWriter, r *http.Request) { | ||
| // @Router /templates/{template} [delete] | ||
| func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) { | ||
| var ( | ||
| apiKey = httpmw.APIKey(r) | ||
| ctx = r.Context() | ||
| template = httpmw.TemplateParam(r) | ||
| auditor = *api.Auditor.Load() | ||
| @@ -101,11 +106,50 @@ func (api *API) deleteTemplate(rw http.ResponseWriter, r *http.Request) { | ||
| }) | ||
| return | ||
| } | ||
| admins, err := findTemplateAdmins(ctx, api.Database) | ||
| if err != nil { | ||
| httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{ | ||
| Message: "Internal error fetching template admins.", | ||
| Detail: err.Error(), | ||
| }) | ||
| return | ||
| } | ||
| for _, admin := range admins { | ||
| if admin.ID == apiKey.UserID { | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| continue | ||
| } | ||
| api.notifyTemplateDeleted(ctx, template, apiKey.UserID, admin.ID) | ||
| } | ||
| httpapi.Write(ctx, rw, http.StatusOK, codersdk.Response{ | ||
| Message: "Template has been deleted!", | ||
| }) | ||
| } | ||
| func (api *API) notifyTemplateDeleted(ctx context.Context, template database.Template, initiatorID uuid.UUID, receiverID uuid.UUID) { | ||
| if initiatorID == receiverID { | ||
dannykopping marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| return | ||
| } | ||
| initiator, err := api.Database.GetUserByID(ctx, initiatorID) | ||
| if err != nil { | ||
| api.Logger.Warn(ctx, "failed to fetch initiator for template deletion notification", slog.F("initiator_id", initiatorID), slog.Error(err)) | ||
| return | ||
| } | ||
| if _, err := api.NotificationsEnqueuer.Enqueue(ctx, receiverID, notifications.TemplateTemplateDeleted, | ||
| map[string]string{ | ||
| "name": template.Name, | ||
| "initiator": initiator.Username, | ||
| }, "api-templates-delete", | ||
| // Associate this notification with all the related entities. | ||
| template.ID, template.OrganizationID, | ||
| ); err != nil { | ||
| api.Logger.Warn(ctx, "failed to notify of template deletion", slog.F("deleted_template", template.ID), slog.Error(err)) | ||
| } | ||
| } | ||
| // Create a new template in an organization. | ||
| // Returns a single template. | ||
| // | ||
| @@ -948,3 +992,22 @@ func (api *API) convertTemplate( | ||
| MaxPortShareLevel: maxPortShareLevel, | ||
| } | ||
| } | ||
| // findTemplateAdmins fetches all users with template admin permission including owners. | ||
| func findTemplateAdmins(ctx context.Context, store database.Store) ([]database.GetUsersRow, error) { | ||
| // Notice: we can't scrape the user information in parallel as pq | ||
| // fails with: unexpected describe rows response: 'D' | ||
| owners, err := store.GetUsers(ctx, database.GetUsersParams{ | ||
| RbacRole: []string{codersdk.RoleOwner}, | ||
| }) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("get owners: %w", err) | ||
| } | ||
| templateAdmins, err := store.GetUsers(ctx, database.GetUsersParams{ | ||
| RbacRole: []string{codersdk.RoleTemplateAdmin}, | ||
| }) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("get template admins: %w", err) | ||
| } | ||
| return append(owners, templateAdmins...), nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -19,6 +19,7 @@ import ( | ||
| "github.com/coder/coder/v2/coderd/database" | ||
| "github.com/coder/coder/v2/coderd/database/dbauthz" | ||
| "github.com/coder/coder/v2/coderd/database/dbtime" | ||
| "github.com/coder/coder/v2/coderd/notifications" | ||
| "github.com/coder/coder/v2/coderd/rbac" | ||
| "github.com/coder/coder/v2/coderd/schedule" | ||
| "github.com/coder/coder/v2/coderd/util/ptr" | ||
| @@ -1326,3 +1327,57 @@ func TestTemplateMetrics(t *testing.T) { | ||
| dbtime.Now(), res.Workspaces[0].LastUsedAt, time.Minute, | ||
| ) | ||
| } | ||
| func TestNotifyDeletedTemplate(t *testing.T) { | ||
dannykopping marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| t.Parallel() | ||
| t.Run("OnlyNotifyOwnersAndTemplateAdmins", func(t *testing.T) { | ||
| ||
| t.Parallel() | ||
| var ( | ||
| notifyEnq = &testutil.FakeNotificationsEnqueuer{} | ||
| client = coderdtest.New(t, &coderdtest.Options{ | ||
| IncludeProvisionerDaemon: true, | ||
| NotificationsEnqueuer: notifyEnq, | ||
| }) | ||
| firstUser = coderdtest.CreateFirstUser(t, client) | ||
| version = coderdtest.CreateTemplateVersion(t, client, firstUser.OrganizationID, nil) | ||
| _ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||
| template = coderdtest.CreateTemplate(t, client, firstUser.OrganizationID, version.ID) | ||
| ctx = testutil.Context(t, testutil.WaitLong) | ||
| ) | ||
| _, anotherOwner := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleOwner()) | ||
| _, tmplAdmin := coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleTemplateAdmin()) | ||
| coderdtest.CreateAnotherUser(t, client, firstUser.OrganizationID, rbac.RoleMember()) | ||
| // When: template is deleted | ||
| err := client.DeleteTemplate(ctx, template.ID) | ||
| require.NoError(t, err) | ||
| // Then: the template owners and template admins should receive the notification | ||
| shouldBeNotified := []uuid.UUID{anotherOwner.ID, tmplAdmin.ID} | ||
| ||
| var deleteTemplateNotifications []*testutil.Notification | ||
| for _, n := range notifyEnq.Sent { | ||
| if n.TemplateID == notifications.TemplateTemplateDeleted { | ||
| deleteTemplateNotifications = append(deleteTemplateNotifications, n) | ||
| } | ||
| } | ||
| require.Len(t, deleteTemplateNotifications, len(shouldBeNotified)) | ||
| for _, userID := range shouldBeNotified { | ||
dannykopping marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| var notification *testutil.Notification | ||
| for _, n := range deleteTemplateNotifications { | ||
| if n.UserID == userID { | ||
| notification = n | ||
| break | ||
| } | ||
| } | ||
| require.NotNil(t, notification) | ||
| require.Contains(t, notification.Targets, template.ID) | ||
| require.Contains(t, notification.Targets, template.OrganizationID) | ||
| require.Equal(t, notification.Labels["name"], template.Name) | ||
| require.Equal(t, notification.Labels["initiator"], coderdtest.FirstUserParams.Username) | ||
| } | ||
| }) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -3441,7 +3441,7 @@ func TestWorkspaceUsageTracking(t *testing.T) { | ||
| }) | ||
| } | ||
| funcTestWorkspaceNotifications(t *testing.T) { | ||
dannykopping marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| t.Parallel() | ||
| t.Run("Dormant", func(t *testing.T) { | ||