Alerting: Fix invalid swagger specification (#51907)

* Alerting: Fix invalid swagger specification

* Add make targets for validating the generated swagger spec
This commit is contained in:
Sofia Papagiannaki
2022-07-13 12:34:54 +03:00
committed by GitHub
parent b3992df988
commit 21632817c5
11 changed files with 720 additions and 145 deletions
+2 -2
View File
@@ -253,7 +253,7 @@ func (srv *ProvisioningSrv) RouteRouteGetAlertRule(c *models.ReqContext, UID str
return response.JSON(http.StatusOK, definitions.NewAlertRule(rule, provenace))
}
func (srv *ProvisioningSrv) RoutePostAlertRule(c *models.ReqContext, ar definitions.AlertRule) response.Response {
func (srv *ProvisioningSrv) RoutePostAlertRule(c *models.ReqContext, ar definitions.ProvisionedAlertRule) response.Response {
createdAlertRule, err := srv.alertRules.CreateAlertRule(c.Req.Context(), ar.UpstreamModel(), alerting_models.ProvenanceAPI)
if errors.Is(err, alerting_models.ErrAlertRuleFailedValidation) {
return ErrResp(http.StatusBadRequest, err, "")
@@ -270,7 +270,7 @@ func (srv *ProvisioningSrv) RoutePostAlertRule(c *models.ReqContext, ar definiti
return response.JSON(http.StatusCreated, ar)
}
func (srv *ProvisioningSrv) RoutePutAlertRule(c *models.ReqContext, ar definitions.AlertRule, UID string) response.Response {
func (srv *ProvisioningSrv) RoutePutAlertRule(c *models.ReqContext, ar definitions.ProvisionedAlertRule, UID string) response.Response {
updated := ar.UpstreamModel()
updated.UID = UID
updatedAlertRule, err := srv.alertRules.UpdateAlertRule(c.Req.Context(), ar.UpstreamModel(), alerting_models.ProvenanceAPI)
@@ -418,12 +418,12 @@ func createInvalidMuteTiming() definitions.MuteTimeInterval {
}
}
func createInvalidAlertRule() definitions.AlertRule {
return definitions.AlertRule{}
func createInvalidAlertRule() definitions.ProvisionedAlertRule {
return definitions.ProvisionedAlertRule{}
}
func createTestAlertRule(title string, orgID int64) definitions.AlertRule {
return definitions.AlertRule{
func createTestAlertRule(title string, orgID int64) definitions.ProvisionedAlertRule {
return definitions.ProvisionedAlertRule{
OrgID: orgID,
Title: title,
Condition: "A",
@@ -445,7 +445,7 @@ func createTestAlertRule(title string, orgID int64) definitions.AlertRule {
}
}
func insertRule(t *testing.T, srv ProvisioningSrv, rule definitions.AlertRule) {
func insertRule(t *testing.T, srv ProvisioningSrv, rule definitions.ProvisionedAlertRule) {
t.Helper()
rc := createTestRequestCtx()
@@ -87,11 +87,11 @@ func (f *ForkedProvisioningApi) forkRouteGetAlertRule(ctx *models.ReqContext, UI
return f.svc.RouteRouteGetAlertRule(ctx, UID)
}
func (f *ForkedProvisioningApi) forkRoutePostAlertRule(ctx *models.ReqContext, ar apimodels.AlertRule) response.Response {
func (f *ForkedProvisioningApi) forkRoutePostAlertRule(ctx *models.ReqContext, ar apimodels.ProvisionedAlertRule) response.Response {
return f.svc.RoutePostAlertRule(ctx, ar)
}
func (f *ForkedProvisioningApi) forkRoutePutAlertRule(ctx *models.ReqContext, ar apimodels.AlertRule, UID string) response.Response {
func (f *ForkedProvisioningApi) forkRoutePutAlertRule(ctx *models.ReqContext, ar apimodels.ProvisionedAlertRule, UID string) response.Response {
return f.svc.RoutePutAlertRule(ctx, ar, UID)
}
@@ -89,7 +89,7 @@ func (f *ForkedProvisioningApi) RouteGetTemplates(ctx *models.ReqContext) respon
return f.forkRouteGetTemplates(ctx)
}
func (f *ForkedProvisioningApi) RoutePostAlertRule(ctx *models.ReqContext) response.Response {
conf := apimodels.AlertRule{}
conf := apimodels.ProvisionedAlertRule{}
if err := web.Bind(ctx.Req, &conf); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
@@ -111,7 +111,7 @@ func (f *ForkedProvisioningApi) RoutePostMuteTiming(ctx *models.ReqContext) resp
}
func (f *ForkedProvisioningApi) RoutePutAlertRule(ctx *models.ReqContext) response.Response {
uIDParam := web.Params(ctx.Req)[":UID"]
conf := apimodels.AlertRule{}
conf := apimodels.ProvisionedAlertRule{}
if err := web.Bind(ctx.Req, &conf); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
+7 -1
View File
@@ -30,6 +30,12 @@ post.json: spec.json
api.json: spec-stable.json
go run cmd/clean-swagger/main.go -if $(<) -of $@
validate-stable: spec-stable.json $(SWAGGER)
$(SWAGGER) validate $(<)
validate: spec.json $(SWAGGER)
$(SWAGGER) validate $(<)
swagger-codegen-api:
docker run --rm -v $$(pwd):/local --user $$(id -u):$$(id -g) parsertongue/swagger-codegen-cli:3.0.32 generate \
-i /local/post.json \
@@ -59,4 +65,4 @@ serve: post.json
serve-stable: api.json
docker run --rm -p 80:8080 -v $$(pwd):/tmp -e SWAGGER_FILE=/tmp/$(<) swaggerapi/swagger-editor
all: post.json api.json swagger-codegen-api fix copy-files clean
all: post.json validate api.json validate-stable swagger-codegen-api fix copy-files clean
+151 -8
View File
@@ -1821,6 +1821,149 @@
"Provenance": {
"type": "string"
},
"ProvisionedAlertRule": {
"properties": {
"annotations": {
"additionalProperties": {
"type": "string"
},
"example": {
"runbook_url": "https://supercoolrunbook.com/page/13"
},
"type": "object"
},
"condition": {
"example": "A",
"type": "string"
},
"data": {
"example": [
{
"datasourceUid": "-100",
"model": {
"conditions": [
{
"evaluator": {
"params": [
0,
0
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": []
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"datasource": {
"type": "__expr__",
"uid": "__expr__"
},
"expression": "1 == 1",
"hide": false,
"intervalMs": 1000,
"maxDataPoints": 43200,
"refId": "A",
"type": "math"
},
"queryType": "",
"refId": "A",
"relativeTimeRange": {
"from": 0,
"to": 0
}
}
],
"items": {
"$ref": "#/definitions/AlertQuery"
},
"type": "array"
},
"execErrState": {
"enum": [
"Alerting",
"Error",
"OK"
],
"type": "string"
},
"folderUID": {
"example": "project_x",
"type": "string"
},
"for": {
"$ref": "#/definitions/Duration"
},
"id": {
"format": "int64",
"type": "integer"
},
"labels": {
"additionalProperties": {
"type": "string"
},
"example": {
"team": "sre-team-1"
},
"type": "object"
},
"noDataState": {
"enum": [
"Alerting",
"NoData",
"OK"
],
"type": "string"
},
"orgID": {
"format": "int64",
"type": "integer"
},
"provenance": {
"$ref": "#/definitions/Provenance"
},
"ruleGroup": {
"example": "eval_group_1",
"maxLength": 190,
"minLength": 1,
"type": "string"
},
"title": {
"example": "Always firing",
"maxLength": 190,
"minLength": 1,
"type": "string"
},
"uid": {
"type": "string"
},
"updated": {
"format": "date-time",
"readOnly": true,
"type": "string"
}
},
"required": [
"orgID",
"folderUID",
"ruleGroup",
"title",
"condition",
"data",
"noDataState",
"execErrState",
"for"
],
"type": "object"
},
"PushoverConfig": {
"properties": {
"expire": {
@@ -3269,15 +3412,15 @@
"in": "body",
"name": "Body",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
}
],
"responses": {
"201": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"400": {
@@ -3328,9 +3471,9 @@
],
"responses": {
"200": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"404": {
@@ -3359,15 +3502,15 @@
"in": "body",
"name": "Body",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
}
],
"responses": {
"200": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"400": {
@@ -11,7 +11,7 @@ import (
// Get a specific alert rule by UID.
//
// Responses:
// 200: AlertRule
// 200: ProvisionedAlertRule
// 404: description: Not found.
// swagger:route POST /api/v1/provisioning/alert-rules provisioning stable RoutePostAlertRule
@@ -22,7 +22,7 @@ import (
// - application/json
//
// Responses:
// 201: AlertRule
// 201: ProvisionedAlertRule
// 400: ValidationError
// swagger:route PUT /api/v1/provisioning/alert-rules/{UID} provisioning stable RoutePutAlertRule
@@ -33,7 +33,7 @@ import (
// - application/json
//
// Responses:
// 200: AlertRule
// 200: ProvisionedAlertRule
// 400: ValidationError
// swagger:route DELETE /api/v1/provisioning/alert-rules/{UID} provisioning stable RouteDeleteAlertRule
@@ -53,10 +53,10 @@ type AlertRuleUIDReference struct {
// swagger:parameters RoutePostAlertRule RoutePutAlertRule
type AlertRulePayload struct {
// in:body
Body AlertRule
Body ProvisionedAlertRule
}
type AlertRule struct {
type ProvisionedAlertRule struct {
ID int64 `json:"id"`
UID string `json:"uid"`
// required: true
@@ -96,7 +96,7 @@ type AlertRule struct {
Provenance models.Provenance `json:"provenance,omitempty"`
}
func (a *AlertRule) UpstreamModel() models.AlertRule {
func (a *ProvisionedAlertRule) UpstreamModel() models.AlertRule {
return models.AlertRule{
ID: a.ID,
UID: a.UID,
@@ -115,8 +115,8 @@ func (a *AlertRule) UpstreamModel() models.AlertRule {
}
}
func NewAlertRule(rule models.AlertRule, provenance models.Provenance) AlertRule {
return AlertRule{
func NewAlertRule(rule models.AlertRule, provenance models.Provenance) ProvisionedAlertRule {
return ProvisionedAlertRule{
ID: rule.ID,
UID: rule.UID,
OrgID: rule.OrgID,
@@ -172,6 +172,7 @@ type AlertRuleGroupPayload struct {
Body AlertRuleGroupMetadata
}
// swagger:model
type AlertRuleGroupMetadata struct {
Interval int64 `json:"interval"`
}
+145 -2
View File
@@ -1821,6 +1821,149 @@
"Provenance": {
"type": "string"
},
"ProvisionedAlertRule": {
"properties": {
"annotations": {
"additionalProperties": {
"type": "string"
},
"example": {
"runbook_url": "https://supercoolrunbook.com/page/13"
},
"type": "object"
},
"condition": {
"example": "A",
"type": "string"
},
"data": {
"example": [
{
"datasourceUid": "-100",
"model": {
"conditions": [
{
"evaluator": {
"params": [
0,
0
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": []
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"datasource": {
"type": "__expr__",
"uid": "__expr__"
},
"expression": "1 == 1",
"hide": false,
"intervalMs": 1000,
"maxDataPoints": 43200,
"refId": "A",
"type": "math"
},
"queryType": "",
"refId": "A",
"relativeTimeRange": {
"from": 0,
"to": 0
}
}
],
"items": {
"$ref": "#/definitions/AlertQuery"
},
"type": "array"
},
"execErrState": {
"enum": [
"Alerting",
"Error",
"OK"
],
"type": "string"
},
"folderUID": {
"example": "project_x",
"type": "string"
},
"for": {
"$ref": "#/definitions/Duration"
},
"id": {
"format": "int64",
"type": "integer"
},
"labels": {
"additionalProperties": {
"type": "string"
},
"example": {
"team": "sre-team-1"
},
"type": "object"
},
"noDataState": {
"enum": [
"Alerting",
"NoData",
"OK"
],
"type": "string"
},
"orgID": {
"format": "int64",
"type": "integer"
},
"provenance": {
"$ref": "#/definitions/Provenance"
},
"ruleGroup": {
"example": "eval_group_1",
"maxLength": 190,
"minLength": 1,
"type": "string"
},
"title": {
"example": "Always firing",
"maxLength": 190,
"minLength": 1,
"type": "string"
},
"uid": {
"type": "string"
},
"updated": {
"format": "date-time",
"readOnly": true,
"type": "string"
}
},
"required": [
"orgID",
"folderUID",
"ruleGroup",
"title",
"condition",
"data",
"noDataState",
"execErrState",
"for"
],
"type": "object"
},
"PushoverConfig": {
"properties": {
"expire": {
@@ -4893,7 +5036,7 @@
"in": "body",
"name": "Body",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
}
],
@@ -4983,7 +5126,7 @@
"in": "body",
"name": "Body",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
}
],
+151 -8
View File
@@ -1659,15 +1659,15 @@
"name": "Body",
"in": "body",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
}
],
"responses": {
"201": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"400": {
@@ -1698,9 +1698,9 @@
],
"responses": {
"200": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"404": {
@@ -1730,15 +1730,15 @@
"name": "Body",
"in": "body",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
}
],
"responses": {
"200": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"400": {
@@ -4206,6 +4206,149 @@
"Provenance": {
"type": "string"
},
"ProvisionedAlertRule": {
"type": "object",
"required": [
"orgID",
"folderUID",
"ruleGroup",
"title",
"condition",
"data",
"noDataState",
"execErrState",
"for"
],
"properties": {
"annotations": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"example": {
"runbook_url": "https://supercoolrunbook.com/page/13"
}
},
"condition": {
"type": "string",
"example": "A"
},
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/AlertQuery"
},
"example": [
{
"datasourceUid": "-100",
"model": {
"conditions": [
{
"evaluator": {
"params": [
0,
0
],
"type": "gt"
},
"operator": {
"type": "and"
},
"query": {
"params": []
},
"reducer": {
"params": [],
"type": "avg"
},
"type": "query"
}
],
"datasource": {
"type": "__expr__",
"uid": "__expr__"
},
"expression": "1 == 1",
"hide": false,
"intervalMs": 1000,
"maxDataPoints": 43200,
"refId": "A",
"type": "math"
},
"queryType": "",
"refId": "A",
"relativeTimeRange": {
"from": 0,
"to": 0
}
}
]
},
"execErrState": {
"type": "string",
"enum": [
"Alerting",
"Error",
"OK"
]
},
"folderUID": {
"type": "string",
"example": "project_x"
},
"for": {
"$ref": "#/definitions/Duration"
},
"id": {
"type": "integer",
"format": "int64"
},
"labels": {
"type": "object",
"additionalProperties": {
"type": "string"
},
"example": {
"team": "sre-team-1"
}
},
"noDataState": {
"type": "string",
"enum": [
"Alerting",
"NoData",
"OK"
]
},
"orgID": {
"type": "integer",
"format": "int64"
},
"provenance": {
"$ref": "#/definitions/Provenance"
},
"ruleGroup": {
"type": "string",
"maxLength": 190,
"minLength": 1,
"example": "eval_group_1"
},
"title": {
"type": "string",
"maxLength": 190,
"minLength": 1,
"example": "Always firing"
},
"uid": {
"type": "string"
},
"updated": {
"type": "string",
"format": "date-time",
"readOnly": true
}
}
},
"PushoverConfig": {
"type": "object",
"properties": {