From e0109cdebdba50e0930d36a0f7a1cae693a871a3 Mon Sep 17 00:00:00 2001 From: Domas Date: Fri, 19 Feb 2021 16:01:16 +0200 Subject: [PATCH] make ruler json again --- pkg/api/cortex-ruler.go | 32 +++++--- spec.json | 178 ++++++++++++++-------------------------- 2 files changed, 81 insertions(+), 129 deletions(-) diff --git a/pkg/api/cortex-ruler.go b/pkg/api/cortex-ruler.go index a4b243ff083..57d2e0a0a08 100644 --- a/pkg/api/cortex-ruler.go +++ b/pkg/api/cortex-ruler.go @@ -2,58 +2,59 @@ package api import ( "github.com/grafana/grafana/pkg/services/ngalert/eval" + promapi "github.com/prometheus/client_golang/api/prometheus/v1/api" "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/rulefmt" ) -// swagger:route Get /api/v1/rules RulerConfig RouteGetRulesConfig +// swagger:route Get /api/v1/rules ruler RouteGetRulesConfig // // List rule groups // // Produces: -// - application/yaml +// - application/json // // Responses: // 202: NamespaceConfigResponse -// swagger:route POST /api/v1/rules/{Namespace} RulerConfig RoutePostNameRulesConfig +// swagger:route POST /api/v1/rules/{Namespace} ruler RoutePostNameRulesConfig // // Creates or updates a rule group // // Consumes: -// - application/yaml +// - application/json // // Responses: // 202: Ack -// swagger:route Get /api/v1/rules/{Namespace} RulerConfig RouteGetNamespaceRulesConfig +// swagger:route Get /api/v1/rules/{Namespace} ruler RouteGetNamespaceRulesConfig // // Get rule groups by namespace // // Produces: -// - application/yaml +// - application/json // // Responses: // 202: NamespaceConfigResponse -// swagger:route Delete /api/v1/rules/{Namespace} RulerConfig RouteDeleteNamespaceRulesConfig +// swagger:route Delete /api/v1/rules/{Namespace} ruler RouteDeleteNamespaceRulesConfig // // Delete namespace // // Responses: // 202: Ack -// swagger:route Get /api/v1/rules/{Namespace}/{Groupname} RulerConfig RouteGetRulegGroupConfig +// swagger:route Get /api/v1/rules/{Namespace}/{Groupname} ruler RouteGetRulegGroupConfig // // Get rule group // // Produces: -// - application/yaml +// - application/json // // Responses: // 202: RuleGroupConfigResponse -// swagger:route Delete /api/v1/rules/{Namespace}/{Groupname} RulerConfig RouteDeleteRuleGroupConfig +// swagger:route Delete /api/v1/rules/{Namespace}/{Groupname} ruler RouteDeleteRuleGroupConfig // // Delete rule group // @@ -97,8 +98,17 @@ type RuleGroupConfig struct { Rules []ExtendedRuleNode `yaml:"rules" json:"rules"` } +type ApiRuleNode struct { + Record string `yaml:"record,omitempty" json:"record,omitempty"` + Alert string `yaml:"alert,omitempty" json:"alert,omitempty"` + Expr string `yaml:"expr" json:"expr"` + For model.Duration `yaml:"for,omitempty" json:"for,omitempty"` + Labels map[string]string `yaml:"labels,omitempty" json:"labels,omitempty"` + Annotations map[string]string `yaml:"annotations,omitempty" json:"annotations,omitempty"` +} + type ExtendedRuleNode struct { - rulefmt.RuleNode + ApiRuleNode //GrafanaManagedAlert yaml.Node `yaml:"grafana_alert,omitempty"` GrafanaManagedAlert ExtendedUpsertAlertDefinitionCommand `yaml:"grafana_alert,omitempty" json:"grafana_alert,omitempty"` } diff --git a/spec.json b/spec.json index 597f1c00e47..13bceb40dcc 100644 --- a/spec.json +++ b/spec.json @@ -296,7 +296,7 @@ "get": { "description": "List rule groups", "produces": [ - "application/yaml" + "application/json" ], "tags": [ "RulerConfig" @@ -316,7 +316,7 @@ "get": { "description": "Get rule groups by namespace", "produces": [ - "application/yaml" + "application/json" ], "tags": [ "RulerConfig" @@ -342,7 +342,7 @@ "post": { "description": "Creates or updates a rule group", "consumes": [ - "application/yaml" + "application/json" ], "tags": [ "RulerConfig" @@ -400,7 +400,7 @@ "get": { "description": "Get rule group", "produces": [ - "application/yaml" + "application/json" ], "tags": [ "RulerConfig" @@ -819,6 +819,41 @@ }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" }, + "ApiRuleNode": { + "type": "object", + "properties": { + "alert": { + "type": "string", + "x-go-name": "Alert" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "x-go-name": "Annotations" + }, + "expr": { + "type": "string", + "x-go-name": "Expr" + }, + "for": { + "$ref": "#/definitions/Duration" + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "x-go-name": "Labels" + }, + "record": { + "type": "string", + "x-go-name": "Record" + } + }, + "x-go-package": "github.com/grafana/alerting-api/pkg/api" + }, "BasicAuth": { "type": "object", "title": "BasicAuth contains basic HTTP authentication credentials.", @@ -1030,32 +1065,37 @@ "ExtendedRuleNode": { "type": "object", "properties": { - "Alert": { - "$ref": "#/definitions/Node" + "alert": { + "type": "string", + "x-go-name": "Alert" }, - "Annotations": { + "annotations": { "type": "object", "additionalProperties": { "type": "string" - } + }, + "x-go-name": "Annotations" }, - "Expr": { - "$ref": "#/definitions/Node" + "expr": { + "type": "string", + "x-go-name": "Expr" }, - "For": { + "for": { "$ref": "#/definitions/Duration" }, - "Labels": { - "type": "object", - "additionalProperties": { - "type": "string" - } - }, - "Record": { - "$ref": "#/definitions/Node" - }, "grafana_alert": { "$ref": "#/definitions/ExtendedUpsertAlertDefinitionCommand" + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "x-go-name": "Labels" + }, + "record": { + "type": "string", + "x-go-name": "Record" } }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" @@ -1268,11 +1308,6 @@ "type": "object", "x-go-package": "github.com/grafana/grafana/pkg/components/simplejson" }, - "Kind": { - "type": "integer", - "format": "uint32", - "x-go-package": "gopkg.in/yaml.v3" - }, "LabelName": { "description": "A LabelName is a key for a LabelSet or Metric. It has a value associated\ntherewith.", "type": "string", @@ -1331,63 +1366,6 @@ }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" }, - "Node": { - "description": "It's worth noting that although Node offers access into details such as\nline numbers, colums, and comments, the content when re-encoded will not\nhave its original textual representation preserved. An effort is made to\nrender the data plesantly, and to preserve comments near the data they\ndescribe, though.\n\nValues that make use of the Node type interact with the yaml package in the\nsame way any other type would do, by encoding and decoding yaml data\ndirectly or indirectly into them.\n\nFor example:\n\nvar person struct {\nName string\nAddress yaml.Node\n}\nerr := yaml.Unmarshal(data, \u0026person)\n\nOr by itself:\n\nvar person Node\nerr := yaml.Unmarshal(data, \u0026person)", - "type": "object", - "title": "Node represents an element in the YAML document hierarchy. While documents\nare typically encoded and decoded into higher level types, such as structs\nand maps, Node is an intermediate representation that allows detailed\ncontrol over the content being decoded or encoded.", - "properties": { - "Alias": { - "$ref": "#/definitions/Node" - }, - "Anchor": { - "description": "Anchor holds the anchor name for this node, which allows aliases to point to it.", - "type": "string" - }, - "Column": { - "type": "integer", - "format": "int64" - }, - "Content": { - "description": "Content holds contained nodes for documents, mappings, and sequences.", - "type": "array", - "items": { - "$ref": "#/definitions/Node" - } - }, - "FootComment": { - "description": "FootComment holds any comments following the node and before empty lines.", - "type": "string" - }, - "HeadComment": { - "description": "HeadComment holds any comments in the lines preceding the node and\nnot separated by an empty line.", - "type": "string" - }, - "Kind": { - "$ref": "#/definitions/Kind" - }, - "Line": { - "description": "Line and Column hold the node position in the decoded YAML text.\nThese fields are not respected when encoding the node.", - "type": "integer", - "format": "int64" - }, - "LineComment": { - "description": "LineComment holds any comments at the end of the line where the node is in.", - "type": "string" - }, - "Style": { - "$ref": "#/definitions/Style" - }, - "Tag": { - "description": "Tag holds the YAML tag defining the data type for the value.\nWhen decoding, this field will always be set to the resolved tag,\neven when it wasn't explicitly provided in the YAML content.\nWhen encoding, if this field is unset the value type will be\nimplied from the node properties, and if it is set, it will only\nbe serialized into the representation if TaggedStyle is used or\nthe implicit tag diverges from the provided one.", - "type": "string" - }, - "Value": { - "description": "Value holds the unescaped and unquoted represenation of the value.", - "type": "string" - } - }, - "x-go-package": "gopkg.in/yaml.v3" - }, "NotifierConfig": { "type": "object", "title": "NotifierConfig contains base options common across all notifier configurations.", @@ -1924,37 +1902,6 @@ }, "x-go-package": "github.com/grafana/alerting-api/pkg/api" }, - "RuleNode": { - "type": "object", - "title": "RuleNode adds yaml.v3 layer to support line and column outputs for invalid rules.", - "properties": { - "Alert": { - "$ref": "#/definitions/Node" - }, - "Annotations": { - "type": "object", - "additionalProperties": { - "type": "string" - } - }, - "Expr": { - "$ref": "#/definitions/Node" - }, - "For": { - "$ref": "#/definitions/Duration" - }, - "Labels": { - "type": "object", - "additionalProperties": { - "type": "string" - } - }, - "Record": { - "$ref": "#/definitions/Node" - } - }, - "x-go-package": "github.com/prometheus/prometheus/pkg/rulefmt" - }, "RuleResponse": { "type": "object", "properties": { @@ -2188,11 +2135,6 @@ }, "x-go-package": "github.com/prometheus/alertmanager/config" }, - "Style": { - "type": "integer", - "format": "uint32", - "x-go-package": "gopkg.in/yaml.v3" - }, "TLSConfig": { "type": "object", "title": "TLSConfig configures the options for TLS connections.",