From 5c2077f3282d493220cc9689e9a6a83ee4914b97 Mon Sep 17 00:00:00 2001 From: Domas Date: Thu, 18 Feb 2021 10:35:48 +0200 Subject: [PATCH 1/2] align config endpoint with cortex, include templates --- pkg/api/receivers.go | 9 +++++++-- spec.json | 38 ++++++++++++++++++++++++-------------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pkg/api/receivers.go b/pkg/api/receivers.go index 5b2643c44e9..3a116757a51 100644 --- a/pkg/api/receivers.go +++ b/pkg/api/receivers.go @@ -32,7 +32,13 @@ import ( // swagger:parameters RoutePostAlertingConfig type BodyAlertingConfig struct { // in:body - Body AlertingConfig + Body UserConfig +} + +// swagger:model +type UserConfig struct { + TemplateFiles map[string]string `yaml:"template_files" json:"template_files"` + AlertmanagerConfig AlertingConfig `yaml:"alertmanager_config" json:"alertmanager_config"` } // swagger:model @@ -40,7 +46,6 @@ type AlertingConfigResponse struct { BodyAlertingConfig } -// swagger:model type AlertingConfig struct { config.Config diff --git a/spec.json b/spec.json index ea6cda35007..1335d296304 100644 --- a/spec.json +++ b/spec.json @@ -157,7 +157,7 @@ "name": "Body", "in": "body", "schema": { - "$ref": "#/definitions/AlertingConfig" + "$ref": "#/definitions/UserConfig" } } ], @@ -392,7 +392,7 @@ "type": "object", "properties": { "Body": { - "$ref": "#/definitions/AlertingConfig" + "$ref": "#/definitions/UserConfig" } }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" @@ -480,7 +480,7 @@ "type": "object", "properties": { "Body": { - "$ref": "#/definitions/AlertingConfig" + "$ref": "#/definitions/UserConfig" } }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" @@ -635,9 +635,10 @@ "x-go-package": "github.com/grafana/grafana/pkg/models" }, "Duration": { - "description": "A Duration represents the elapsed time between two instants\nas an int64 nanosecond count. The representation limits the\nlargest representable duration to approximately 290 years.", + "description": "Duration wraps time.Duration. It is used to parse the custom duration format\nfrom YAML.\nThis type should not propagate beyond the scope of input/output processing.", "type": "integer", "format": "int64", + "x-go-package": "time", "$ref": "#/definitions/Duration" }, "ElementType": { @@ -1285,12 +1286,6 @@ "type": "object", "title": "Receiver configuration provides configuration on how to contact a receiver.", "properties": { - "GrafanaManagedReceivers": { - "type": "array", - "items": { - "$ref": "#/definitions/GrafanaReceiver" - } - }, "email_configs": { "type": "array", "items": { @@ -1353,7 +1348,7 @@ "x-go-name": "WechatConfigs" } }, - "x-go-package": "github.com/grafana/alerting-api/pkg/api" + "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.", @@ -1831,9 +1826,8 @@ "x-go-package": "github.com/prometheus/common/config" }, "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" @@ -1866,12 +1860,28 @@ "$ref": "#/definitions/Userinfo" } }, - "x-go-package": "net/url" + "x-go-package": "github.com/prometheus/common/config" }, "Unauthorized": { "type": "object", "x-go-package": "github.com/grafana/alerting-api/pkg/models" }, + "UserConfig": { + "type": "object", + "properties": { + "alertmanager_config": { + "$ref": "#/definitions/AlertingConfig" + }, + "template_files": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "x-go-name": "TemplateFiles" + } + }, + "x-go-package": "github.com/grafana/alerting-api/pkg/api" + }, "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.", "type": "object", From b51a59a59f085bd6dd8752d851b0f034e50625c6 Mon Sep 17 00:00:00 2001 From: Domas Date: Thu, 18 Feb 2021 12:41:05 +0200 Subject: [PATCH 2/2] rename struct to stop swagger thrashing --- pkg/api/receivers.go | 10 +-- spec.json | 147 ++++++++++++++++++++++++++++++++----------- 2 files changed, 115 insertions(+), 42 deletions(-) diff --git a/pkg/api/receivers.go b/pkg/api/receivers.go index 3a116757a51..d8f630fe8a2 100644 --- a/pkg/api/receivers.go +++ b/pkg/api/receivers.go @@ -38,7 +38,7 @@ type BodyAlertingConfig struct { // swagger:model type UserConfig struct { TemplateFiles map[string]string `yaml:"template_files" json:"template_files"` - AlertmanagerConfig AlertingConfig `yaml:"alertmanager_config" json:"alertmanager_config"` + AlertmanagerConfig ApiAlertingConfig `yaml:"alertmanager_config" json:"alertmanager_config"` } // swagger:model @@ -46,16 +46,16 @@ type AlertingConfigResponse struct { BodyAlertingConfig } -type AlertingConfig struct { +type ApiAlertingConfig struct { config.Config // Override with our superset receiver type - Receivers []*Receiver `yaml:"receivers,omitempty" json:"receivers,omitempty"` + Receivers []*ApiReceiver `yaml:"receivers,omitempty" json:"receivers,omitempty"` } type GrafanaReceiver alerting.NotifierPlugin -type Receiver struct { +type ApiReceiver struct { config.Receiver - GrafanaManagedReceivers []*GrafanaReceiver + GrafanaManagedReceivers []*GrafanaReceiver `yaml:"grafana_managed_receiver_configs,omitempty" json:"grafana_managed_receiver_configs,omitempty"` } diff --git a/spec.json b/spec.json index 1335d296304..b409e4e973d 100644 --- a/spec.json +++ b/spec.json @@ -354,40 +354,6 @@ }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" }, - "AlertingConfig": { - "type": "object", - "properties": { - "global": { - "$ref": "#/definitions/GlobalConfig" - }, - "inhibit_rules": { - "type": "array", - "items": { - "$ref": "#/definitions/InhibitRule" - }, - "x-go-name": "InhibitRules" - }, - "receivers": { - "description": "Override with our superset receiver type", - "type": "array", - "items": { - "$ref": "#/definitions/Receiver" - }, - "x-go-name": "Receivers" - }, - "route": { - "$ref": "#/definitions/Route" - }, - "templates": { - "type": "array", - "items": { - "type": "string" - }, - "x-go-name": "Templates" - } - }, - "x-go-package": "github.com/grafana/alerting-api/pkg/api" - }, "AlertingConfigResponse": { "type": "object", "properties": { @@ -456,6 +422,114 @@ }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" }, + "ApiAlertingConfig": { + "type": "object", + "properties": { + "global": { + "$ref": "#/definitions/GlobalConfig" + }, + "inhibit_rules": { + "type": "array", + "items": { + "$ref": "#/definitions/InhibitRule" + }, + "x-go-name": "InhibitRules" + }, + "receivers": { + "description": "Override with our superset receiver type", + "type": "array", + "items": { + "$ref": "#/definitions/ApiReceiver" + }, + "x-go-name": "Receivers" + }, + "route": { + "$ref": "#/definitions/Route" + }, + "templates": { + "type": "array", + "items": { + "type": "string" + }, + "x-go-name": "Templates" + } + }, + "x-go-package": "github.com/grafana/alerting-api/pkg/api" + }, + "ApiReceiver": { + "type": "object", + "properties": { + "email_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/EmailConfig" + }, + "x-go-name": "EmailConfigs" + }, + "grafana_managed_receiver_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/GrafanaReceiver" + }, + "x-go-name": "GrafanaManagedReceivers" + }, + "name": { + "description": "A unique identifier for this receiver.", + "type": "string", + "x-go-name": "Name" + }, + "opsgenie_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/OpsGenieConfig" + }, + "x-go-name": "OpsGenieConfigs" + }, + "pagerduty_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/PagerdutyConfig" + }, + "x-go-name": "PagerdutyConfigs" + }, + "pushover_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/PushoverConfig" + }, + "x-go-name": "PushoverConfigs" + }, + "slack_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/SlackConfig" + }, + "x-go-name": "SlackConfigs" + }, + "victorops_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/VictorOpsConfig" + }, + "x-go-name": "VictorOpsConfigs" + }, + "webhook_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/WebhookConfig" + }, + "x-go-name": "WebhookConfigs" + }, + "wechat_configs": { + "type": "array", + "items": { + "$ref": "#/definitions/WechatConfig" + }, + "x-go-name": "WechatConfigs" + } + }, + "x-go-package": "github.com/grafana/alerting-api/pkg/api" + }, "BadRequest": { "type": "object", "x-go-package": "github.com/grafana/alerting-api/pkg/models" @@ -635,10 +709,9 @@ "x-go-package": "github.com/grafana/grafana/pkg/models" }, "Duration": { - "description": "Duration wraps time.Duration. It is used to parse the custom duration format\nfrom YAML.\nThis type should not propagate beyond the scope of input/output processing.", + "description": "A Duration represents the elapsed time between two instants\nas an int64 nanosecond count. The representation limits the\nlargest representable duration to approximately 290 years.", "type": "integer", "format": "int64", - "x-go-package": "time", "$ref": "#/definitions/Duration" }, "ElementType": { @@ -1870,7 +1943,7 @@ "type": "object", "properties": { "alertmanager_config": { - "$ref": "#/definitions/AlertingConfig" + "$ref": "#/definitions/ApiAlertingConfig" }, "template_files": { "type": "object",