CloudMigrations: create snapshot for Notification Templates (#94676)

* CloudMigrations: create snapshot for Notification Templates

* CloudMigrations: add notification templates resource to frontend
This commit is contained in:
Matheus Macabu
2024-10-15 12:55:33 +02:00
committed by GitHub
parent f97f489c2c
commit 016dea1143
9 changed files with 100 additions and 6 deletions
@@ -799,9 +799,6 @@ func setUpServiceTest(t *testing.T, withDashboardMock bool) cloudmigration.Servi
require.NoError(t, err)
var validConfig = `{
"template_files": {
"a": "template"
},
"alertmanager_config": {
"route": {
"receiver": "grafana-default-email"
@@ -33,6 +33,7 @@ var currentMigrationTypes = []cloudmigration.MigrateDataType{
cloudmigration.LibraryElementDataType,
cloudmigration.DashboardDataType,
cloudmigration.MuteTimingType,
cloudmigration.NotificationTemplateType,
}
func (s *Service) getMigrationDataJSON(ctx context.Context, signedInUser *user.SignedInUser) (*cloudmigration.MigrateDataRequest, error) {
@@ -66,9 +67,17 @@ func (s *Service) getMigrationDataJSON(ctx context.Context, signedInUser *user.S
return nil, err
}
// Alerts: Notification Templates
notificationTemplates, err := s.getNotificationTemplates(ctx, signedInUser)
if err != nil {
s.log.Error("Failed to get alert notification templates", "err", err)
return nil, err
}
migrationDataSlice := make(
[]cloudmigration.MigrateDataRequestItem, 0,
len(dataSources)+len(dashs)+len(folders)+len(libraryElements)+len(muteTimings),
len(dataSources)+len(dashs)+len(folders)+len(libraryElements)+
len(muteTimings)+len(notificationTemplates),
)
for _, ds := range dataSources {
@@ -124,6 +133,15 @@ func (s *Service) getMigrationDataJSON(ctx context.Context, signedInUser *user.S
})
}
for _, notificationTemplate := range notificationTemplates {
migrationDataSlice = append(migrationDataSlice, cloudmigration.MigrateDataRequestItem{
Type: cloudmigration.NotificationTemplateType,
RefID: notificationTemplate.Name,
Name: notificationTemplate.Name,
Data: notificationTemplate,
})
}
// Obtain the names of parent elements for Dashboard and Folders data types
parentNamesByType, err := s.getParentNames(ctx, signedInUser, dashs, folders, libraryElements)
if err != nil {
@@ -39,3 +39,30 @@ func (s *Service) getAlertMuteTimings(ctx context.Context, signedInUser *user.Si
return muteTimeIntervals, nil
}
type notificationTemplate struct {
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{
Name: template.Name,
Template: template.Template,
})
}
return notificationTemplates, nil
}
@@ -42,6 +42,36 @@ func TestGetAlertMuteTimings(t *testing.T) {
})
}
func TestGetNotificationTemplates(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Run("when the feature flag `onPremToCloudMigrationsAlerts` is not enabled it returns nil", func(t *testing.T) {
s := setUpServiceTest(t, false).(*Service)
s.features = featuremgmt.WithFeatures(featuremgmt.FlagOnPremToCloudMigrations)
notificationTemplates, err := s.getNotificationTemplates(ctx, nil)
require.NoError(t, err)
require.Nil(t, notificationTemplates)
})
t.Run("when the feature flag `onPremToCloudMigrationsAlerts` is enabled it returns the notification templates", func(t *testing.T) {
s := setUpServiceTest(t, false).(*Service)
s.features = featuremgmt.WithFeatures(featuremgmt.FlagOnPremToCloudMigrations, featuremgmt.FlagOnPremToCloudMigrationsAlerts)
var orgID int64 = 1
user := &user.SignedInUser{OrgID: orgID}
createdTemplate := createNotificationTemplate(t, ctx, s, orgID)
notificationTemplates, err := s.getNotificationTemplates(ctx, user)
require.NoError(t, err)
require.NotNil(t, notificationTemplates)
require.Len(t, notificationTemplates, 1)
require.Equal(t, createdTemplate.Name, notificationTemplates[0].Name)
})
}
func createMuteTiming(t *testing.T, ctx context.Context, service *Service, orgID int64) definitions.MuteTimeInterval {
t.Helper()
@@ -67,3 +97,15 @@ func createMuteTiming(t *testing.T, ctx context.Context, service *Service, orgID
return createdTiming
}
func createNotificationTemplate(t *testing.T, ctx context.Context, service *Service, orgID int64) definitions.NotificationTemplate {
tmpl := definitions.NotificationTemplate{
Name: "MyTestNotificationTemplate",
Template: "This is a test template\n{{ .ExternalURL }}",
}
createdTemplate, err := service.ngAlert.Api.Templates.CreateTemplate(ctx, orgID, tmpl)
require.NoError(t, err)
return createdTemplate
}