diff --git a/pkg/api/alertmanager.go b/pkg/api/alertmanager.go index 2e7f8f39e20..c54c7f5f8f2 100644 --- a/pkg/api/alertmanager.go +++ b/pkg/api/alertmanager.go @@ -179,5 +179,9 @@ type GrafanaReceiver models.CreateAlertNotificationCommand type ApiReceiver struct { config.Receiver + GrafanaReceivers +} + +type GrafanaReceivers struct { GrafanaManagedReceivers []*GrafanaReceiver `yaml:"grafana_managed_receiver_configs,omitempty" json:"grafana_managed_receiver_configs,omitempty"` } diff --git a/pkg/api/cortex-ruler.go b/pkg/api/cortex-ruler.go index 12b1bb76654..2495cbdf0ed 100644 --- a/pkg/api/cortex-ruler.go +++ b/pkg/api/cortex-ruler.go @@ -1,9 +1,9 @@ package api import ( + "github.com/grafana/grafana/pkg/services/ngalert" "github.com/grafana/grafana/pkg/services/ngalert/eval" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/pkg/rulefmt" ) // swagger:route Get /api/v1/rules ruler RouteGetRulesConfig @@ -99,9 +99,9 @@ type RuleGroupConfig struct { } type ApiRuleNode struct { - Record string `yaml:"record,omitempty" json:"record,omitempty"` - Alert string `yaml:"alert,omitempty" json:"alert,omitempty"` - Expr string `yaml:"expr" json:"expr"` + Record string `yaml:"record,omitempty" json:"record,omitempty"` + Alert string `yaml:"alert,omitempty" json:"alert,omitempty"` + Expr string `yaml:"expr" json:"expr"` For model.Duration `yaml:"for,omitempty" json:"for,omitempty"` Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"` Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"` @@ -114,17 +114,17 @@ type ExtendedRuleNode struct { } // UpsertAlertDefinitionCommand is copy of the unexported struct: -https://github.com/grafana/grafana/blob/debb82e12417e82a0e2bd09e1a450065f884c1bc/pkg/services/ngalert/models.go#L85 +// https://github.com/grafana/grafana/blob/debb82e12417e82a0e2bd09e1a450065f884c1bc/pkg/services/ngalert/models.go#L85 type UpsertAlertDefinitionCommand struct { - Title string `json:"title" yaml:"title"` + Title string `json:"title" yaml:"title"` // OrgID is an obsolete field (it will derive from the x-grafana-org-id header) - OrgID int64 `json:"-" yaml:"-"` + OrgID int64 `json:"-" yaml:"-"` // Condition is the refID of the query or expression to be evaluated - Condition string `json:"condition" yaml:"condition"` + Condition string `json:"condition" yaml:"condition"` // Data is an array of the queries and expressions - Data []eval.AlertQuery `json:"data" yaml:"data"` + Data []eval.AlertQuery `json:"data" yaml:"data"` // IntervalSeconds is an obsolete field (it will derive from the ruleGroup interval) - IntervalSeconds *int64 `json:"-" yaml:"-"` + IntervalSeconds *int64 `json:"-" yaml:"-"` // UID is set only for existing definitions UID string `json:"uid" yaml:"uid"` @@ -158,7 +158,7 @@ type ExtendedUpsertAlertDefinitionCommand struct { ExecutionErrorState ExecutionErrorState `json:"exec_err_state" yaml:"exec_err_state"` Settings map[string]interface{} `json:"settings" yaml:"settings"` // internal state - FolderUID string `json:"-" yaml:"-"` + FolderUID string `json:"-" yaml:"-"` DatasourceUIDs []string `json:"-" yaml:"-"` - RuleGroupUID string `json:"-" yaml:"-"` + RuleGroupUID string `json:"-" yaml:"-"` } diff --git a/pkg/validation/alertconfig.go b/pkg/validation/alertconfig.go new file mode 100644 index 00000000000..35f1fd5d3d5 --- /dev/null +++ b/pkg/validation/alertconfig.go @@ -0,0 +1,34 @@ +package validation + +import ( + "github.com/grafana/alerting-api/pkg/api" + "github.com/prometheus/alertmanager/config" +) + +// GrafanaAlertingConfig contains only the Grafana managed alerting configurations. +type GrafanaAlertingConfig struct { + api.GrafanaReceivers + Route *config.Route `yaml:"route,omitempty" json:"route,omitempty"` + Templates []string `yaml:"templates" json:"templates"` +} + +// func SplitAlertingConfig(apiConf api.ApiAlertingConfig) (amConfig config.Config, gConfig GrafanaAlertingConfig, err error) { + +// } + +// func MergeAlertingConfigs(amConfig config.Config, gConfig GrafanaAlertingConfig) (conf api.ApiAlertingConfig, err error) { +// } + +// Routing trees must either contain only Grafana Managed receiver types or AlertManager receiver types +// func SplitRoutes(routes []*config.Route, receivers map[string]*api.ApiReceiver) (gRoutes, amRoutes []*config.Route, err error) { +// for _, route := range routes { +// } +// } + +func allReceivers(route *config.Route) (res []string) { + res = append(res, route.Receiver) + for _, subRoute := range route.Routes { + res = append(res, allReceivers(subRoute)...) + } + return res +} diff --git a/pkg/validation/alertconfig_test.go b/pkg/validation/alertconfig_test.go new file mode 100644 index 00000000000..48f4f624314 --- /dev/null +++ b/pkg/validation/alertconfig_test.go @@ -0,0 +1,29 @@ +package validation + +import ( + "testing" + + "github.com/prometheus/alertmanager/config" + "github.com/stretchr/testify/require" +) + +func Test_allReceivers(t *testing.T) { + input := &config.Route{ + Receiver: "foo", + Routes: []*config.Route{ + { + Receiver: "bar", + Routes: []*config.Route{ + { + Receiver: "bazz", + }, + }, + }, + { + Receiver: "buzz", + }, + }, + } + + require.Equal(t, []string{"foo", "bar", "bazz", "buzz"}, allReceivers(input)) +}