Skip to content
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
Next Next commit
feat: add template delete notification
  • Loading branch information
BrunoQuaresma committed Aug 12, 2024
commit 5915daa286eb0c571208080f4fc40210775a0513
Empty file.
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
);
1 change: 1 addition & 0 deletions coderd/notifications/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
TemplateWorkspaceDormant = uuid.MustParse("0ea69165-ec14-4314-91f1-69566ac3c5a0")
TemplateWorkspaceAutoUpdated = uuid.MustParse("c34a0c09-0704-4cac-bd1c-0c0146811c2b")
TemplateWorkspaceMarkedForDeletion = uuid.MustParse("51ce2fdf-c9ca-4be1-8d70-628674f9bc42")
TemplateTemplateDeleted = uuid.MustParse("29a09665-2a4c-403f-9648-54301670e7be")
)

// Account-related events.
Expand Down
31 changes: 31 additions & 0 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 All @@ -12,12 +13,15 @@
"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"
Expand Down Expand Up @@ -56,6 +60,7 @@
// @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()
Expand Down Expand Up @@ -101,11 +106,37 @@
})
return
}

api.notifyTemplateDeleted(ctx, template, apiKey.UserID)

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) {
if template.CreatedBy == initiatorID {
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, template.CreatedBy, 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))

Check failure on line 136 in coderd/templates.go

View workflow job for this annotation

GitHub Actions / lint

ruleguard: uuid.UUID field "deleted_template" must have "_id" suffix. (gocritic)
}
}

// Create a new template in an organization.
// Returns a single template.
//
Expand Down
64 changes: 64 additions & 0 deletions coderd/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -1326,3 +1327,66 @@ func TestTemplateMetrics(t *testing.T) {
dbtime.Now(), res.Workspaces[0].LastUsedAt, time.Minute,
)
}

func TestTemplateNotifications(t *testing.T) {
t.Parallel()

t.Run("Delete", func(t *testing.T) {
t.Parallel()

t.Run("InitiatorNotTemplateAdmin", func(t *testing.T) {
t.Parallel()

var (
notifyEnq = &testutil.FakeNotificationsEnqueuer{}
client = coderdtest.New(t, &coderdtest.Options{
IncludeProvisionerDaemon: true,
NotificationsEnqueuer: notifyEnq,
})
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
initiatorClient, _ = coderdtest.CreateAnotherUser(t, client, user.OrganizationID, rbac.RoleOwner())
ctx = testutil.Context(t, testutil.WaitLong)
)

// When: initiator is not the template admin
err := initiatorClient.DeleteTemplate(ctx, template.ID)
require.NoError(t, err)

// Then: the template admin should receive a notification
require.Len(t, notifyEnq.Sent, 1)
require.Equal(t, notifyEnq.Sent[0].TemplateID, notifications.TemplateTemplateDeleted)
require.Equal(t, notifyEnq.Sent[0].UserID, template.CreatedByID)
require.Contains(t, notifyEnq.Sent[0].Targets, template.ID)
require.Contains(t, notifyEnq.Sent[0].Targets, template.OrganizationID)
require.Contains(t, notifyEnq.Sent[0].Labels["name"], template.Name)
require.Contains(t, notifyEnq.Sent[0].Labels["initiator"], coderdtest.FirstUserParams.Username)
})

t.Run("InitiatorIsTemplateAdmin", func(t *testing.T) {
t.Parallel()

var (
notifyEnq = &testutil.FakeNotificationsEnqueuer{}
client = coderdtest.New(t, &coderdtest.Options{
IncludeProvisionerDaemon: true,
NotificationsEnqueuer: notifyEnq,
})
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
_ = coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
ctx = testutil.Context(t, testutil.WaitLong)
)

// When: initiator is the template admin
err := client.DeleteTemplate(ctx, template.ID)
require.NoError(t, err)

// Then: the template admin should not receive a notification
require.Len(t, notifyEnq.Sent, 0)
})
})
}
2 changes: 1 addition & 1 deletion coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3441,7 +3441,7 @@ func TestWorkspaceUsageTracking(t *testing.T) {
})
}

func TestNotifications(t *testing.T) {
func TestWorkspaceNotifications(t *testing.T) {
t.Parallel()

t.Run("Dormant", func(t *testing.T) {
Expand Down
Loading