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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 100 additions and 6 deletions

View File

@ -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"

View File

@ -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 {

View File

@ -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
}

View File

@ -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
}

View File

@ -221,6 +221,8 @@ function ResourceIcon({ resource }: { resource: ResourceTableItem }) {
return <Icon size="xl" name="library-panel" />;
case 'MUTE_TIMING':
return <Icon size="xl" name="bell-slash" />;
case 'NOTIFICATION_TEMPLATE':
return <Icon size="xl" name="gf-layout-simple" />;
default:
return undefined;
}

View File

@ -15,6 +15,8 @@ export function prettyTypeName(type: ResourceTableItem['type']) {
return t('migrate-to-cloud.resource-type.library_element', 'Library Element');
case 'MUTE_TIMING':
return t('migrate-to-cloud.resource-type.mute_timing', 'Mute Timing');
case 'NOTIFICATION_TEMPLATE':
return t('migrate-to-cloud.resource-type.notification_template', 'Notification Template');
default:
return t('migrate-to-cloud.resource-type.unknown', 'Unknown');
}

View File

@ -54,6 +54,8 @@ function getTranslatedMessage(snapshot: GetSnapshotResponseDto) {
types.push(t('migrate-to-cloud.migrated-counts.library_elements', 'library elements'));
} else if (type === 'MUTE_TIMING') {
types.push(t('migrate-to-cloud.migrated-counts.mute_timings', 'mute timings'));
} else if (type === 'NOTIFICATION_TEMPLATE') {
types.push(t('migrate-to-cloud.migrated-counts.notification_templates', 'notification templates'));
}
distinctItems += 1;

View File

@ -1409,7 +1409,8 @@
"datasources": "data sources",
"folders": "folders",
"library_elements": "library elements",
"mute_timings": "mute timings"
"mute_timings": "mute timings",
"notification_templates": "notification templates"
},
"migration-token": {
"delete-button": "Delete token",
@ -1494,6 +1495,7 @@
"folder": "Folder",
"library_element": "Library Element",
"mute_timing": "Mute Timing",
"notification_template": "Notification Template",
"unknown": "Unknown"
},
"summary": {

View File

@ -1409,7 +1409,8 @@
"datasources": "đäŧä şőūřčęş",
"folders": "ƒőľđęřş",
"library_elements": "ľįþřäřy ęľęmęʼnŧş",
"mute_timings": "mūŧę ŧįmįʼnģş"
"mute_timings": "mūŧę ŧįmįʼnģş",
"notification_templates": "ʼnőŧįƒįčäŧįőʼn ŧęmpľäŧęş"
},
"migration-token": {
"delete-button": "Đęľęŧę ŧőĸęʼn",
@ -1494,6 +1495,7 @@
"folder": "Főľđęř",
"library_element": "Ŀįþřäřy Ēľęmęʼnŧ",
"mute_timing": "Mūŧę Ŧįmįʼnģ",
"notification_template": "Ńőŧįƒįčäŧįőʼn Ŧęmpľäŧę",
"unknown": "Ůʼnĸʼnőŵʼn"
},
"summary": {