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:
Yuri Tseretyan
2023-03-29 13:34:59 -04:00
committed by GitHub
parent d23ebf63d2
commit 9eaffdf5a8
11 changed files with 233 additions and 238 deletions
@@ -4,8 +4,6 @@ import (
"time"
"github.com/prometheus/common/model"
"github.com/grafana/grafana/pkg/services/provisioning/alerting/file"
)
// swagger:route GET /api/v1/provisioning/alert-rules provisioning stable RouteGetAlertRules
@@ -217,4 +215,41 @@ type AlertRuleGroup struct {
// AlertingFileExport is the full provisioned file export.
// swagger:model
type AlertingFileExport = file.AlertingFileExport
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 NoDataState `json:"noDataState" yaml:"noDataState"`
ExecErrState 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 RelativeTimeRange `json:"relativeTimeRange,omitempty" yaml:"relativeTimeRange,omitempty"`
DatasourceUID string `json:"datasourceUid" yaml:"datasourceUid"`
Model map[string]interface{} `json:"model" yaml:"model"`
}
@@ -1,15 +1,7 @@
package definitions
import (
"fmt"
"github.com/grafana/alerting/logging"
alertingNotify "github.com/grafana/alerting/notify"
"github.com/grafana/alerting/receivers"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config"
"github.com/grafana/grafana/pkg/setting"
)
// swagger:route GET /api/v1/provisioning/contact-points provisioning stable RouteGetContactpoints
@@ -101,63 +93,6 @@ type EmbeddedContactPoint struct {
const RedactedValue = "[REDACTED]"
func (e *EmbeddedContactPoint) Valid(decryptFunc receivers.GetDecryptedValueFn) error {
if e.Type == "" {
return fmt.Errorf("type should not be an empty string")
}
if e.Settings == nil {
return fmt.Errorf("settings should not be empty")
}
factory, exists := alertingNotify.Factory(e.Type)
if !exists {
return fmt.Errorf("unknown type '%s'", e.Type)
}
jsonBytes, err := e.Settings.MarshalJSON()
if err != nil {
return err
}
cfg, _ := receivers.NewFactoryConfig(&receivers.NotificationChannelConfig{
Settings: jsonBytes,
Type: e.Type,
}, nil, decryptFunc, nil, nil, func(ctx ...interface{}) logging.Logger {
return &logging.FakeLogger{}
}, setting.BuildVersion)
if _, err := factory(cfg); err != nil {
return err
}
return nil
}
func (e *EmbeddedContactPoint) SecretKeys() ([]string, error) {
notifiers := channels_config.GetAvailableNotifiers()
for _, n := range notifiers {
if n.Type == e.Type {
secureFields := []string{}
for _, field := range n.Options {
if field.Secure {
secureFields = append(secureFields, field.PropertyName)
}
}
return secureFields, nil
}
}
return nil, fmt.Errorf("no secrets configured for type '%s'", e.Type)
}
func (e *EmbeddedContactPoint) ExtractSecrets() (map[string]string, error) {
secrets := map[string]string{}
secretKeys, err := e.SecretKeys()
if err != nil {
return nil, err
}
for _, secretKey := range secretKeys {
secretValue := e.Settings.Get(secretKey).MustString()
e.Settings.Del(secretKey)
secrets[secretKey] = secretValue
}
return secrets, nil
}
func (e *EmbeddedContactPoint) ResourceID() string {
return e.UID
}