Files
grafana/pkg/services/cloudmigration/cloudmigrationimpl/snapshot_mgmt_alerts.go
T
Matheus Macabu 1051561154 CloudMigrations: create snapshot for Notification Policies (#94852)
* CloudMigrations: create snapshot for Notification Policy

* CloudMigrations: add notification policy constants and components

* CloudMigrations: add uid to resources that have it
2024-10-17 14:58:05 +02:00

136 lines
4.3 KiB
Go

package cloudmigrationimpl
import (
"context"
"fmt"
"github.com/prometheus/alertmanager/config"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
"github.com/grafana/grafana/pkg/services/user"
)
type muteTimeInterval struct {
UID string `json:"uid"`
// There is a lot of custom (de)serialization logic from Alertmanager,
// and this is the same type used by the underlying API, hence we can use the type as-is.
config.MuteTimeInterval `json:",inline"`
}
func (s *Service) getAlertMuteTimings(ctx context.Context, signedInUser *user.SignedInUser) ([]muteTimeInterval, error) {
if !s.features.IsEnabledGlobally(featuremgmt.FlagOnPremToCloudMigrationsAlerts) {
return nil, nil
}
muteTimings, err := s.ngAlert.Api.MuteTimings.GetMuteTimings(ctx, signedInUser.OrgID)
if err != nil {
return nil, fmt.Errorf("fetching ngalert mute timings: %w", err)
}
muteTimeIntervals := make([]muteTimeInterval, 0, len(muteTimings))
for _, muteTiming := range muteTimings {
muteTimeIntervals = append(muteTimeIntervals, muteTimeInterval{
UID: muteTiming.UID,
MuteTimeInterval: config.MuteTimeInterval{
Name: muteTiming.Name,
TimeIntervals: muteTiming.TimeIntervals,
},
})
}
return muteTimeIntervals, nil
}
type notificationTemplate struct {
UID string `json:"uid"`
Name string `json:"name"`
Template string `json:"template"`
}
func (s *Service) getNotificationTemplates(ctx context.Context, signedInUser *user.SignedInUser) ([]notificationTemplate, error) {
if !s.features.IsEnabledGlobally(featuremgmt.FlagOnPremToCloudMigrationsAlerts) {
return nil, nil
}
templates, err := s.ngAlert.Api.Templates.GetTemplates(ctx, signedInUser.OrgID)
if err != nil {
return nil, fmt.Errorf("fetching ngalert notification templates: %w", err)
}
notificationTemplates := make([]notificationTemplate, 0, len(templates))
for _, template := range templates {
notificationTemplates = append(notificationTemplates, notificationTemplate{
UID: template.UID,
Name: template.Name,
Template: template.Template,
})
}
return notificationTemplates, nil
}
type contactPoint struct {
Settings *simplejson.Json `json:"settings"`
UID string `json:"uid"`
Name string `json:"name"`
Type string `json:"type"`
DisableResolveMessage bool `json:"disableResolveMessage"`
}
func (s *Service) getContactPoints(ctx context.Context, signedInUser *user.SignedInUser) ([]contactPoint, error) {
if !s.features.IsEnabledGlobally(featuremgmt.FlagOnPremToCloudMigrationsAlerts) {
return nil, nil
}
query := provisioning.ContactPointQuery{
OrgID: signedInUser.GetOrgID(),
Decrypt: true, // needed to recreate the settings in the target instance.
}
embeddedContactPoints, err := s.ngAlert.Api.ContactPointService.GetContactPoints(ctx, query, signedInUser)
if err != nil {
return nil, fmt.Errorf("fetching ngalert contact points: %w", err)
}
contactPoints := make([]contactPoint, 0, len(embeddedContactPoints))
for _, embeddedContactPoint := range embeddedContactPoints {
contactPoints = append(contactPoints, contactPoint{
UID: embeddedContactPoint.UID,
Name: embeddedContactPoint.Name,
Type: embeddedContactPoint.Type,
Settings: embeddedContactPoint.Settings,
DisableResolveMessage: embeddedContactPoint.DisableResolveMessage,
})
}
return contactPoints, nil
}
type notificationPolicy struct {
Name string
Routes definitions.Route
}
func (s *Service) getNotificationPolicies(ctx context.Context, signedInUser *user.SignedInUser) (notificationPolicy, error) {
if !s.features.IsEnabledGlobally(featuremgmt.FlagOnPremToCloudMigrationsAlerts) {
return notificationPolicy{}, nil
}
policyTree, _, err := s.ngAlert.Api.Policies.GetPolicyTree(ctx, signedInUser.GetOrgID())
if err != nil {
return notificationPolicy{}, fmt.Errorf("fetching ngalert notification policy tree: %w", err)
}
return notificationPolicy{
Name: "Notification Policy Tree",
Routes: policyTree,
}, nil
}