Alerting: New API endpoint GET /api/v1/ngalert (#55134)

* declare new endpoint /api/v1/ngalert
* add authorization for new path
* add request handler for the new path
pull/55269/head
Yuriy Tseretyan 3 years ago committed by GitHub
parent ca2139e9ce
commit 8a62020211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      pkg/services/ngalert/api/api_configuration.go
  2. 10
      pkg/services/ngalert/api/authorization.go
  3. 2
      pkg/services/ngalert/api/authorization_test.go
  4. 4
      pkg/services/ngalert/api/configuration.go
  5. 14
      pkg/services/ngalert/api/generated_base_api_configuration.go
  6. 13
      pkg/services/ngalert/api/tooling/api.json
  7. 15
      pkg/services/ngalert/api/tooling/definitions/admin.go
  8. 33
      pkg/services/ngalert/api/tooling/post.json
  9. 33
      pkg/services/ngalert/api/tooling/spec.json

@ -143,3 +143,22 @@ func (srv ConfigSrv) externalAlertmanagers(ctx context.Context, orgID int64) ([]
}
return alertmanagers, nil
}
func (srv ConfigSrv) RouteGetAlertingStatus(c *models.ReqContext) response.Response {
sendsAlertsTo := ngmodels.InternalAlertmanager
cfg, err := srv.store.GetAdminConfiguration(c.OrgID)
if err != nil && !errors.Is(err, store.ErrNoAdminConfiguration) {
msg := "failed to fetch configuration from the database"
srv.log.Error(msg, "err", err)
return ErrResp(http.StatusInternalServerError, err, msg)
}
if cfg != nil {
sendsAlertsTo = cfg.SendAlertsTo
}
resp := apimodels.AlertingStatus{
AlertmanagersChoice: apimodels.AlertmanagersChoice(sendsAlertsTo.String()),
}
return response.JSON(http.StatusOK, resp)
}

@ -172,6 +172,16 @@ func (api *API) authorize(method, path string) web.Handler {
case http.MethodPost + "/api/alertmanager/{DatasourceUID}/config/api/v1/receivers/test":
eval = ac.EvalPermission(ac.ActionAlertingNotificationsExternalRead, datasources.ScopeProvider.GetResourceScopeUID(ac.Parameter(":DatasourceUID")))
case http.MethodGet + "/api/v1/ngalert":
// let user with any alerting permission access this API
eval = ac.EvalAny(
ac.EvalPermission(ac.ActionAlertingInstanceRead),
ac.EvalPermission(ac.ActionAlertingInstancesExternalRead),
ac.EvalPermission(ac.ActionAlertingRuleRead),
ac.EvalPermission(ac.ActionAlertingRuleExternalRead),
ac.EvalPermission(ac.ActionAlertingNotificationsRead),
ac.EvalPermission(ac.ActionAlertingNotificationsExternalRead),
)
// Raw Alertmanager Config Paths
case http.MethodDelete + "/api/v1/ngalert/admin_config",
http.MethodGet + "/api/v1/ngalert/admin_config",

@ -49,7 +49,7 @@ func TestAuthorize(t *testing.T) {
}
paths[p] = methods
}
require.Len(t, paths, 39)
require.Len(t, paths, 40)
ac := acmock.New()
api := &API{AccessControl: ac}

