| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							|  |  |  | 	apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" | 
					
						
							|  |  |  | 	ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/store" | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	v1 "github.com/prometheus/client_golang/api/prometheus/v1" | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type AdminSrv struct { | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	scheduler Scheduler | 
					
						
							|  |  |  | 	store     store.AdminConfigurationStore | 
					
						
							|  |  |  | 	log       log.Logger | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (srv AdminSrv) RouteGetAlertmanagers(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	urls := srv.scheduler.AlertmanagersFor(c.OrgId) | 
					
						
							|  |  |  | 	droppedURLs := srv.scheduler.DroppedAlertmanagersFor(c.OrgId) | 
					
						
							|  |  |  | 	ams := v1.AlertManagersResult{Active: make([]v1.AlertManager, len(urls)), Dropped: make([]v1.AlertManager, len(droppedURLs))} | 
					
						
							|  |  |  | 	for i, url := range urls { | 
					
						
							|  |  |  | 		ams.Active[i].URL = url.String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	for i, url := range droppedURLs { | 
					
						
							|  |  |  | 		ams.Dropped[i].URL = url.String() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, apimodels.GettableAlertmanagers{ | 
					
						
							|  |  |  | 		Status: "success", | 
					
						
							|  |  |  | 		Data:   ams, | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (srv AdminSrv) RouteGetNGalertConfig(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	if c.OrgRole != models.ROLE_ADMIN { | 
					
						
							|  |  |  | 		return accessForbiddenResp() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg, err := srv.store.GetAdminConfiguration(c.OrgId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		if errors.Is(err, store.ErrNoAdminConfiguration) { | 
					
						
							|  |  |  | 			return ErrResp(http.StatusNotFound, err, "") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		msg := "failed to fetch admin configuration from the database" | 
					
						
							|  |  |  | 		srv.log.Error(msg, "err", err) | 
					
						
							|  |  |  | 		return ErrResp(http.StatusInternalServerError, err, msg) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	resp := apimodels.GettableNGalertConfig{ | 
					
						
							| 
									
										
										
											
												Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
											
										 
											2022-02-02 07:36:55 +08:00
										 |  |  | 		Alertmanagers:       cfg.Alertmanagers, | 
					
						
							|  |  |  | 		AlertmanagersChoice: apimodels.AlertmanagersChoice(cfg.SendAlertsTo.String()), | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return response.JSON(http.StatusOK, resp) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (srv AdminSrv) RoutePostNGalertConfig(c *models.ReqContext, body apimodels.PostableNGalertConfig) response.Response { | 
					
						
							|  |  |  | 	if c.OrgRole != models.ROLE_ADMIN { | 
					
						
							|  |  |  | 		return accessForbiddenResp() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
											
												Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
											
										 
											2022-02-02 07:36:55 +08:00
										 |  |  | 	sendAlertsTo, err := ngmodels.StringToAlertmanagersChoice(string(body.AlertmanagersChoice)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return response.Error(400, "Invalid alertmanager choice specified", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if sendAlertsTo == ngmodels.ExternalAlertmanagers && len(body.Alertmanagers) == 0 { | 
					
						
							|  |  |  | 		return response.Error(400, "At least one Alertmanager must be provided to choose this option", nil) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 	cfg := &ngmodels.AdminConfiguration{ | 
					
						
							|  |  |  | 		Alertmanagers: body.Alertmanagers, | 
					
						
							| 
									
										
										
											
												Alerting: send alerts to external, internal, or both alertmanagers (#40341)
* (WIP) send alerts to external, internal, or both alertmanagers
* Modify admin configuration endpoint, update swagger docs
* Integration test for admin config updated
* Code review changes
* Fix alertmanagers choice not changing bug, add unit test
* Add AlertmanagersChoice as enum in swagger, code review changes
* Fix API and tests errors
* Change enum from int to string, use 'SendAlertsTo' instead of 'AlertmanagerChoice' where necessary
* Fix tests to reflect last changes
* Keep senders running when alerts are handled just internally
* Check if any external AM has been discovered before sending alerts, update tests
* remove duplicate data from logs
* update comment
* represent alertmanagers choice as an int instead of a string
* default alertmanagers choice to all alertmanagers, test cases
* update definitions and generate spec
											
										 
											2022-02-02 07:36:55 +08:00
										 |  |  | 		SendAlertsTo:  sendAlertsTo, | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 		OrgID:         c.OrgId, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-13 05:19:16 +08:00
										 |  |  | 	if err := cfg.Validate(); err != nil { | 
					
						
							|  |  |  | 		msg := "failed to validate admin configuration" | 
					
						
							|  |  |  | 		srv.log.Error(msg, "err", err) | 
					
						
							|  |  |  | 		return ErrResp(http.StatusBadRequest, err, msg) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 	cmd := store.UpdateAdminConfigurationCmd{AdminConfiguration: cfg} | 
					
						
							|  |  |  | 	if err := srv.store.UpdateAdminConfiguration(cmd); err != nil { | 
					
						
							|  |  |  | 		msg := "failed to save the admin configuration to the database" | 
					
						
							|  |  |  | 		srv.log.Error(msg, "err", err) | 
					
						
							|  |  |  | 		return ErrResp(http.StatusBadRequest, err, msg) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	return response.JSON(http.StatusCreated, util.DynMap{"message": "admin configuration updated"}) | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (srv AdminSrv) RouteDeleteNGalertConfig(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	if c.OrgRole != models.ROLE_ADMIN { | 
					
						
							|  |  |  | 		return accessForbiddenResp() | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := srv.store.DeleteAdminConfiguration(c.OrgId) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		srv.log.Error("unable to delete configuration", "err", err) | 
					
						
							|  |  |  | 		return ErrResp(http.StatusInternalServerError, err, "") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	return response.JSON(http.StatusOK, util.DynMap{"message": "admin configuration deleted"}) | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | } |