5520f48153
Several niche bugs have surfaced as a result of the decrypt code Grafana uses in receivers API being different than what is used to decrypt secrets before sending to remote AM. Example: - Dingding notifier not abiding by new Patching added to local AM, thus causing missing url errors. * noop refactor to simplify decryptConfiguration * Move compat function package * Use new receiver models to encrypt/decrypt in remote AM
70 lines
2.2 KiB
Go
70 lines
2.2 KiB
Go
package notifier
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
alertingNotify "github.com/grafana/alerting/notify"
|
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
)
|
|
|
|
func PostableGrafanaReceiverToGrafanaIntegrationConfig(p *apimodels.PostableGrafanaReceiver) *alertingNotify.GrafanaIntegrationConfig {
|
|
return &alertingNotify.GrafanaIntegrationConfig{
|
|
UID: p.UID,
|
|
Name: p.Name,
|
|
Type: p.Type,
|
|
DisableResolveMessage: p.DisableResolveMessage,
|
|
Settings: json.RawMessage(p.Settings),
|
|
SecureSettings: p.SecureSettings,
|
|
}
|
|
}
|
|
|
|
func PostableApiReceiverToApiReceiver(r *apimodels.PostableApiReceiver) *alertingNotify.APIReceiver {
|
|
integrations := alertingNotify.GrafanaIntegrations{
|
|
Integrations: make([]*alertingNotify.GrafanaIntegrationConfig, 0, len(r.GrafanaManagedReceivers)),
|
|
}
|
|
for _, cfg := range r.GrafanaManagedReceivers {
|
|
integrations.Integrations = append(integrations.Integrations, PostableGrafanaReceiverToGrafanaIntegrationConfig(cfg))
|
|
}
|
|
|
|
return &alertingNotify.APIReceiver{
|
|
ConfigReceiver: r.Receiver,
|
|
GrafanaIntegrations: integrations,
|
|
}
|
|
}
|
|
|
|
func PostableApiAlertingConfigToApiReceivers(c apimodels.PostableApiAlertingConfig) []*alertingNotify.APIReceiver {
|
|
apiReceivers := make([]*alertingNotify.APIReceiver, 0, len(c.Receivers))
|
|
for _, receiver := range c.Receivers {
|
|
apiReceivers = append(apiReceivers, PostableApiReceiverToApiReceiver(receiver))
|
|
}
|
|
return apiReceivers
|
|
}
|
|
|
|
// Silence-specific compat functions to convert between grafana/alerting and model types.
|
|
|
|
func GettableSilenceToSilence(s alertingNotify.GettableSilence) *models.Silence {
|
|
sil := models.Silence(s)
|
|
return &sil
|
|
}
|
|
|
|
func GettableSilencesToSilences(silences alertingNotify.GettableSilences) []*models.Silence {
|
|
res := make([]*models.Silence, 0, len(silences))
|
|
for _, sil := range silences {
|
|
res = append(res, GettableSilenceToSilence(*sil))
|
|
}
|
|
return res
|
|
}
|
|
|
|
func SilenceToPostableSilence(s models.Silence) *alertingNotify.PostableSilence {
|
|
var id string
|
|
if s.ID != nil {
|
|
id = *s.ID
|
|
}
|
|
return &alertingNotify.PostableSilence{
|
|
ID: id,
|
|
Silence: s.Silence,
|
|
}
|
|
}
|