The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/services/ngalert/api/tooling/definitions/alertmanager_validation.go

51 lines
1.1 KiB

package definitions
import (
"fmt"
"regexp"
"strings"
"github.com/grafana/alerting/templates"
"gopkg.in/yaml.v3"
)
func (t *NotificationTemplate) Validate() error {
if t.Name == "" {
return fmt.Errorf("template must have a name")
}
if t.Template == "" {
return fmt.Errorf("template must have content")
}
content := strings.TrimSpace(t.Template)
found, err := regexp.MatchString(`\{\{\s*define`, content)
if err != nil {
return fmt.Errorf("failed to match regex: %w", err)
}
if !found {
lines := strings.Split(content, "\n")
for i, s := range lines {
lines[i] = " " + s
}
content = strings.Join(lines, "\n")
content = fmt.Sprintf("{{ define \"%s\" }}\n%s\n{{ end }}", t.Name, content)
}
t.Template = content
def := templates.TemplateDefinition{
Name: t.Name,
Template: t.Template,
Kind: templates.GrafanaKind,
}
return def.Validate()
}
func (mt *MuteTimeInterval) Validate() error {
s, err := yaml.Marshal(mt.MuteTimeInterval)
if err != nil {
return err
}
if err = yaml.Unmarshal(s, &(mt.MuteTimeInterval)); err != nil {
return err
}
return nil
}