| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | package notifier | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-04 03:32:30 +08:00
										 |  |  | 	"github.com/prometheus/alertmanager/config" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 	alertingNotify "github.com/grafana/alerting/notify" | 
					
						
							| 
									
										
										
										
											2024-03-05 02:12:49 +08:00
										 |  |  | 	alertingTemplates "github.com/grafana/alerting/templates" | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 	apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/models" | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 01:39:46 +08:00
										 |  |  | func PostableGrafanaReceiverToGrafanaIntegrationConfig(p *apimodels.PostableGrafanaReceiver) *alertingNotify.GrafanaIntegrationConfig { | 
					
						
							|  |  |  | 	return &alertingNotify.GrafanaIntegrationConfig{ | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 		UID:                   p.UID, | 
					
						
							|  |  |  | 		Name:                  p.Name, | 
					
						
							|  |  |  | 		Type:                  p.Type, | 
					
						
							|  |  |  | 		DisableResolveMessage: p.DisableResolveMessage, | 
					
						
							|  |  |  | 		Settings:              json.RawMessage(p.Settings), | 
					
						
							|  |  |  | 		SecureSettings:        p.SecureSettings, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func PostableApiReceiverToApiReceiver(r *apimodels.PostableApiReceiver) *alertingNotify.APIReceiver { | 
					
						
							| 
									
										
										
										
											2023-04-26 01:39:46 +08:00
										 |  |  | 	integrations := alertingNotify.GrafanaIntegrations{ | 
					
						
							|  |  |  | 		Integrations: make([]*alertingNotify.GrafanaIntegrationConfig, 0, len(r.GrafanaManagedReceivers)), | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-04-26 01:39:46 +08:00
										 |  |  | 	for _, cfg := range r.GrafanaManagedReceivers { | 
					
						
							|  |  |  | 		integrations.Integrations = append(integrations.Integrations, PostableGrafanaReceiverToGrafanaIntegrationConfig(cfg)) | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return &alertingNotify.APIReceiver{ | 
					
						
							| 
									
										
										
										
											2023-04-26 01:39:46 +08:00
										 |  |  | 		ConfigReceiver:      r.Receiver, | 
					
						
							|  |  |  | 		GrafanaIntegrations: integrations, | 
					
						
							| 
									
										
										
										
											2023-04-14 00:25:32 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func PostableApiAlertingConfigToApiReceivers(c apimodels.PostableApiAlertingConfig) []*alertingNotify.APIReceiver { | 
					
						
							|  |  |  | 	apiReceivers := make([]*alertingNotify.APIReceiver, 0, len(c.Receivers)) | 
					
						
							|  |  |  | 	for _, receiver := range c.Receivers { | 
					
						
							|  |  |  | 		apiReceivers = append(apiReceivers, PostableApiReceiverToApiReceiver(receiver)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return apiReceivers | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | type DecryptFn = func(value string) string | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | func PostableToGettableGrafanaReceiver(r *apimodels.PostableGrafanaReceiver, provenance *models.Provenance, decryptFn DecryptFn, listOnly bool) (apimodels.GettableGrafanaReceiver, error) { | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 	out := apimodels.GettableGrafanaReceiver{ | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | 		UID:  r.UID, | 
					
						
							|  |  |  | 		Name: r.Name, | 
					
						
							|  |  |  | 		Type: r.Type, | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if provenance != nil { | 
					
						
							|  |  |  | 		out.Provenance = apimodels.Provenance(*provenance) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | 	// if we aren't only listing, include the settings in the output
 | 
					
						
							|  |  |  | 	if !listOnly { | 
					
						
							|  |  |  | 		secureFields := make(map[string]bool, len(r.SecureSettings)) | 
					
						
							|  |  |  | 		settings, err := simplejson.NewJson([]byte(r.Settings)) | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return apimodels.GettableGrafanaReceiver{}, err | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for k, v := range r.SecureSettings { | 
					
						
							|  |  |  | 			decryptedValue := decryptFn(v) | 
					
						
							|  |  |  | 			if decryptedValue == "" { | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				settings.Set(k, decryptedValue) | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			secureFields[k] = true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		jsonBytes, err := settings.MarshalJSON() | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return apimodels.GettableGrafanaReceiver{}, err | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | 		out.Settings = jsonBytes | 
					
						
							|  |  |  | 		out.SecureFields = secureFields | 
					
						
							|  |  |  | 		out.DisableResolveMessage = r.DisableResolveMessage | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return out, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | func PostableToGettableApiReceiver(r *apimodels.PostableApiReceiver, provenances map[string]models.Provenance, decryptFn DecryptFn, listOnly bool) (apimodels.GettableApiReceiver, error) { | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 	out := apimodels.GettableApiReceiver{ | 
					
						
							|  |  |  | 		Receiver: config.Receiver{ | 
					
						
							|  |  |  | 			Name: r.Receiver.Name, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, gr := range r.GrafanaManagedReceivers { | 
					
						
							|  |  |  | 		var prov *models.Provenance | 
					
						
							|  |  |  | 		if p, ok := provenances[gr.UID]; ok { | 
					
						
							|  |  |  | 			prov = &p | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-06 02:12:15 +08:00
										 |  |  | 		gettable, err := PostableToGettableGrafanaReceiver(gr, prov, decryptFn, listOnly) | 
					
						
							| 
									
										
										
										
											2024-02-02 03:42:59 +08:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return apimodels.GettableApiReceiver{}, err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		out.GrafanaManagedReceivers = append(out.GrafanaManagedReceivers, &gettable) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return out, nil | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-03-05 02:12:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // ToTemplateDefinitions converts the given PostableUserConfig's TemplateFiles to a slice of TemplateDefinitions.
 | 
					
						
							|  |  |  | func ToTemplateDefinitions(cfg *apimodels.PostableUserConfig) []alertingTemplates.TemplateDefinition { | 
					
						
							|  |  |  | 	out := make([]alertingTemplates.TemplateDefinition, 0, len(cfg.TemplateFiles)) | 
					
						
							|  |  |  | 	for name, tmpl := range cfg.TemplateFiles { | 
					
						
							|  |  |  | 		out = append(out, alertingTemplates.TemplateDefinition{ | 
					
						
							|  |  |  | 			Name:     name, | 
					
						
							|  |  |  | 			Template: tmpl, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return out | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-05-04 03:32:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Silence-specific compat functions to convert between grafana/alerting and model types.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func GettableSilenceToSilence(s alertingNotify.GettableSilence) *models.Silence { | 
					
						
							|  |  |  | 	sil := models.Silence(s) | 
					
						
							|  |  |  | 	return &sil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func GettableSilencesToSilences(silences alertingNotify.GettableSilences) []*models.Silence { | 
					
						
							|  |  |  | 	res := make([]*models.Silence, 0, len(silences)) | 
					
						
							|  |  |  | 	for _, sil := range silences { | 
					
						
							|  |  |  | 		res = append(res, GettableSilenceToSilence(*sil)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return res | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func SilenceToPostableSilence(s models.Silence) *alertingNotify.PostableSilence { | 
					
						
							|  |  |  | 	var id string | 
					
						
							|  |  |  | 	if s.ID != nil { | 
					
						
							|  |  |  | 		id = *s.ID | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &alertingNotify.PostableSilence{ | 
					
						
							|  |  |  | 		ID:      id, | 
					
						
							|  |  |  | 		Silence: s.Silence, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |