mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Add GetTemplate to template service and update tests (#91854)
* add GetTemplate to template service * refactor GetTemplates to fetch all provenances at once * refactor tests
This commit is contained in:
parent
868f9320e9
commit
5834981f86
@ -45,6 +45,7 @@ type ContactPointService interface {
|
||||
|
||||
type TemplateService interface {
|
||||
GetTemplates(ctx context.Context, orgID int64) ([]definitions.NotificationTemplate, error)
|
||||
GetTemplate(ctx context.Context, orgID int64, name string) (definitions.NotificationTemplate, error)
|
||||
SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error)
|
||||
DeleteTemplate(ctx context.Context, orgID int64, name string, provenance definitions.Provenance, version string) error
|
||||
}
|
||||
|
@ -271,7 +271,7 @@ func createTestRoutingTree() definitions.Route {
|
||||
}
|
||||
|
||||
func createTestAlertingConfig() *definitions.PostableUserConfig {
|
||||
cfg, _ := legacy_storage.DeserializeAlertmanagerConfig([]byte(defaultConfig))
|
||||
cfg, _ := legacy_storage.DeserializeAlertmanagerConfig([]byte(setting.GetAlertmanagerDefaultConfiguration()))
|
||||
cfg.AlertmanagerConfig.Receivers = append(cfg.AlertmanagerConfig.Receivers,
|
||||
&definitions.PostableApiReceiver{
|
||||
Receiver: config.Receiver{
|
||||
|
@ -36,6 +36,15 @@ func (t *TemplateService) GetTemplates(ctx context.Context, orgID int64) ([]defi
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(revision.Config.TemplateFiles) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
provenances, err := t.provenanceStore.GetProvenances(ctx, orgID, (&definitions.NotificationTemplate{}).ResourceType())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
templates := make([]definitions.NotificationTemplate, 0, len(revision.Config.TemplateFiles))
|
||||
for name, tmpl := range revision.Config.TemplateFiles {
|
||||
tmpl := definitions.NotificationTemplate{
|
||||
@ -43,17 +52,41 @@ func (t *TemplateService) GetTemplates(ctx context.Context, orgID int64) ([]defi
|
||||
Template: tmpl,
|
||||
ResourceVersion: calculateTemplateFingerprint(tmpl),
|
||||
}
|
||||
provenance, ok := provenances[tmpl.ResourceID()]
|
||||
if !ok {
|
||||
provenance = models.ProvenanceNone
|
||||
}
|
||||
tmpl.Provenance = definitions.Provenance(provenance)
|
||||
templates = append(templates, tmpl)
|
||||
}
|
||||
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
func (t *TemplateService) GetTemplate(ctx context.Context, orgID int64, name string) (definitions.NotificationTemplate, error) {
|
||||
revision, err := t.configStore.Get(ctx, orgID)
|
||||
if err != nil {
|
||||
return definitions.NotificationTemplate{}, err
|
||||
}
|
||||
|
||||
for tmplName, tmpl := range revision.Config.TemplateFiles {
|
||||
if tmplName != name {
|
||||
continue
|
||||
}
|
||||
tmpl := definitions.NotificationTemplate{
|
||||
Name: name,
|
||||
Template: tmpl,
|
||||
ResourceVersion: calculateTemplateFingerprint(tmpl),
|
||||
}
|
||||
|
||||
provenance, err := t.provenanceStore.GetProvenance(ctx, &tmpl, orgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return definitions.NotificationTemplate{}, err
|
||||
}
|
||||
tmpl.Provenance = definitions.Provenance(provenance)
|
||||
|
||||
templates = append(templates, tmpl)
|
||||
return tmpl, nil
|
||||
}
|
||||
|
||||
return templates, nil
|
||||
return definitions.NotificationTemplate{}, ErrTemplateNotFound.Errorf("")
|
||||
}
|
||||
|
||||
func (t *TemplateService) SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error) {
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user