@ -32,3 +32,7 @@ func (f *ConfigurationApiHandler) handleRoutePostNGalertConfig(c *models.ReqCont
func (f *ConfigurationApiHandler) handleRouteDeleteNGalertConfig(c *models.ReqContext) response.Response {
return f.grafana.RouteDeleteNGalertConfig(c)
}
func (f *ConfigurationApiHandler) handleRouteGetStatus(c *models.ReqContext) response.Response {
return f.grafana.RouteGetAlertingStatus(c)
}

@ -22,6 +22,7 @@ type ConfigurationApi interface {
RouteDeleteNGalertConfig(*models.ReqContext) response.Response
RouteGetAlertmanagers(*models.ReqContext) response.Response
RouteGetNGalertConfig(*models.ReqContext) response.Response
RouteGetStatus(*models.ReqContext) response.Response
RoutePostNGalertConfig(*models.ReqContext) response.Response
}
@ -34,6 +35,9 @@ func (f *ConfigurationApiHandler) RouteGetAlertmanagers(ctx *models.ReqContext)
func (f *ConfigurationApiHandler) RouteGetNGalertConfig(ctx *models.ReqContext) response.Response {
return f.handleRouteGetNGalertConfig(ctx)
}
func (f *ConfigurationApiHandler) RouteGetStatus(ctx *models.ReqContext) response.Response {
return f.handleRouteGetStatus(ctx)
}
func (f *ConfigurationApiHandler) RoutePostNGalertConfig(ctx *models.ReqContext) response.Response {
// Parse Request Body
conf := apimodels.PostableNGalertConfig{}
@ -75,6 +79,16 @@ func (api *API) RegisterConfigurationApiEndpoints(srv ConfigurationApi, m *metri
m,
),
)
group.Get(
toMacaronPath("/api/v1/ngalert"),
api.authorize(http.MethodGet, "/api/v1/ngalert"),
metrics.Instrument(
http.MethodGet,
"/api/v1/ngalert",
srv.RouteGetStatus,
m,
),
)
group.Post(
toMacaronPath("/api/v1/ngalert/admin_config"),
api.authorize(http.MethodPost, "/api/v1/ngalert/admin_config"),

@ -232,6 +232,19 @@
],
"type": "object"
},
"AlertingStatus": {
"properties": {
"alertmanagersChoice": {
"enum": [
"all",
"internal",
"external"
],
"type": "string"
}
},
"type": "object"
},
"ApiRuleNode": {
"properties": {
"alert": {

@ -4,6 +4,16 @@ import (
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
)
// swagger:route GET /api/v1/ngalert configuration RouteGetStatus
//
// Get the status of the alerting engine
//
// Produces:
// - application/json
//
// Responses:
// 200: AlertingStatus
// swagger:route GET /api/v1/ngalert/alertmanagers configuration RouteGetAlertmanagers
//
// Get the discovered and dropped Alertmanagers of the user's organization based on the specified configuration.
@ -81,3 +91,8 @@ type GettableAlertmanagers struct {
Status string `json:"status"`
Data v1.AlertManagersResult `json:"data"`
}
// swagger:model
type AlertingStatus struct {
AlertmanagersChoice AlertmanagersChoice `json:"alertmanagersChoice"`
}

@ -232,6 +232,19 @@
],
"type": "object"
},
"AlertingStatus": {
"properties": {
"alertmanagersChoice": {
"enum": [
"all",
"internal",
"external"
],
"type": "string"
}
},
"type": "object"
},
"ApiRuleNode": {
"properties": {
"alert": {
@ -5558,6 +5571,26 @@
]
}
},
"/api/v1/ngalert": {
"get": {
"description": "Get the status of the alerting engine",
"operationId": "RouteGetStatus",
"produces": [
"application/json"
],
"responses": {
"200": {
"description": "AlertingStatus",
"schema": {
"$ref": "#/definitions/AlertingStatus"
}
}
},
"tags": [
"configuration"
]
}
},
"/api/v1/provisioning/alert-rules": {
"post": {
"consumes": [

@ -1757,6 +1757,26 @@
}
}
},
"/api/v1/ngalert": {
"get": {
"description": "Get the status of the alerting engine",
"produces": [
"application/json"
],
"tags": [
"configuration"
],
"operationId": "RouteGetStatus",
"responses": {
"200": {
"description": "AlertingStatus",
"schema": {
"$ref": "#/definitions/AlertingStatus"
}
}
}
}
},
"/api/v1/provisioning/alert-rules": {
"post": {
"consumes": [
@ -2733,6 +2753,19 @@
}
}
},
"AlertingStatus": {
"type": "object",
"properties": {
"alertmanagersChoice": {
"type": "string",
"enum": [
"all",
"internal",
"external"
]
}
}
},
"ApiRuleNode": {
"type": "object",
"properties": {

Loading…
Cancel
Save