AlertingNG: Unified alerting API mock (#32040)

* AlertingNG: Alertmanager mock API

* AlertingNG: Remove permissions API routes

* Add example POST payloads

* Prometheus and testing mock API
This commit is contained in:
Sofia Papagiannaki
2021-03-17 12:47:03 +02:00
committed by GitHub
parent ee8a864615
commit 68b05b8aaa
14 changed files with 1525 additions and 59 deletions
@@ -0,0 +1,133 @@
package api
import (
"net/http"
"time"
apimodels "github.com/grafana/alerting-api/pkg/api"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
)
type PrometheusApiMock struct {
log log.Logger
}
func (mock PrometheusApiMock) RouteGetAlertStatuses(c *models.ReqContext) response.Response {
datasourceId := c.Params(":DatasourceId")
mock.log.Info("RouteGetAlertStatuses: ", "DatasourceId", datasourceId)
now := time.Now()
result := apimodels.AlertResponse{
Data: apimodels.AlertDiscovery{
Alerts: []*apimodels.Alert{
{
Labels: map[string]string{
"label 1": "value 1",
"label 2": "value 2",
},
Annotations: map[string]string{
"alert annotation 1": "value 1",
"alert annotation 2": "value 2",
},
State: "firing",
ActiveAt: &now,
Value: "",
},
{
Labels: map[string]string{
"label 1-2": "value 1",
"label 2-2": "value 2",
},
Annotations: map[string]string{
"alert annotation 1-2": "value 1",
"alert annotation 2-2": "value 2",
},
State: "inactive",
ActiveAt: &now,
Value: "",
},
},
},
}
return response.JSON(http.StatusOK, result)
}
func (mock PrometheusApiMock) RouteGetRuleStatuses(c *models.ReqContext) response.Response {
datasourceId := c.Params(":DatasourceId")
mock.log.Info("RouteGetRuleStatuses: ", "DatasourceId", datasourceId)
now := time.Now()
result := apimodels.RuleResponse{
Data: apimodels.RuleDiscovery{
RuleGroups: []*apimodels.RuleGroup{
{
Name: "group1",
Interval: 60,
LastEvaluation: now.Add(-time.Minute),
EvaluationTime: 1,
Rules: []apimodels.AlertingRule{
{
State: "firing",
Name: "rule 1-1",
Query: `{
"title": "rule 1-1",
"condition": "A",
"data": [
{
"refId": "A",
"queryType": "",
"relativeTimeRange": {
"from": 18000,
"to": 10800
},
"model": {
"datasource": "__expr__",
"type": "math",
"expression": "2 + 2 > 1"
}
}
],
"no_data_state": "NoData",
"exec_err_state": "Alerting"
}`,
Duration: 600,
Annotations: map[string]string{
"annotation 1": "value 1",
"annotation 2": "value 2",
},
Alerts: []*apimodels.Alert{
{
Labels: map[string]string{
"label 1": "value 1",
"label 2": "value 2",
},
Annotations: map[string]string{
"alert annotation 1": "value 1",
"alert annotation 2": "value 2",
},
State: "firing",
ActiveAt: &now,
Value: "",
},
{
Labels: map[string]string{
"label 1-2": "value 1",
"label 2-2": "value 2",
},
Annotations: map[string]string{
"alert annotation 1-2": "value 1",
"alert annotation 2-2": "value 2",
},
State: "inactive",
ActiveAt: &now,
Value: "",
},
},
},
},
},
},
},
}
return response.JSON(http.StatusOK, result)
}