Alerting: Support for imported Templates (#114196)
* refactor template service to contstruct notification template in one place, get provenance before creating and calculate resource version after. * refactor get by UID and name * introduce template kind in NotificationTemplate * introduce includeImported flag and use in the k8s api * support imported templates * add kind to template uid * tests for imported templates * update API model * set kind to default templates
This commit is contained in:
@@ -5,7 +5,8 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/grafana/alerting/templates"
|
||||
"github.com/grafana/alerting/definition"
|
||||
"github.com/grafana/alerting/notify"
|
||||
"go.yaml.in/yaml/v3"
|
||||
)
|
||||
|
||||
@@ -31,11 +32,18 @@ func (t *NotificationTemplate) Validate() error {
|
||||
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,
|
||||
if t.Kind == "" {
|
||||
t.Kind = definition.GrafanaTemplateKind
|
||||
}
|
||||
postable := definition.PostableApiTemplate{
|
||||
Name: t.Name,
|
||||
Content: t.Template,
|
||||
Kind: t.Kind,
|
||||
}
|
||||
if err := postable.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
def := notify.PostableAPITemplateToTemplateDefinition(postable)
|
||||
return def.Validate()
|
||||
}
|
||||
|
||||
|
||||
@@ -462,6 +462,16 @@ func TestValidateNotificationTemplates(t *testing.T) {
|
||||
},
|
||||
expContent: `{{ define "Alert Instance Template" }}\nFiring: {{ .Labels.alertname }}\nSilence: {{ .SilenceURL }}\n{{ end }}[what is this?]`,
|
||||
},
|
||||
{
|
||||
name: "unknown template kind",
|
||||
template: NotificationTemplate{
|
||||
Name: "Alert Instance Template",
|
||||
Template: `{{ define "Same name as definition" }}\nFiring: {{ .Labels.alertname }}\nSilence: {{ .SilenceURL }}\n{{ end }}`,
|
||||
Provenance: "test",
|
||||
Kind: "unknown",
|
||||
},
|
||||
expError: errors.New("unknown template kind: unknown"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tc {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package definitions
|
||||
|
||||
import "github.com/grafana/alerting/definition"
|
||||
|
||||
// swagger:route GET /v1/provisioning/templates provisioning stable RouteGetTemplates
|
||||
//
|
||||
// Get all notification template groups.
|
||||
@@ -55,11 +57,12 @@ type RouteDeleteTemplateParam struct {
|
||||
|
||||
// swagger:model
|
||||
type NotificationTemplate struct {
|
||||
UID string `json:"-" yaml:"-"`
|
||||
Name string `json:"name"`
|
||||
Template string `json:"template"`
|
||||
Provenance Provenance `json:"provenance,omitempty"`
|
||||
ResourceVersion string `json:"version,omitempty"`
|
||||
UID string `json:"-" yaml:"-"`
|
||||
Name string `json:"name"`
|
||||
Template string `json:"template"`
|
||||
Provenance Provenance `json:"provenance,omitempty"`
|
||||
ResourceVersion string `json:"version,omitempty"`
|
||||
Kind definition.TemplateKind `json:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
// swagger:model
|
||||
|
||||
Reference in New Issue
Block a user