mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Remove dependency on alerting package in definitions (#65390)
* move export rules to definitions package * move provisioning contact point methods to provisioning package * move AlertRuleGroupWithFolderTitle to ngalert models and adapter functions to api's compat * move rule_types files back to where they were before.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
|
||||
"github.com/grafana/grafana/pkg/services/provisioning/values"
|
||||
)
|
||||
|
||||
@@ -94,7 +95,7 @@ func (config *ReceiverV1) mapToModel(name string) (definitions.EmbeddedContactPo
|
||||
}
|
||||
// As the values are not encrypted when coming from disk files,
|
||||
// we can simply return the fallback for validation.
|
||||
err := cp.Valid(func(_ context.Context, _ map[string][]byte, _, fallback string) string {
|
||||
err := provisioning.ValidateContactPoint(cp, func(_ context.Context, _ map[string][]byte, _, fallback string) string {
|
||||
return fallback
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package file
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -31,11 +31,11 @@ type AlertRuleGroupV1 struct {
|
||||
Rules []AlertRuleV1 `json:"rules" yaml:"rules"`
|
||||
}
|
||||
|
||||
func (ruleGroupV1 *AlertRuleGroupV1) MapToModel() (AlertRuleGroupWithFolderTitle, error) {
|
||||
ruleGroup := AlertRuleGroupWithFolderTitle{AlertRuleGroup: &models.AlertRuleGroup{}}
|
||||
func (ruleGroupV1 *AlertRuleGroupV1) MapToModel() (models.AlertRuleGroupWithFolderTitle, error) {
|
||||
ruleGroup := models.AlertRuleGroupWithFolderTitle{AlertRuleGroup: &models.AlertRuleGroup{}}
|
||||
ruleGroup.Title = ruleGroupV1.Name.Value()
|
||||
if strings.TrimSpace(ruleGroup.Title) == "" {
|
||||
return AlertRuleGroupWithFolderTitle{}, errors.New("rule group has no name set")
|
||||
return models.AlertRuleGroupWithFolderTitle{}, errors.New("rule group has no name set")
|
||||
}
|
||||
ruleGroup.OrgID = ruleGroupV1.OrgID.Value()
|
||||
if ruleGroup.OrgID < 1 {
|
||||
@@ -43,29 +43,23 @@ func (ruleGroupV1 *AlertRuleGroupV1) MapToModel() (AlertRuleGroupWithFolderTitle
|
||||
}
|
||||
interval, err := model.ParseDuration(ruleGroupV1.Interval.Value())
|
||||
if err != nil {
|
||||
return AlertRuleGroupWithFolderTitle{}, err
|
||||
return models.AlertRuleGroupWithFolderTitle{}, err
|
||||
}
|
||||
ruleGroup.Interval = int64(time.Duration(interval).Seconds())
|
||||
ruleGroup.FolderTitle = ruleGroupV1.Folder.Value()
|
||||
if strings.TrimSpace(ruleGroup.FolderTitle) == "" {
|
||||
return AlertRuleGroupWithFolderTitle{}, errors.New("rule group has no folder set")
|
||||
return models.AlertRuleGroupWithFolderTitle{}, errors.New("rule group has no folder set")
|
||||
}
|
||||
for _, ruleV1 := range ruleGroupV1.Rules {
|
||||
rule, err := ruleV1.mapToModel(ruleGroup.OrgID)
|
||||
if err != nil {
|
||||
return AlertRuleGroupWithFolderTitle{}, err
|
||||
return models.AlertRuleGroupWithFolderTitle{}, err
|
||||
}
|
||||
ruleGroup.Rules = append(ruleGroup.Rules, rule)
|
||||
}
|
||||
return ruleGroup, nil
|
||||
}
|
||||
|
||||
type AlertRuleGroupWithFolderTitle struct {
|
||||
*models.AlertRuleGroup
|
||||
OrgID int64
|
||||
FolderTitle string
|
||||
}
|
||||
|
||||
type AlertRuleV1 struct {
|
||||
UID values.StringValue `json:"uid" yaml:"uid"`
|
||||
Title values.StringValue `json:"title" yaml:"title"`
|
||||
@@ -175,132 +169,3 @@ func (queryV1 *QueryV1) mapToModel() (models.AlertQuery, error) {
|
||||
Model: rawMessage,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Response structs
|
||||
|
||||
// AlertingFileExport is the full provisioned file export.
|
||||
// swagger:model
|
||||
type AlertingFileExport struct {
|
||||
APIVersion int64 `json:"apiVersion" yaml:"apiVersion"`
|
||||
Groups []AlertRuleGroupExport `json:"groups" yaml:"groups"`
|
||||
}
|
||||
|
||||
// AlertRuleGroupExport is the provisioned file export of AlertRuleGroupV1.
|
||||
type AlertRuleGroupExport struct {
|
||||
OrgID int64 `json:"orgId" yaml:"orgId"`
|
||||
Name string `json:"name" yaml:"name"`
|
||||
Folder string `json:"folder" yaml:"folder"`
|
||||
Interval model.Duration `json:"interval" yaml:"interval"`
|
||||
Rules []AlertRuleExport `json:"rules" yaml:"rules"`
|
||||
}
|
||||
|
||||
// AlertRuleExport is the provisioned file export of models.AlertRule.
|
||||
type AlertRuleExport struct {
|
||||
UID string `json:"uid" yaml:"uid"`
|
||||
Title string `json:"title" yaml:"title"`
|
||||
Condition string `json:"condition" yaml:"condition"`
|
||||
Data []AlertQueryExport `json:"data" yaml:"data"`
|
||||
DashboardUID string `json:"dasboardUid,omitempty" yaml:"dashboardUid,omitempty"`
|
||||
PanelID int64 `json:"panelId,omitempty" yaml:"panelId,omitempty"`
|
||||
NoDataState models.NoDataState `json:"noDataState" yaml:"noDataState"`
|
||||
ExecErrState models.ExecutionErrorState `json:"execErrState" yaml:"execErrState"`
|
||||
For model.Duration `json:"for" yaml:"for"`
|
||||
Annotations map[string]string `json:"annotations,omitempty" yaml:"annotations,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
|
||||
IsPaused bool `json:"isPaused" yaml:"isPaused"`
|
||||
}
|
||||
|
||||
// AlertQueryExport is the provisioned export of models.AlertQuery.
|
||||
type AlertQueryExport struct {
|
||||
RefID string `json:"refId" yaml:"refId"`
|
||||
QueryType string `json:"queryType,omitempty" yaml:"queryType,omitempty"`
|
||||
RelativeTimeRange models.RelativeTimeRange `json:"relativeTimeRange,omitempty" yaml:"relativeTimeRange,omitempty"`
|
||||
DatasourceUID string `json:"datasourceUid" yaml:"datasourceUid"`
|
||||
Model map[string]interface{} `json:"model" yaml:"model"`
|
||||
}
|
||||
|
||||
// NewAlertingFileExport creates an AlertingFileExport DTO from []AlertRuleGroupWithFolderTitle.
|
||||
func NewAlertingFileExport(groups []AlertRuleGroupWithFolderTitle) (AlertingFileExport, error) {
|
||||
f := AlertingFileExport{APIVersion: 1}
|
||||
for _, group := range groups {
|
||||
export, err := newAlertRuleGroupExport(group)
|
||||
if err != nil {
|
||||
return AlertingFileExport{}, err
|
||||
}
|
||||
f.Groups = append(f.Groups, export)
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// newAlertRuleGroupExport creates a AlertRuleGroupExport DTO from models.AlertRuleGroup.
|
||||
func newAlertRuleGroupExport(d AlertRuleGroupWithFolderTitle) (AlertRuleGroupExport, error) {
|
||||
rules := make([]AlertRuleExport, 0, len(d.Rules))
|
||||
for i := range d.Rules {
|
||||
alert, err := newAlertRuleExport(d.Rules[i])
|
||||
if err != nil {
|
||||
return AlertRuleGroupExport{}, err
|
||||
}
|
||||
rules = append(rules, alert)
|
||||
}
|
||||
return AlertRuleGroupExport{
|
||||
OrgID: d.OrgID,
|
||||
Name: d.Title,
|
||||
Folder: d.FolderTitle,
|
||||
Interval: model.Duration(time.Duration(d.Interval) * time.Second),
|
||||
Rules: rules,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// newAlertRuleExport creates a AlertRuleExport DTO from models.AlertRule.
|
||||
func newAlertRuleExport(rule models.AlertRule) (AlertRuleExport, error) {
|
||||
data := make([]AlertQueryExport, 0, len(rule.Data))
|
||||
for i := range rule.Data {
|
||||
query, err := newAlertQueryExport(rule.Data[i])
|
||||
if err != nil {
|
||||
return AlertRuleExport{}, err
|
||||
}
|
||||
data = append(data, query)
|
||||
}
|
||||
|
||||
var dashboardUID string
|
||||
if rule.DashboardUID != nil {
|
||||
dashboardUID = *rule.DashboardUID
|
||||
}
|
||||
|
||||
var panelID int64
|
||||
if rule.PanelID != nil {
|
||||
panelID = *rule.PanelID
|
||||
}
|
||||
|
||||
return AlertRuleExport{
|
||||
UID: rule.UID,
|
||||
Title: rule.Title,
|
||||
For: model.Duration(rule.For),
|
||||
Condition: rule.Condition,
|
||||
Data: data,
|
||||
DashboardUID: dashboardUID,
|
||||
PanelID: panelID,
|
||||
NoDataState: rule.NoDataState,
|
||||
ExecErrState: rule.ExecErrState,
|
||||
Annotations: rule.Annotations,
|
||||
Labels: rule.Labels,
|
||||
IsPaused: rule.IsPaused,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// newAlertQueryExport creates a AlertQueryExport DTO from models.AlertQuery.
|
||||
func newAlertQueryExport(query models.AlertQuery) (AlertQueryExport, error) {
|
||||
// We unmarshal the json.RawMessage model into a map in order to facilitate yaml marshalling.
|
||||
var mdl map[string]interface{}
|
||||
err := json.Unmarshal(query.Model, &mdl)
|
||||
if err != nil {
|
||||
return AlertQueryExport{}, err
|
||||
}
|
||||
return AlertQueryExport{
|
||||
RefID: query.RefID,
|
||||
QueryType: query.QueryType,
|
||||
RelativeTimeRange: query.RelativeTimeRange,
|
||||
DatasourceUID: query.DatasourceUID,
|
||||
Model: mdl,
|
||||
}, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package file
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@@ -3,7 +3,7 @@ package alerting
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/provisioning/alerting/file"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/provisioning/values"
|
||||
)
|
||||
|
||||
@@ -16,8 +16,8 @@ type OrgID int64
|
||||
type AlertingFile struct {
|
||||
configVersion
|
||||
Filename string
|
||||
Groups []file.AlertRuleGroupWithFolderTitle
|
||||
DeleteRules []file.RuleDelete
|
||||
Groups []models.AlertRuleGroupWithFolderTitle
|
||||
DeleteRules []RuleDelete
|
||||
ContactPoints []ContactPoint
|
||||
DeleteContactPoints []DeleteContactPoint
|
||||
Policies []NotificiationPolicy
|
||||
@@ -31,8 +31,8 @@ type AlertingFile struct {
|
||||
type AlertingFileV1 struct {
|
||||
configVersion
|
||||
Filename string
|
||||
Groups []file.AlertRuleGroupV1 `json:"groups" yaml:"groups"`
|
||||
DeleteRules []file.RuleDeleteV1 `json:"deleteRules" yaml:"deleteRules"`
|
||||
Groups []AlertRuleGroupV1 `json:"groups" yaml:"groups"`
|
||||
DeleteRules []RuleDeleteV1 `json:"deleteRules" yaml:"deleteRules"`
|
||||
ContactPoints []ContactPointV1 `json:"contactPoints" yaml:"contactPoints"`
|
||||
DeleteContactPoints []DeleteContactPointV1 `json:"deleteContactPoints" yaml:"deleteContactPoints"`
|
||||
Policies []NotificiationPolicyV1 `json:"policies" yaml:"policies"`
|
||||
@@ -133,7 +133,7 @@ func (fileV1 *AlertingFileV1) mapRules(alertingFile *AlertingFile) error {
|
||||
if orgID < 1 {
|
||||
orgID = 1
|
||||
}
|
||||
ruleDelete := file.RuleDelete{
|
||||
ruleDelete := RuleDelete{
|
||||
UID: ruleDeleteV1.UID.Value(),
|
||||
OrgID: orgID,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user