CloudMigrations: create snapshot for Alert Rules (#95404)

* CloudMigrations: create snapshot for Alert Rules

* CloudMigrations: display Alert Rules in resource list summary
This commit is contained in:
Matheus Macabu
2024-10-25 17:20:05 +02:00
committed by GitHub
parent 6f4a9eb07b
commit b2de69d741
9 changed files with 167 additions and 16 deletions
@@ -3,11 +3,14 @@ package cloudmigrationimpl
import (
"context"
"fmt"
"time"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/featuremgmt"
ngalertapi "github.com/grafana/grafana/pkg/services/ngalert/api"
"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"
@@ -133,3 +136,60 @@ func (s *Service) getNotificationPolicies(ctx context.Context, signedInUser *use
Routes: policyTree,
}, nil
}
type alertRule struct {
Updated time.Time `json:"updated,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Record *definitions.Record `json:"record"`
NotificationSettings *definitions.AlertRuleNotificationSettings `json:"notification_settings"`
FolderUID string `json:"folderUID"`
RuleGroup string `json:"ruleGroup"`
NoDataState string `json:"noDataState"`
Condition string `json:"condition"`
UID string `json:"uid"`
Title string `json:"title"`
ExecErrState string `json:"execErrState"`
Data []definitions.AlertQuery `json:"data"`
ID int64 `json:"id"`
For model.Duration `json:"for"`
OrgID int64 `json:"orgID"`
IsPaused bool `json:"isPaused"`
}
func (s *Service) getAlertRules(ctx context.Context, signedInUser *user.SignedInUser) ([]alertRule, error) {
if !s.features.IsEnabledGlobally(featuremgmt.FlagOnPremToCloudMigrationsAlerts) {
return nil, nil
}
alertRules, _, err := s.ngAlert.Api.AlertRules.GetAlertRules(ctx, signedInUser)
if err != nil {
return nil, fmt.Errorf("fetching alert rules: %w", err)
}
provisionedAlertRules := make([]alertRule, 0, len(alertRules))
for _, rule := range alertRules {
provisionedAlertRules = append(provisionedAlertRules, alertRule{
ID: rule.ID,
UID: rule.UID,
OrgID: rule.OrgID,
FolderUID: rule.NamespaceUID,
RuleGroup: rule.RuleGroup,
Title: rule.Title,
For: model.Duration(rule.For),
Condition: rule.Condition,
Data: ngalertapi.ApiAlertQueriesFromAlertQueries(rule.Data),
Updated: rule.Updated,
NoDataState: rule.NoDataState.String(),
ExecErrState: rule.ExecErrState.String(),
Annotations: rule.Annotations,
Labels: rule.Labels,
IsPaused: rule.IsPaused,
NotificationSettings: ngalertapi.AlertRuleNotificationSettingsFromNotificationSettings(rule.NotificationSettings),
Record: ngalertapi.ApiRecordFromModelRecord(rule.Record),
})
}
return provisionedAlertRules, nil
}