[wip] starts validation

This commit is contained in:
Owen Diehl
2021-02-23 16:48:27 -05:00
parent 5477076cb2
commit 87cabcda8d
4 changed files with 79 additions and 12 deletions
+4
View File
@@ -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"`
}
+12 -12
View File
@@ -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:"-"`
}
+34
View File
@@ -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
}
+29
View File
@@ -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))
}