| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2021-04-20 02:26:04 +08:00
										 |  |  | 	apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	"gopkg.in/yaml.v3" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2021-04-07 13:42:43 +08:00
										 |  |  | 	amSilencesPath    = "/alertmanager/api/v2/silences" | 
					
						
							|  |  |  | 	amSilencePath     = "/alertmanager/api/v2/silence/%s" | 
					
						
							|  |  |  | 	amAlertGroupsPath = "/alertmanager/api/v2/alerts/groups" | 
					
						
							|  |  |  | 	amAlertsPath      = "/alertmanager/api/v2/alerts" | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	amConfigPath      = "/api/v1/alerts" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type LotexAM struct { | 
					
						
							|  |  |  | 	log log.Logger | 
					
						
							|  |  |  | 	*AlertingProxy | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewLotexAM(proxy *AlertingProxy, log log.Logger) *LotexAM { | 
					
						
							|  |  |  | 	return &LotexAM{ | 
					
						
							|  |  |  | 		log:           log, | 
					
						
							|  |  |  | 		AlertingProxy: proxy, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-01 04:00:56 +08:00
										 |  |  | func (am *LotexAM) RouteCreateSilence(ctx *models.ReqContext, silenceBody apimodels.PostableSilence) response.Response { | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	blob, err := json.Marshal(silenceBody) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(500, "Failed marshal silence", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodPost, | 
					
						
							|  |  |  | 		withPath(*ctx.Req.URL, amSilencesPath), | 
					
						
							|  |  |  | 		bytes.NewBuffer(blob), | 
					
						
							|  |  |  | 		jsonExtractor(&apimodels.GettableSilence{}), | 
					
						
							|  |  |  | 		map[string]string{"Content-Type": "application/json"}, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteDeleteAlertingConfig(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodDelete, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			amConfigPath, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 		messageExtractor, | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteDeleteSilence(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodDelete, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			fmt.Sprintf(amSilencePath, ctx.Params(":SilenceId")), | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 		messageExtractor, | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteGetAlertingConfig(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodGet, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			amConfigPath, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-04-07 13:42:43 +08:00
										 |  |  | 		yamlExtractor(&apimodels.GettableUserConfig{}), | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteGetAMAlertGroups(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodGet, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			amAlertGroupsPath, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 		jsonExtractor(&apimodels.AlertGroups{}), | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteGetAMAlerts(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodGet, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			amAlertsPath, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 		jsonExtractor(&apimodels.GettableAlerts{}), | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteGetSilence(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodGet, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			fmt.Sprintf(amSilencePath, ctx.Params(":SilenceId")), | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 		jsonExtractor(&apimodels.GettableSilence{}), | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RouteGetSilences(ctx *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodGet, | 
					
						
							|  |  |  | 		withPath( | 
					
						
							|  |  |  | 			*ctx.Req.URL, | 
					
						
							|  |  |  | 			amSilencesPath, | 
					
						
							|  |  |  | 		), | 
					
						
							|  |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 		jsonExtractor(&apimodels.GettableSilences{}), | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 		nil, | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RoutePostAlertingConfig(ctx *models.ReqContext, config apimodels.PostableUserConfig) response.Response { | 
					
						
							| 
									
										
										
										
											2021-04-12 17:04:37 +08:00
										 |  |  | 	yml, err := yaml.Marshal(&config) | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(500, "Failed marshal alert manager configuration ", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodPost, | 
					
						
							|  |  |  | 		withPath(*ctx.Req.URL, amConfigPath), | 
					
						
							|  |  |  | 		bytes.NewBuffer(yml), | 
					
						
							|  |  |  | 		messageExtractor, | 
					
						
							|  |  |  | 		nil, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (am *LotexAM) RoutePostAMAlerts(ctx *models.ReqContext, alerts apimodels.PostableAlerts) response.Response { | 
					
						
							|  |  |  | 	yml, err := yaml.Marshal(alerts) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(500, "Failed marshal postable alerts", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-08 03:36:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return am.withReq( | 
					
						
							|  |  |  | 		ctx, | 
					
						
							|  |  |  | 		http.MethodPost, | 
					
						
							|  |  |  | 		withPath(*ctx.Req.URL, amAlertsPath), | 
					
						
							|  |  |  | 		bytes.NewBuffer(yml), | 
					
						
							|  |  |  | 		messageExtractor, | 
					
						
							|  |  |  | 		nil, | 
					
						
							|  |  |  | 	) | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | } |