From 7a01fb369dde8184fb666b69cee7b355b621947f Mon Sep 17 00:00:00 2001 From: Sofia Papagiannaki Date: Fri, 13 Aug 2021 16:15:53 +0300 Subject: [PATCH] Alerting: Fix API spec generation (#37852) * Alerting: Fix API spec generation * Apply suggestion from code review Co-authored-by: gotjosh --- pkg/services/ngalert/api/tooling/Makefile | 6 ++ .../api/tooling/definitions/alertmanager.go | 26 +++--- pkg/services/ngalert/api/tooling/post.json | 62 +++++--------- pkg/services/ngalert/api/tooling/spec.json | 83 +++++++------------ 4 files changed, 68 insertions(+), 109 deletions(-) diff --git a/pkg/services/ngalert/api/tooling/Makefile b/pkg/services/ngalert/api/tooling/Makefile index 1d154ff4187..8e1536612d5 100644 --- a/pkg/services/ngalert/api/tooling/Makefile +++ b/pkg/services/ngalert/api/tooling/Makefile @@ -16,6 +16,12 @@ spec.json: $(GO_PKG_FILES) quay.io/goswagger/swagger:$(SWAGGER_TAG) \ generate spec -m -o $@ +ensure_go-swagger_mac: + @hash swagger &>/dev/null || (brew tap go-swagger/go-swagger && brew install go-swagger) + +spec.json-mac: ensure_go-swagger_mac $(GO_PKG_FILES) + swagger generate spec -m -w $(API_DIR) -o spec.json + post.json: spec.json go run cmd/clean-swagger/main.go -if $(<) -of $@ diff --git a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go index 28b4913f38b..60da4024a45 100644 --- a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go +++ b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go @@ -54,7 +54,7 @@ import ( // get alertmanager alerts // // Responses: -// 200: GettableAlerts +// 200: gettableAlerts // 400: ValidationError // swagger:route POST /api/alertmanager/{Recipient}/api/v2/alerts alertmanager RoutePostAMAlerts @@ -70,7 +70,7 @@ import ( // get alertmanager alerts // // Responses: -// 200: AlertGroups +// 200: alertGroups // 400: ValidationError // swagger:route GET /api/alertmanager/{Recipient}/api/v2/silences alertmanager RouteGetSilences @@ -78,7 +78,7 @@ import ( // get silences // // Responses: -// 200: GettableSilences +// 200: gettableSilences // 400: ValidationError // swagger:route POST /api/alertmanager/{Recipient}/api/v2/silences alertmanager RouteCreateSilence @@ -86,7 +86,7 @@ import ( // create silence // // Responses: -// 201: GettableSilence +// 201: gettableSilence // 400: ValidationError // swagger:route GET /api/alertmanager/{Recipient}/api/v2/silence/{SilenceId} alertmanager RouteGetSilence @@ -94,7 +94,7 @@ import ( // get silence // // Responses: -// 200: GettableSilence +// 200: gettableSilence // 400: ValidationError // swagger:route DELETE /api/alertmanager/{Recipient}/api/v2/silence/{SilenceId} alertmanager RouteDeleteSilence @@ -196,28 +196,28 @@ func NewGettableStatus(cfg *PostableApiAlertingConfig) *GettableStatus { } } -// swagger:model +// swagger:model postableSilence type PostableSilence = amv2.PostableSilence -// swagger:model +// swagger:model gettableSilences type GettableSilences = amv2.GettableSilences -// swagger:model +// swagger:model gettableSilence type GettableSilence = amv2.GettableSilence -// swagger:model +// swagger:model gettableAlerts type GettableAlerts = amv2.GettableAlerts -// swagger:model +// swagger:model gettableAlert type GettableAlert = amv2.GettableAlert -// swagger:model +// swagger:model alertGroups type AlertGroups = amv2.AlertGroups -// swagger:model +// swagger:model alertGroup type AlertGroup = amv2.AlertGroup -// swagger:model +// swagger:model receiver type Receiver = amv2.Receiver // swagger:parameters RouteGetAMAlerts RouteGetAMAlertGroups diff --git a/pkg/services/ngalert/api/tooling/post.json b/pkg/services/ngalert/api/tooling/post.json index e6a5a6572d8..94a86f2ccea 100644 --- a/pkg/services/ngalert/api/tooling/post.json +++ b/pkg/services/ngalert/api/tooling/post.json @@ -57,8 +57,6 @@ "type": "object", "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "AlertGroup": {}, - "AlertGroups": {}, "AlertInstancesResponse": { "properties": { "instances": { @@ -505,7 +503,6 @@ "Failure": { "$ref": "#/definitions/ResponseDetails" }, - "GettableAlert": {}, "GettableAlertmanagers": { "properties": { "data": { @@ -519,7 +516,6 @@ "type": "object", "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "GettableAlerts": {}, "GettableApiAlertingConfig": { "properties": { "global": { @@ -823,12 +819,6 @@ "type": "object", "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "GettableSilence": { - "$ref": "#/definitions/gettableSilence" - }, - "GettableSilences": { - "$ref": "#/definitions/gettableSilences" - }, "GettableStatus": { "properties": { "cluster": { @@ -1630,7 +1620,6 @@ "type": "object", "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "PostableSilence": {}, "PostableUserConfig": { "properties": { "alertmanager_config": { @@ -1765,7 +1754,8 @@ } }, "title": "Receiver configuration provides configuration on how to contact a receiver.", - "type": "object" + "type": "object", + "x-go-package": "github.com/prometheus/alertmanager/config" }, "Regexp": { "description": "A Regexp is safe for concurrent use by multiple goroutines,\nexcept for configuration methods, such as Longest.", @@ -2284,7 +2274,6 @@ "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, "URL": { - "description": "The general form represented is:\n\n[scheme:][//[userinfo@]host][/]path[?query][#fragment]\n\nURLs that do not start with a slash after the scheme are interpreted as:\n\nscheme:opaque[?query][#fragment]\n\nNote that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.\nA consequence is that it is impossible to tell which slashes in the Path were\nslashes in the raw URL and which were %2f. This distinction is rarely important,\nbut when it is, the code should use RawPath, an optional field which only gets\nset if the default encoding is different from Path.\n\nURL's String method uses the EscapedPath method to obtain the path. See the\nEscapedPath method for more details.", "properties": { "ForceQuery": { "type": "boolean" @@ -2317,9 +2306,9 @@ "$ref": "#/definitions/Userinfo" } }, - "title": "A URL represents a parsed URL (technically, a URI reference).", + "title": "URL is a custom URL type that allows validation at configuration load time.", "type": "object", - "x-go-package": "net/url" + "x-go-package": "github.com/prometheus/common/config" }, "Userinfo": { "description": "The Userinfo type is an immutable encapsulation of username and\npassword details for a URL. An existing Userinfo value is guaranteed\nto have a username set (potentially empty, as allowed by RFC 2396),\nand optionally a password.", @@ -2491,7 +2480,7 @@ "alerts": { "description": "alerts", "items": { - "$ref": "#/definitions/GettableAlert" + "$ref": "#/definitions/gettableAlert" }, "type": "array", "x-go-name": "Alerts" @@ -2500,7 +2489,7 @@ "$ref": "#/definitions/labelSet" }, "receiver": { - "$ref": "#/definitions/Receiver" + "$ref": "#/definitions/receiver" } }, "required": [ @@ -2508,18 +2497,14 @@ "labels", "receiver" ], - "type": "object", - "x-go-name": "AlertGroup", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "type": "object" }, "alertGroups": { "description": "AlertGroups alert groups", "items": { "$ref": "#/definitions/alertGroup" }, - "type": "array", - "x-go-name": "AlertGroups", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "type": "array" }, "alertStatus": { "description": "AlertStatus alert status", @@ -2639,7 +2624,6 @@ "$ref": "#/definitions/Duration" }, "gettableAlert": { - "description": "GettableAlert gettable alert", "properties": { "annotations": { "$ref": "#/definitions/labelSet" @@ -2667,7 +2651,7 @@ "receivers": { "description": "receivers", "items": { - "$ref": "#/definitions/Receiver" + "$ref": "#/definitions/receiver" }, "type": "array", "x-go-name": "Receivers" @@ -2703,7 +2687,6 @@ "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "gettableAlerts": { - "description": "GettableAlerts gettable alerts", "items": { "$ref": "#/definitions/gettableAlert" }, @@ -2764,12 +2747,9 @@ "status", "updatedAt" ], - "type": "object", - "x-go-name": "GettableSilence", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "type": "object" }, "gettableSilences": { - "description": "GettableSilences gettable silences", "items": { "$ref": "#/definitions/gettableSilence" }, @@ -2904,7 +2884,6 @@ "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "postableSilence": { - "description": "PostableSilence postable silence", "properties": { "comment": { "description": "comment", @@ -2949,7 +2928,6 @@ "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "receiver": { - "description": "Receiver receiver", "properties": { "name": { "description": "name", @@ -3132,9 +3110,9 @@ ], "responses": { "200": { - "description": "GettableAlerts", + "description": "gettableAlerts", "schema": { - "$ref": "#/definitions/GettableAlerts" + "$ref": "#/definitions/gettableAlerts" } }, "400": { @@ -3245,9 +3223,9 @@ ], "responses": { "200": { - "description": "AlertGroups", + "description": "alertGroups", "schema": { - "$ref": "#/definitions/AlertGroups" + "$ref": "#/definitions/alertGroups" } }, "400": { @@ -3319,9 +3297,9 @@ ], "responses": { "200": { - "description": "GettableSilence", + "description": "gettableSilence", "schema": { - "$ref": "#/definitions/GettableSilence" + "$ref": "#/definitions/gettableSilence" } }, "400": { @@ -3360,9 +3338,9 @@ ], "responses": { "200": { - "description": "GettableSilences", + "description": "gettableSilences", "schema": { - "$ref": "#/definitions/GettableSilences" + "$ref": "#/definitions/gettableSilences" } }, "400": { @@ -3397,9 +3375,9 @@ ], "responses": { "201": { - "description": "GettableSilence", + "description": "gettableSilence", "schema": { - "$ref": "#/definitions/GettableSilence" + "$ref": "#/definitions/gettableSilence" } }, "400": { diff --git a/pkg/services/ngalert/api/tooling/spec.json b/pkg/services/ngalert/api/tooling/spec.json index 7483eaf256e..98cd43cc174 100644 --- a/pkg/services/ngalert/api/tooling/spec.json +++ b/pkg/services/ngalert/api/tooling/spec.json @@ -75,9 +75,9 @@ ], "responses": { "200": { - "description": "GettableAlerts", + "description": "gettableAlerts", "schema": { - "$ref": "#/definitions/GettableAlerts" + "$ref": "#/definitions/gettableAlerts" } }, "400": { @@ -188,9 +188,9 @@ ], "responses": { "200": { - "description": "AlertGroups", + "description": "alertGroups", "schema": { - "$ref": "#/definitions/AlertGroups" + "$ref": "#/definitions/alertGroups" } }, "400": { @@ -226,9 +226,9 @@ ], "responses": { "200": { - "description": "GettableSilence", + "description": "gettableSilence", "schema": { - "$ref": "#/definitions/GettableSilence" + "$ref": "#/definitions/gettableSilence" } }, "400": { @@ -303,9 +303,9 @@ ], "responses": { "200": { - "description": "GettableSilences", + "description": "gettableSilences", "schema": { - "$ref": "#/definitions/GettableSilences" + "$ref": "#/definitions/gettableSilences" } }, "400": { @@ -340,9 +340,9 @@ ], "responses": { "201": { - "description": "GettableSilence", + "description": "gettableSilence", "schema": { - "$ref": "#/definitions/GettableSilence" + "$ref": "#/definitions/gettableSilence" } }, "400": { @@ -1035,12 +1035,6 @@ }, "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "AlertGroup": { - "$ref": "#/definitions/AlertGroup" - }, - "AlertGroups": { - "$ref": "#/definitions/AlertGroups" - }, "AlertInstancesResponse": { "type": "object", "properties": { @@ -1490,9 +1484,6 @@ "Failure": { "$ref": "#/definitions/ResponseDetails" }, - "GettableAlert": { - "$ref": "#/definitions/GettableAlert" - }, "GettableAlertmanagers": { "type": "object", "properties": { @@ -1506,9 +1497,6 @@ }, "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "GettableAlerts": { - "$ref": "#/definitions/GettableAlerts" - }, "GettableApiAlertingConfig": { "type": "object", "properties": { @@ -1812,12 +1800,6 @@ }, "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "GettableSilence": { - "$ref": "#/definitions/gettableSilence" - }, - "GettableSilences": { - "$ref": "#/definitions/gettableSilences" - }, "GettableStatus": { "type": "object", "required": [ @@ -2620,9 +2602,6 @@ }, "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, - "PostableSilence": { - "$ref": "#/definitions/PostableSilence" - }, "PostableUserConfig": { "type": "object", "properties": { @@ -2758,7 +2737,7 @@ "x-go-name": "WechatConfigs" } }, - "$ref": "#/definitions/Receiver" + "x-go-package": "github.com/prometheus/alertmanager/config" }, "Regexp": { "description": "A Regexp is safe for concurrent use by multiple goroutines,\nexcept for configuration methods, such as Longest.", @@ -3277,9 +3256,8 @@ "x-go-package": "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" }, "URL": { - "description": "The general form represented is:\n\n[scheme:][//[userinfo@]host][/]path[?query][#fragment]\n\nURLs that do not start with a slash after the scheme are interpreted as:\n\nscheme:opaque[?query][#fragment]\n\nNote that the Path field is stored in decoded form: /%47%6f%2f becomes /Go/.\nA consequence is that it is impossible to tell which slashes in the Path were\nslashes in the raw URL and which were %2f. This distinction is rarely important,\nbut when it is, the code should use RawPath, an optional field which only gets\nset if the default encoding is different from Path.\n\nURL's String method uses the EscapedPath method to obtain the path. See the\nEscapedPath method for more details.", "type": "object", - "title": "A URL represents a parsed URL (technically, a URI reference).", + "title": "URL is a custom URL type that allows validation at configuration load time.", "properties": { "ForceQuery": { "type": "boolean" @@ -3312,7 +3290,7 @@ "$ref": "#/definitions/Userinfo" } }, - "x-go-package": "net/url" + "x-go-package": "github.com/prometheus/common/config" }, "Userinfo": { "description": "The Userinfo type is an immutable encapsulation of username and\npassword details for a URL. An existing Userinfo value is guaranteed\nto have a username set (potentially empty, as allowed by RFC 2396),\nand optionally a password.", @@ -3491,7 +3469,7 @@ "description": "alerts", "type": "array", "items": { - "$ref": "#/definitions/GettableAlert" + "$ref": "#/definitions/gettableAlert" }, "x-go-name": "Alerts" }, @@ -3499,11 +3477,10 @@ "$ref": "#/definitions/labelSet" }, "receiver": { - "$ref": "#/definitions/Receiver" + "$ref": "#/definitions/receiver" } }, - "x-go-name": "AlertGroup", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "$ref": "#/definitions/alertGroup" }, "alertGroups": { "description": "AlertGroups alert groups", @@ -3511,8 +3488,7 @@ "items": { "$ref": "#/definitions/alertGroup" }, - "x-go-name": "AlertGroups", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "$ref": "#/definitions/alertGroups" }, "alertStatus": { "description": "AlertStatus alert status", @@ -3632,7 +3608,6 @@ "$ref": "#/definitions/Duration" }, "gettableAlert": { - "description": "GettableAlert gettable alert", "type": "object", "required": [ "labels", @@ -3672,7 +3647,7 @@ "description": "receivers", "type": "array", "items": { - "$ref": "#/definitions/Receiver" + "$ref": "#/definitions/receiver" }, "x-go-name": "Receivers" }, @@ -3693,16 +3668,17 @@ } }, "x-go-name": "GettableAlert", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", + "$ref": "#/definitions/gettableAlert" }, "gettableAlerts": { - "description": "GettableAlerts gettable alerts", "type": "array", "items": { "$ref": "#/definitions/gettableAlert" }, "x-go-name": "GettableAlerts", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", + "$ref": "#/definitions/gettableAlerts" }, "gettableSilence": { "description": "GettableSilence gettable silence", @@ -3758,17 +3734,16 @@ "x-go-name": "UpdatedAt" } }, - "x-go-name": "GettableSilence", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "$ref": "#/definitions/gettableSilence" }, "gettableSilences": { - "description": "GettableSilences gettable silences", "type": "array", "items": { "$ref": "#/definitions/gettableSilence" }, "x-go-name": "GettableSilences", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", + "$ref": "#/definitions/gettableSilences" }, "labelSet": { "description": "LabelSet label set", @@ -3897,7 +3872,6 @@ "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" }, "postableSilence": { - "description": "PostableSilence postable silence", "type": "object", "required": [ "comment", @@ -3939,10 +3913,10 @@ } }, "x-go-name": "PostableSilence", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", + "$ref": "#/definitions/postableSilence" }, "receiver": { - "description": "Receiver receiver", "type": "object", "required": [ "name" @@ -3955,7 +3929,8 @@ } }, "x-go-name": "Receiver", - "x-go-package": "github.com/prometheus/alertmanager/api/v2/models" + "x-go-package": "github.com/prometheus/alertmanager/api/v2/models", + "$ref": "#/definitions/receiver" }, "silence": { "description": "Silence silence",