grafana/pkg/services/ngalert/api/api_notifications.go
Matthew Jacobson 32f06c6d9c
Alerting: Receiver API complete core implementation (#91738)
* Replace global authz abstraction with one compatible with uid scope

* Replace GettableApiReceiver with models.Receiver in receiver_svc

* GrafanaIntegrationConfig -> models.Integration

* Implement Create/Update methods

* Add optimistic concurrency to receiver API

* Add scope to ReceiversRead & ReceiversReadSecrets

migrates existing permissions to include implicit global scope

* Add receiver create, update, delete actions

* Check if receiver is used by rules before delete

* On receiver name change update in routes and notification settings

* Improve errors

* Linting

* Include read permissions are requirements for create/update/delete

* Alias ngalert/models to ngmodels to differentiate from v0alpha1 model

* Ensure integration UIDs are valid, unique, and generated if empty

* Validate integration settings on create/update

* Leverage UidToName to GetReceiver instead of GetReceivers

* Remove some unnecessary uses of simplejson

* alerting.notifications.receiver -> alerting.notifications.receivers

* validator -> provenanceValidator

* Only validate the modified receiver

stops existing invalid receivers from preventing modification of a valid
receiver.

* Improve error in Integration.Encrypt

* Remove scope from alert.notifications.receivers:create

* Add todos for receiver renaming

* Use receiverAC precondition checks in k8s api

* Linting

* Optional optimistic concurrency for delete

* make update-workspace

* More specific auth checks in k8s authorize.go

* Add debug log when delete optimistic concurrency is skipped

* Improve error message on authorizer.DecisionDeny

* Keep error for non-forbidden errutil errors
2024-08-26 10:47:53 -04:00

81 lines
2.7 KiB
Go

package api
import (
"context"
"net/http"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/infra/log"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
type NotificationSrv struct {
logger log.Logger
receiverService ReceiverService
muteTimingService MuteTimingService // defined in api_provisioning.go
}
type ReceiverService interface {
GetReceiver(ctx context.Context, q models.GetReceiverQuery, u identity.Requester) (*models.Receiver, error)
ListReceivers(ctx context.Context, q models.ListReceiversQuery, user identity.Requester) ([]*models.Receiver, error)
}
func (srv *NotificationSrv) RouteGetTimeInterval(c *contextmodel.ReqContext, name string) response.Response {
muteTimeInterval, err := srv.muteTimingService.GetMuteTiming(c.Req.Context(), name, c.OrgID)
if err != nil {
return errorToResponse(err)
}
return response.JSON(http.StatusOK, muteTimeInterval) // TODO convert to timing interval
}
func (srv *NotificationSrv) RouteGetTimeIntervals(c *contextmodel.ReqContext) response.Response {
muteTimeIntervals, err := srv.muteTimingService.GetMuteTimings(c.Req.Context(), c.OrgID)
if err != nil {
return errorToResponse(err)
}
return response.JSON(http.StatusOK, muteTimeIntervals) // TODO convert to timing interval
}
func (srv *NotificationSrv) RouteGetReceiver(c *contextmodel.ReqContext, name string) response.Response {
q := models.GetReceiverQuery{
OrgID: c.SignedInUser.OrgID,
Name: name,
Decrypt: c.QueryBool("decrypt"),
}
receiver, err := srv.receiverService.GetReceiver(c.Req.Context(), q, c.SignedInUser)
if err != nil {
return response.ErrOrFallback(http.StatusInternalServerError, "failed to get receiver", err)
}
gettable, err := GettableApiReceiverFromReceiver(receiver)
if err != nil {
return response.ErrOrFallback(http.StatusInternalServerError, "failed to convert receiver", err)
}
return response.JSON(http.StatusOK, gettable)
}
func (srv *NotificationSrv) RouteGetReceivers(c *contextmodel.ReqContext) response.Response {
q := models.ListReceiversQuery{
OrgID: c.SignedInUser.OrgID,
Names: c.QueryStrings("names"),
Limit: c.QueryInt("limit"),
Offset: c.QueryInt("offset"),
}
receivers, err := srv.receiverService.ListReceivers(c.Req.Context(), q, c.SignedInUser)
if err != nil {
return response.ErrOrFallback(http.StatusInternalServerError, "failed to get receiver groups", err)
}
gettables, err := GettableApiReceiversFromReceivers(receivers)
if err != nil {
return response.ErrOrFallback(http.StatusInternalServerError, "failed to convert receivers", err)
}
return response.JSON(http.StatusOK, gettables)
}