From 40312c527be9b9963e0ecda036826ca551ea6352 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 19 Jan 2024 14:37:11 -0500 Subject: [PATCH] ngalert openapi: Fix ObjectMatchers definition (#79477) These don't get marshalled and unmarshalled in the same way as they are represented in Go This PR changes the OpenAPI spec to reflect what the API accepts and sends back --- pkg/services/ngalert/api/tooling/api.json | 17 +++++++++++---- .../api/tooling/definitions/alertmanager.go | 21 +++++++++++++------ pkg/services/ngalert/api/tooling/post.json | 15 ++++++++++--- pkg/services/ngalert/api/tooling/spec.json | 15 ++++++++++--- public/api-merged.json | 17 +++++++++++---- public/openapi3.json | 16 +++++++++++--- 6 files changed, 78 insertions(+), 23 deletions(-) diff --git a/pkg/services/ngalert/api/tooling/api.json b/pkg/services/ngalert/api/tooling/api.json index cf77940b62e..0a9a87eba07 100644 --- a/pkg/services/ngalert/api/tooling/api.json +++ b/pkg/services/ngalert/api/tooling/api.json @@ -2270,9 +2270,18 @@ "title": "OAuth2 is the oauth2 client configuration.", "type": "object" }, + "ObjectMatcher": { + "items": { + "type": "string" + }, + "title": "ObjectMatcher is a matcher that can be used to filter alerts.", + "type": "array" + }, "ObjectMatchers": { - "$ref": "#/definitions/Matchers", - "description": "ObjectMatchers is Matchers with a different Unmarshal and Marshal methods that accept matchers as objects\nthat have already been parsed." + "items": { + "$ref": "#/definitions/ObjectMatcher" + }, + "type": "array" }, "OpsGenieConfig": { "properties": { @@ -4414,6 +4423,7 @@ "type": "object" }, "alertGroup": { + "description": "AlertGroup alert group", "properties": { "alerts": { "description": "alerts", @@ -4542,7 +4552,6 @@ "type": "object" }, "gettableAlert": { - "description": "GettableAlert gettable alert", "properties": { "annotations": { "$ref": "#/definitions/labelSet" @@ -4652,13 +4661,13 @@ "type": "object" }, "gettableSilences": { + "description": "GettableSilences gettable silences", "items": { "$ref": "#/definitions/gettableSilence" }, "type": "array" }, "integration": { - "description": "Integration integration", "properties": { "lastNotifyAttempt": { "description": "A timestamp indicating the last attempt to deliver a notification regardless of the outcome.\nFormat: date-time", diff --git a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go index a6fe8912455..2013c5daea2 100644 --- a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go +++ b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go @@ -1273,13 +1273,22 @@ type PostableGrafanaReceivers struct { type EncryptFn func(ctx context.Context, payload []byte) ([]byte, error) +// ObjectMatcher is a matcher that can be used to filter alerts. +// swagger:model ObjectMatcher +type ObjectMatcherAPIModel [3]string + +// ObjectMatchers is a list of matchers that can be used to filter alerts. +// swagger:model ObjectMatchers +type ObjectMatchersAPIModel []ObjectMatcherAPIModel + +// swagger:ignore // ObjectMatchers is Matchers with a different Unmarshal and Marshal methods that accept matchers as objects // that have already been parsed. type ObjectMatchers labels.Matchers // UnmarshalYAML implements the yaml.Unmarshaler interface for Matchers. func (m *ObjectMatchers) UnmarshalYAML(unmarshal func(interface{}) error) error { - var rawMatchers [][3]string + var rawMatchers ObjectMatchersAPIModel if err := unmarshal(&rawMatchers); err != nil { return err } @@ -1326,7 +1335,7 @@ func (m *ObjectMatchers) UnmarshalYAML(unmarshal func(interface{}) error) error // UnmarshalJSON implements the json.Unmarshaler interface for Matchers. func (m *ObjectMatchers) UnmarshalJSON(data []byte) error { - var rawMatchers [][3]string + var rawMatchers ObjectMatchersAPIModel if err := json.Unmarshal(data, &rawMatchers); err != nil { return err } @@ -1360,9 +1369,9 @@ func (m *ObjectMatchers) UnmarshalJSON(data []byte) error { // MarshalYAML implements the yaml.Marshaler interface for Matchers. func (m ObjectMatchers) MarshalYAML() (interface{}, error) { - result := make([][3]string, len(m)) + result := make(ObjectMatchersAPIModel, len(m)) for i, matcher := range m { - result[i] = [3]string{matcher.Name, matcher.Type.String(), matcher.Value} + result[i] = ObjectMatcherAPIModel{matcher.Name, matcher.Type.String(), matcher.Value} } return result, nil } @@ -1372,9 +1381,9 @@ func (m ObjectMatchers) MarshalJSON() ([]byte, error) { if len(m) == 0 { return nil, nil } - result := make([][3]string, len(m)) + result := make(ObjectMatchersAPIModel, len(m)) for i, matcher := range m { - result[i] = [3]string{matcher.Name, matcher.Type.String(), matcher.Value} + result[i] = ObjectMatcherAPIModel{matcher.Name, matcher.Type.String(), matcher.Value} } return json.Marshal(result) } diff --git a/pkg/services/ngalert/api/tooling/post.json b/pkg/services/ngalert/api/tooling/post.json index 662c0a6c36e..c3d72b28d26 100644 --- a/pkg/services/ngalert/api/tooling/post.json +++ b/pkg/services/ngalert/api/tooling/post.json @@ -2270,9 +2270,18 @@ "title": "OAuth2 is the oauth2 client configuration.", "type": "object" }, + "ObjectMatcher": { + "items": { + "type": "string" + }, + "title": "ObjectMatcher is a matcher that can be used to filter alerts.", + "type": "array" + }, "ObjectMatchers": { - "$ref": "#/definitions/Matchers", - "description": "ObjectMatchers is Matchers with a different Unmarshal and Marshal methods that accept matchers as objects\nthat have already been parsed." + "items": { + "$ref": "#/definitions/ObjectMatcher" + }, + "type": "array" }, "OpsGenieConfig": { "properties": { @@ -4603,7 +4612,6 @@ "type": "array" }, "gettableSilence": { - "description": "GettableSilence gettable silence", "properties": { "comment": { "description": "comment", @@ -4652,6 +4660,7 @@ "type": "object" }, "gettableSilences": { + "description": "GettableSilences gettable silences", "items": { "$ref": "#/definitions/gettableSilence" }, diff --git a/pkg/services/ngalert/api/tooling/spec.json b/pkg/services/ngalert/api/tooling/spec.json index 8905f106187..a67c47a68a0 100644 --- a/pkg/services/ngalert/api/tooling/spec.json +++ b/pkg/services/ngalert/api/tooling/spec.json @@ -5695,9 +5695,18 @@ } } }, + "ObjectMatcher": { + "type": "array", + "title": "ObjectMatcher is a matcher that can be used to filter alerts.", + "items": { + "type": "string" + } + }, "ObjectMatchers": { - "description": "ObjectMatchers is Matchers with a different Unmarshal and Marshal methods that accept matchers as objects\nthat have already been parsed.", - "$ref": "#/definitions/Matchers" + "type": "array", + "items": { + "$ref": "#/definitions/ObjectMatcher" + } }, "OpsGenieConfig": { "type": "object", @@ -8032,7 +8041,6 @@ "$ref": "#/definitions/gettableAlerts" }, "gettableSilence": { - "description": "GettableSilence gettable silence", "type": "object", "required": [ "comment", @@ -8082,6 +8090,7 @@ "$ref": "#/definitions/gettableSilence" }, "gettableSilences": { + "description": "GettableSilences gettable silences", "type": "array", "items": { "$ref": "#/definitions/gettableSilence" diff --git a/public/api-merged.json b/public/api-merged.json index 63c4e95df12..ad2ae396d5a 100644 --- a/public/api-merged.json +++ b/public/api-merged.json @@ -16751,9 +16751,18 @@ "format": "int64" } }, + "ObjectMatcher": { + "type": "array", + "title": "ObjectMatcher is a matcher that can be used to filter alerts.", + "items": { + "type": "string" + } + }, "ObjectMatchers": { - "description": "ObjectMatchers is Matchers with a different Unmarshal and Marshal methods that accept matchers as objects\nthat have already been parsed.", - "$ref": "#/definitions/Matchers" + "type": "array", + "items": { + "$ref": "#/definitions/ObjectMatcher" + } }, "OpsGenieConfig": { "type": "object", @@ -21454,6 +21463,7 @@ } }, "alertGroup": { + "description": "AlertGroup alert group", "type": "object", "required": [ "alerts", @@ -21610,7 +21620,6 @@ } }, "gettableAlert": { - "description": "GettableAlert gettable alert", "type": "object", "required": [ "labels", @@ -21720,13 +21729,13 @@ } }, "gettableSilences": { + "description": "GettableSilences gettable silences", "type": "array", "items": { "$ref": "#/definitions/gettableSilence" } }, "integration": { - "description": "Integration integration", "type": "object", "required": [ "name", diff --git a/public/openapi3.json b/public/openapi3.json index c42dfd3a8b1..d7a2ea385dd 100644 --- a/public/openapi3.json +++ b/public/openapi3.json @@ -7275,8 +7275,18 @@ "title": "An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.", "type": "array" }, + "ObjectMatcher": { + "items": { + "type": "string" + }, + "title": "ObjectMatcher is a matcher that can be used to filter alerts.", + "type": "array" + }, "ObjectMatchers": { - "$ref": "#/components/schemas/Matchers" + "items": { + "$ref": "#/components/schemas/ObjectMatcher" + }, + "type": "array" }, "OpsGenieConfig": { "properties": { @@ -11976,6 +11986,7 @@ "type": "object" }, "alertGroup": { + "description": "AlertGroup alert group", "properties": { "alerts": { "description": "alerts", @@ -12132,7 +12143,6 @@ "type": "object" }, "gettableAlert": { - "description": "GettableAlert gettable alert", "properties": { "annotations": { "$ref": "#/components/schemas/labelSet" @@ -12242,13 +12252,13 @@ "type": "object" }, "gettableSilences": { + "description": "GettableSilences gettable silences", "items": { "$ref": "#/components/schemas/gettableSilence" }, "type": "array" }, "integration": { - "description": "Integration integration", "properties": { "lastNotifyAttempt": { "description": "A timestamp indicating the last attempt to deliver a notification regardless of the outcome.\nFormat: date-time",