mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
This commit renames "Message templates" to "Notification templates" in the user interface as it suggests that these templates cannot be used to template anything other than the message. However, message templates are much more general and can be used to template other fields too such as the subject of an email, or the title of a Slack message.
126 lines
3.1 KiB
Go
126 lines
3.1 KiB
Go
package provisioning
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
)
|
|
|
|
type TemplateService struct {
|
|
config AMConfigStore
|
|
prov ProvisioningStore
|
|
xact TransactionManager
|
|
log log.Logger
|
|
}
|
|
|
|
func NewTemplateService(config AMConfigStore, prov ProvisioningStore, xact TransactionManager, log log.Logger) *TemplateService {
|
|
return &TemplateService{
|
|
config: config,
|
|
prov: prov,
|
|
xact: xact,
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
func (t *TemplateService) GetTemplates(ctx context.Context, orgID int64) (map[string]string, error) {
|
|
revision, err := getLastConfiguration(ctx, orgID, t.config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if revision.cfg.TemplateFiles == nil {
|
|
return map[string]string{}, nil
|
|
}
|
|
|
|
return revision.cfg.TemplateFiles, nil
|
|
}
|
|
|
|
func (t *TemplateService) SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error) {
|
|
err := tmpl.Validate()
|
|
if err != nil {
|
|
return definitions.NotificationTemplate{}, fmt.Errorf("%w: %s", ErrValidation, err.Error())
|
|
}
|
|
|
|
revision, err := getLastConfiguration(ctx, orgID, t.config)
|
|
if err != nil {
|
|
return definitions.NotificationTemplate{}, err
|
|
}
|
|
|
|
if revision.cfg.TemplateFiles == nil {
|
|
revision.cfg.TemplateFiles = map[string]string{}
|
|
}
|
|
revision.cfg.TemplateFiles[tmpl.Name] = tmpl.Template
|
|
|
|
serialized, err := serializeAlertmanagerConfig(*revision.cfg)
|
|
if err != nil {
|
|
return definitions.NotificationTemplate{}, err
|
|
}
|
|
cmd := models.SaveAlertmanagerConfigurationCmd{
|
|
AlertmanagerConfiguration: string(serialized),
|
|
ConfigurationVersion: revision.version,
|
|
FetchedConfigurationHash: revision.concurrencyToken,
|
|
Default: false,
|
|
OrgID: orgID,
|
|
}
|
|
err = t.xact.InTransaction(ctx, func(ctx context.Context) error {
|
|
err = PersistConfig(ctx, t.config, &cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = t.prov.SetProvenance(ctx, &tmpl, orgID, tmpl.Provenance)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return definitions.NotificationTemplate{}, err
|
|
}
|
|
|
|
return tmpl, nil
|
|
}
|
|
|
|
func (t *TemplateService) DeleteTemplate(ctx context.Context, orgID int64, name string) error {
|
|
revision, err := getLastConfiguration(ctx, orgID, t.config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
delete(revision.cfg.TemplateFiles, name)
|
|
|
|
serialized, err := serializeAlertmanagerConfig(*revision.cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cmd := models.SaveAlertmanagerConfigurationCmd{
|
|
AlertmanagerConfiguration: string(serialized),
|
|
ConfigurationVersion: revision.version,
|
|
FetchedConfigurationHash: revision.concurrencyToken,
|
|
Default: false,
|
|
OrgID: orgID,
|
|
}
|
|
err = t.xact.InTransaction(ctx, func(ctx context.Context) error {
|
|
err = PersistConfig(ctx, t.config, &cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tgt := definitions.NotificationTemplate{
|
|
Name: name,
|
|
}
|
|
err = t.prov.DeleteProvenance(ctx, &tgt, orgID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|