Files
kopia/internal/server/api_notification_profile.go
Jarek Kowalski c1757a0c67 feat(general): misc notifications improvements (#4319)
* feat(general): various notifications improvements

* added API to test notification profiles
* added --http-header to webhook notification configuration
* refactored configuration to always apply defaults before persisting options in the repository
* added 'notification profile show --profile-name=X' command

* more tests

* more test coverage

* report notification code coverage
2024-12-29 09:50:20 -08:00

81 lines
2.5 KiB
Go

package server
import (
"context"
"encoding/json"
"github.com/kopia/kopia/internal/serverapi"
"github.com/kopia/kopia/notification"
"github.com/kopia/kopia/notification/notifyprofile"
"github.com/kopia/kopia/notification/sender"
"github.com/kopia/kopia/repo"
)
func handleNotificationProfileCreate(ctx context.Context, rc requestContext) (any, *apiError) {
var cfg notifyprofile.Config
if err := json.Unmarshal(rc.body, &cfg); err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "malformed request body: "+string(rc.body))
}
if err := repo.WriteSession(ctx, rc.rep, repo.WriteSessionOptions{
Purpose: "NotificationProfileCreate",
}, func(ctx context.Context, w repo.RepositoryWriter) error {
return notifyprofile.SaveProfile(ctx, w, cfg)
}); err != nil {
return nil, internalServerError(err)
}
return &serverapi.Empty{}, nil
}
func handleNotificationProfileTest(ctx context.Context, rc requestContext) (any, *apiError) {
var cfg notifyprofile.Config
if err := json.Unmarshal(rc.body, &cfg); err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "malformed request body: "+string(rc.body))
}
s, err := sender.GetSender(ctx, cfg.ProfileName, cfg.MethodConfig.Type, cfg.MethodConfig.Config)
if err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "unable to construct sender: "+err.Error())
}
//nolint:contextcheck
if err := notification.SendTestNotification(rc.srv.rootContext(), rc.rep, s); err != nil {
return nil, requestError(serverapi.ErrorMalformedRequest, "unable to send notification: "+err.Error())
}
return &serverapi.Empty{}, nil
}
func handleNotificationProfileGet(ctx context.Context, rc requestContext) (any, *apiError) {
cfg, err := notifyprofile.GetProfile(ctx, rc.rep, rc.muxVar("profileName"))
if err != nil {
return nil, internalServerError(err)
}
return cfg, nil
}
func handleNotificationProfileDelete(ctx context.Context, rc requestContext) (any, *apiError) {
if err := repo.WriteSession(ctx, rc.rep, repo.WriteSessionOptions{
Purpose: "NotificationProfileDelete",
}, func(ctx context.Context, w repo.RepositoryWriter) error {
return notifyprofile.DeleteProfile(ctx, w, rc.muxVar("profileName"))
}); err != nil {
return nil, internalServerError(err)
}
return &serverapi.Empty{}, nil
}
func handleNotificationProfileList(ctx context.Context, rc requestContext) (any, *apiError) {
profiles, err := notifyprofile.ListProfiles(ctx, rc.rep)
if err != nil {
return nil, internalServerError(err)
}
return profiles, nil
}