This commit is contained in:
Matt Jacobson
2026-01-07 12:00:49 -05:00
parent d220d765b8
commit 0fa9f3a247
23 changed files with 898 additions and 283 deletions
+24 -6
View File
@@ -20,6 +20,7 @@ import (
"github.com/grafana/grafana/pkg/services/ngalert/api/hcl"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
alerting_models "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/notifier/legacy_storage"
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/util"
@@ -58,6 +59,8 @@ type NotificationPolicyService interface {
GetPolicyTree(ctx context.Context, orgID int64) (definitions.Route, string, error)
UpdatePolicyTree(ctx context.Context, orgID int64, tree definitions.Route, p alerting_models.Provenance, version string) (definitions.Route, string, error)
ResetPolicyTree(ctx context.Context, orgID int64, provenance alerting_models.Provenance) (definitions.Route, error)
GetManagedRoute(ctx context.Context, orgID int64, name string) (legacy_storage.ManagedRoute, error)
}
type MuteTimingService interface {
@@ -96,15 +99,27 @@ func (srv *ProvisioningSrv) RouteGetPolicyTree(c *contextmodel.ReqContext) respo
}
func (srv *ProvisioningSrv) RouteGetPolicyTreeExport(c *contextmodel.ReqContext) response.Response {
policies, _, err := srv.policies.GetPolicyTree(c.Req.Context(), c.GetOrgID())
if err != nil {
if errors.Is(err, store.ErrNoAlertmanagerConfiguration) {
return ErrResp(http.StatusNotFound, err, "")
routeName := c.Query("routeName")
var policy definitions.Route
if routeName == "" {
var err error
policy, _, err = srv.policies.GetPolicyTree(c.Req.Context(), c.GetOrgID())
if err != nil {
if errors.Is(err, store.ErrNoAlertmanagerConfiguration) {
return ErrResp(http.StatusNotFound, err, "")
}
return ErrResp(http.StatusInternalServerError, err, "")
}
return ErrResp(http.StatusInternalServerError, err, "")
} else {
managedRoute, err := srv.policies.GetManagedRoute(c.Req.Context(), c.GetOrgID(), routeName)
if err != nil {
return response.ErrOrFallback(http.StatusInternalServerError, "failed to export notification policy tree", err)
}
policy = managedRoute.AsAMRoute()
}
e, err := AlertingFileExportFromRoute(c.GetOrgID(), policies)
e, err := AlertingFileExportFromRoute(c.GetOrgID(), routeName, policy)
if err != nil {
return ErrResp(http.StatusInternalServerError, err, "failed to create alerting file export")
}
@@ -593,6 +608,9 @@ func escapeAlertingFileExport(body definitions.AlertingFileExport) definitions.A
}
func escapeRouteExport(r *definitions.RouteExport) {
if r.Name != nil {
r.Name = util.Pointer(addEscapeCharactersToString(*r.Name))
}
r.Receiver = addEscapeCharactersToString(r.Receiver)
if r.GroupByStr != nil {
groupByStr := make([]string, len(*r.GroupByStr))
@@ -2248,6 +2248,7 @@ func createTestRequestCtx() contextmodel.ReqContext {
}
type fakeNotificationPolicyService struct {
NotificationPolicyService
tree definitions.Route
prov models.Provenance
}
@@ -2318,7 +2319,9 @@ func (f *fakeNotificationPolicyService) ResetPolicyTree(ctx context.Context, org
return f.tree, nil
}
type fakeFailingNotificationPolicyService struct{}
type fakeFailingNotificationPolicyService struct {
NotificationPolicyService
}
func (f *fakeFailingNotificationPolicyService) GetPolicyTree(ctx context.Context, orgID int64) (definitions.Route, string, error) {
return definitions.Route{}, "", fmt.Errorf("something went wrong")
@@ -2332,7 +2335,9 @@ func (f *fakeFailingNotificationPolicyService) ResetPolicyTree(ctx context.Conte
return definitions.Route{}, fmt.Errorf("something went wrong")
}
type fakeRejectingNotificationPolicyService struct{}
type fakeRejectingNotificationPolicyService struct {
NotificationPolicyService
}
type fakeRejectingNotificationSettingsValidatorProvider struct{}
+4 -2
View File
@@ -327,12 +327,14 @@ func ReceiverExportFromEmbeddedContactPoint(contact definitions.EmbeddedContactP
}
// AlertingFileExportFromRoute creates a definitions.AlertingFileExport DTO from definitions.Route.
func AlertingFileExportFromRoute(orgID int64, route definitions.Route) (definitions.AlertingFileExport, error) {
func AlertingFileExportFromRoute(orgID int64, name string, route definitions.Route) (definitions.AlertingFileExport, error) {
export := RouteExportFromRoute(&route)
export.Name = OmitDefault(util.Pointer(name))
f := definitions.AlertingFileExport{
APIVersion: 1,
Policies: []definitions.NotificationPolicyExport{{
OrgID: orgID,
RouteExport: RouteExportFromRoute(&route),
RouteExport: export,
}},
}
return f, nil
@@ -775,10 +775,11 @@ func fromPrometheusConfig(prometheusConfig config.Config) PostableApiAlertingCon
// swagger:model
type PostableUserConfig struct {
TemplateFiles map[string]string `yaml:"template_files" json:"template_files"`
AlertmanagerConfig PostableApiAlertingConfig `yaml:"alertmanager_config" json:"alertmanager_config"`
ExtraConfigs []ExtraConfiguration `yaml:"extra_config,omitempty" json:"extra_config,omitempty"`
amSimple map[string]interface{} `yaml:"-" json:"-"`
TemplateFiles map[string]string `yaml:"template_files" json:"template_files"`
AlertmanagerConfig PostableApiAlertingConfig `yaml:"alertmanager_config" json:"alertmanager_config"`
ExtraConfigs []ExtraConfiguration `yaml:"extra_config,omitempty" json:"extra_config,omitempty"`
ManagedRoutes map[string]*definition.Route `yaml:"managed_routes,omitempty" json:"managed_routes,omitempty"` // TODO: Move to ConfigRevision?
amSimple map[string]interface{} `yaml:"-" json:"-"`
}
func (c *PostableUserConfig) GetMergedAlertmanagerConfig() (MergeResult, error) {
@@ -70,7 +70,8 @@ type NotificationPolicyExport struct {
// RouteExport is the provisioned file export of definitions.Route. This is needed to hide fields that aren't useable in
// provisioning file format. An alternative would be to define a custom MarshalJSON and MarshalYAML that excludes them.
type RouteExport struct {
Receiver string `yaml:"receiver,omitempty" json:"receiver,omitempty" hcl:"contact_point"`
Name *string `yaml:"name,omitempty" json:"name,omitempty" hcl:"name,optional"`
Receiver string `yaml:"receiver,omitempty" json:"receiver,omitempty" hcl:"contact_point"`
GroupByStr *[]string `yaml:"group_by,omitempty" json:"group_by,omitempty" hcl:"group_by"`
// Deprecated. Remove before v1.0 release.