mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Return provenance of notification templates (#82274)
This commit is contained in:
parent
4b67ac117f
commit
ba63e62311
@ -38,7 +38,7 @@ type ContactPointService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TemplateService interface {
|
type TemplateService interface {
|
||||||
GetTemplates(ctx context.Context, orgID int64) (map[string]string, error)
|
GetTemplates(ctx context.Context, orgID int64) ([]definitions.NotificationTemplate, error)
|
||||||
SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error)
|
SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error)
|
||||||
DeleteTemplate(ctx context.Context, orgID int64, name string) error
|
DeleteTemplate(ctx context.Context, orgID int64, name string) error
|
||||||
}
|
}
|
||||||
@ -201,11 +201,7 @@ func (srv *ProvisioningSrv) RouteGetTemplates(c *contextmodel.ReqContext) respon
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrResp(http.StatusInternalServerError, err, "")
|
return ErrResp(http.StatusInternalServerError, err, "")
|
||||||
}
|
}
|
||||||
result := make([]definitions.NotificationTemplate, 0, len(templates))
|
return response.JSON(http.StatusOK, templates)
|
||||||
for k, v := range templates {
|
|
||||||
result = append(result, definitions.NotificationTemplate{Name: k, Template: v})
|
|
||||||
}
|
|
||||||
return response.JSON(http.StatusOK, result)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *ProvisioningSrv) RouteGetTemplate(c *contextmodel.ReqContext, name string) response.Response {
|
func (srv *ProvisioningSrv) RouteGetTemplate(c *contextmodel.ReqContext, name string) response.Response {
|
||||||
@ -213,8 +209,10 @@ func (srv *ProvisioningSrv) RouteGetTemplate(c *contextmodel.ReqContext, name st
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return ErrResp(http.StatusInternalServerError, err, "")
|
return ErrResp(http.StatusInternalServerError, err, "")
|
||||||
}
|
}
|
||||||
if tmpl, ok := templates[name]; ok {
|
for _, tmpl := range templates {
|
||||||
return response.JSON(http.StatusOK, definitions.NotificationTemplate{Name: name, Template: tmpl})
|
if tmpl.Name == name {
|
||||||
|
return response.JSON(http.StatusOK, tmpl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return response.Empty(http.StatusNotFound)
|
return response.Empty(http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
@ -25,17 +25,27 @@ func NewTemplateService(config AMConfigStore, prov ProvisioningStore, xact Trans
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TemplateService) GetTemplates(ctx context.Context, orgID int64) (map[string]string, error) {
|
func (t *TemplateService) GetTemplates(ctx context.Context, orgID int64) ([]definitions.NotificationTemplate, error) {
|
||||||
revision, err := t.configStore.Get(ctx, orgID)
|
revision, err := t.configStore.Get(ctx, orgID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if revision.cfg.TemplateFiles == nil {
|
var templates []definitions.NotificationTemplate
|
||||||
return map[string]string{}, nil
|
for name, tmpl := range revision.cfg.TemplateFiles {
|
||||||
|
tmpl := definitions.NotificationTemplate{
|
||||||
|
Name: name,
|
||||||
|
Template: tmpl,
|
||||||
|
}
|
||||||
|
provenance, err := t.provenanceStore.GetProvenance(ctx, &tmpl, orgID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
tmpl.Provenance = definitions.Provenance(provenance)
|
||||||
|
templates = append(templates, tmpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
return revision.cfg.TemplateFiles, nil
|
return templates, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TemplateService) SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error) {
|
func (t *TemplateService) SetTemplate(ctx context.Context, orgID int64, tmpl definitions.NotificationTemplate) (definitions.NotificationTemplate, error) {
|
||||||
|
@ -21,6 +21,7 @@ func TestTemplateService(t *testing.T) {
|
|||||||
GetsConfig(models.AlertConfiguration{
|
GetsConfig(models.AlertConfiguration{
|
||||||
AlertmanagerConfiguration: configWithTemplates,
|
AlertmanagerConfiguration: configWithTemplates,
|
||||||
})
|
})
|
||||||
|
sut.provenanceStore.(*MockProvisioningStore).EXPECT().GetProvenance(mock.Anything, mock.Anything, mock.Anything).Return(models.ProvenanceAPI, nil)
|
||||||
|
|
||||||
result, err := sut.GetTemplates(context.Background(), 1)
|
result, err := sut.GetTemplates(context.Background(), 1)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user