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
@@ -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
}