Skip to content

Commit

Permalink
Alerting: Fix invalid swagger specification (grafana#51907)
Browse files Browse the repository at this point in the history
* Alerting: Fix invalid swagger specification

* Add make targets for validating the generated swagger spec
  • Loading branch information
papagian authored Jul 13, 2022
1 parent b3992df commit 2163281
Show file tree
Hide file tree
Showing 11 changed files with 718 additions and 143 deletions.
4 changes: 2 additions & 2 deletions pkg/services/ngalert/api/api_provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions pkg/services/ngalert/api/api_provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions pkg/services/ngalert/api/forked_provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/services/ngalert/api/generated_base_api_provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/services/ngalert/api/tooling/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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
159 changes: 151 additions & 8 deletions pkg/services/ngalert/api/tooling/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down Expand Up @@ -3328,9 +3471,9 @@
],
"responses": {
"200": {
"description": "AlertRule",
"description": "ProvisionedAlertRule",
"schema": {
"$ref": "#/definitions/AlertRule"
"$ref": "#/definitions/ProvisionedAlertRule"
}
},
"404": {
Expand Down Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -172,6 +172,7 @@ type AlertRuleGroupPayload struct {
Body AlertRuleGroupMetadata
}

// swagger:model
type AlertRuleGroupMetadata struct {
Interval int64 `json:"interval"`
}
Expand Down
Loading

0 comments on commit 2163281

Please sign in to comment.