Alerting: Use correct response body for silence post API (#57114)

This commit is contained in:
Yuriy Tseretyan 2022-10-17 15:43:37 -04:00 committed by GitHub
parent 46fb4081ba
commit 888bdfd4ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 88 additions and 28 deletions

View File

@ -10,6 +10,7 @@ import (
"time"
"github.com/go-openapi/strfmt"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
@ -85,7 +86,9 @@ func (srv AlertmanagerSrv) RouteCreateSilence(c *models.ReqContext, postableSile
return ErrResp(http.StatusInternalServerError, err, "failed to create silence")
}
return response.JSON(http.StatusAccepted, util.DynMap{"message": "silence created", "id": silenceID})
return response.JSON(http.StatusAccepted, apimodels.PostSilencesOKBody{
SilenceID: silenceID,
})
}
func (srv AlertmanagerSrv) RouteDeleteAlertingConfig(c *models.ReqContext) response.Response {

View File

@ -133,7 +133,7 @@ func (am *LotexAM) RouteCreateSilence(ctx *models.ReqContext, silenceBody apimod
"silences",
nil,
bytes.NewBuffer(blob),
jsonExtractor(&apimodels.GettableSilence{}),
jsonExtractor(&apimodels.PostSilencesOKBody{}),
map[string]string{"Content-Type": "application/json"},
)
}

View File

@ -413,6 +413,9 @@
"auth_password": {
"$ref": "#/definitions/Secret"
},
"auth_password_file": {
"type": "string"
},
"auth_secret": {
"$ref": "#/definitions/Secret"
},
@ -1161,6 +1164,9 @@
"smtp_auth_password": {
"$ref": "#/definitions/Secret"
},
"smtp_auth_password_file": {
"type": "string"
},
"smtp_auth_secret": {
"$ref": "#/definitions/Secret"
},
@ -3035,6 +3041,9 @@
},
"type": "array"
},
"location": {
"type": "string"
},
"months": {
"items": {
"type": "string"
@ -3269,7 +3278,6 @@
"type": "object"
},
"alertGroup": {
"description": "AlertGroup alert group",
"properties": {
"alerts": {
"description": "alerts",
@ -3293,7 +3301,6 @@
"type": "object"
},
"alertGroups": {
"description": "AlertGroups alert groups",
"items": {
"$ref": "#/definitions/alertGroup"
},
@ -3454,7 +3461,6 @@
"type": "object"
},
"gettableAlerts": {
"description": "GettableAlerts gettable alerts",
"items": {
"$ref": "#/definitions/gettableAlert"
},
@ -3510,13 +3516,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",
@ -3613,6 +3619,15 @@
],
"type": "object"
},
"postSilencesOKBody": {
"properties": {
"silenceID": {
"description": "silence ID",
"type": "string"
}
},
"type": "object"
},
"postableAlert": {
"description": "PostableAlert postable alert",
"properties": {

View File

@ -199,7 +199,7 @@ import (
// create silence
//
// Responses:
// 201: gettableSilence
// 201: postSilencesOKBody
// 400: ValidationError
// swagger:route POST /api/alertmanager/{DatasourceUID}/api/v2/silences alertmanager RouteCreateSilence
@ -207,7 +207,7 @@ import (
// create silence
//
// Responses:
// 201: gettableSilence
// 201: postSilencesOKBody
// 400: ValidationError
// 404: NotFound
@ -389,6 +389,12 @@ func NewGettableStatus(cfg *PostableApiAlertingConfig) *GettableStatus {
// swagger:model postableSilence
type PostableSilence = amv2.PostableSilence
// swagger:model postSilencesOKBody
type PostSilencesOKBody struct { // vendored from "github.com/prometheus/alertmanager/api/v2/restapi/operations/silence/PostSilencesOKBody" because import brings too many other things
// silence ID
SilenceID string `json:"silenceID,omitempty"`
}
// swagger:model gettableSilences
type GettableSilences = amv2.GettableSilences

View File

@ -413,6 +413,9 @@
"auth_password": {
"$ref": "#/definitions/Secret"
},
"auth_password_file": {
"type": "string"
},
"auth_secret": {
"$ref": "#/definitions/Secret"
},
@ -1161,6 +1164,9 @@
"smtp_auth_password": {
"$ref": "#/definitions/Secret"
},
"smtp_auth_password_file": {
"type": "string"
},
"smtp_auth_secret": {
"$ref": "#/definitions/Secret"
},
@ -3035,6 +3041,9 @@
},
"type": "array"
},
"location": {
"type": "string"
},
"months": {
"items": {
"type": "string"
@ -3078,7 +3087,6 @@
"type": "object"
},
"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"
@ -3114,7 +3122,7 @@
"$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"
},
"Userinfo": {
@ -3293,6 +3301,7 @@
"type": "object"
},
"alertGroups": {
"description": "AlertGroups alert groups",
"items": {
"$ref": "#/definitions/alertGroup"
},
@ -3397,6 +3406,7 @@
"type": "object"
},
"gettableAlert": {
"description": "GettableAlert gettable alert",
"properties": {
"annotations": {
"$ref": "#/definitions/labelSet"
@ -3452,7 +3462,6 @@
"type": "object"
},
"gettableAlerts": {
"description": "GettableAlerts gettable alerts",
"items": {
"$ref": "#/definitions/gettableAlert"
},
@ -3507,6 +3516,7 @@
"type": "object"
},
"gettableSilences": {
"description": "GettableSilences gettable silences",
"items": {
"$ref": "#/definitions/gettableSilence"
},
@ -3610,6 +3620,15 @@
],
"type": "object"
},
"postSilencesOKBody": {
"properties": {
"silenceID": {
"description": "silence ID",
"type": "string"
}
},
"type": "object"
},
"postableAlert": {
"description": "PostableAlert postable alert",
"properties": {
@ -3685,7 +3704,6 @@
"type": "object"
},
"receiver": {
"description": "Receiver receiver",
"properties": {
"active": {
"description": "active",
@ -4066,9 +4084,9 @@
],
"responses": {
"201": {
"description": "gettableSilence",
"description": "postSilencesOKBody",
"schema": {
"$ref": "#/definitions/gettableSilence"
"$ref": "#/definitions/postSilencesOKBody"
}
},
"400": {
@ -4602,9 +4620,9 @@
],
"responses": {
"201": {
"description": "gettableSilence",
"description": "postSilencesOKBody",
"schema": {
"$ref": "#/definitions/gettableSilence"
"$ref": "#/definitions/postSilencesOKBody"
}
},
"400": {

View File

@ -281,9 +281,9 @@
],
"responses": {
"201": {
"description": "gettableSilence",
"description": "postSilencesOKBody",
"schema": {
"$ref": "#/definitions/gettableSilence"
"$ref": "#/definitions/postSilencesOKBody"
}
},
"400": {
@ -817,9 +817,9 @@
],
"responses": {
"201": {
"description": "gettableSilence",
"description": "postSilencesOKBody",
"schema": {
"$ref": "#/definitions/gettableSilence"
"$ref": "#/definitions/postSilencesOKBody"
}
},
"400": {
@ -2951,6 +2951,9 @@
"auth_password": {
"$ref": "#/definitions/Secret"
},
"auth_password_file": {
"type": "string"
},
"auth_secret": {
"$ref": "#/definitions/Secret"
},
@ -3700,6 +3703,9 @@
"smtp_auth_password": {
"$ref": "#/definitions/Secret"
},
"smtp_auth_password_file": {
"type": "string"
},
"smtp_auth_secret": {
"$ref": "#/definitions/Secret"
},
@ -5575,6 +5581,9 @@
"type": "string"
}
},
"location": {
"type": "string"
},
"months": {
"type": "array",
"items": {
@ -5617,9 +5626,8 @@
}
},
"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"
@ -5833,6 +5841,7 @@
"$ref": "#/definitions/alertGroup"
},
"alertGroups": {
"description": "AlertGroups alert groups",
"type": "array",
"items": {
"$ref": "#/definitions/alertGroup"
@ -5938,6 +5947,7 @@
}
},
"gettableAlert": {
"description": "GettableAlert gettable alert",
"type": "object",
"required": [
"labels",
@ -5994,7 +6004,6 @@
"$ref": "#/definitions/gettableAlert"
},
"gettableAlerts": {
"description": "GettableAlerts gettable alerts",
"type": "array",
"items": {
"$ref": "#/definitions/gettableAlert"
@ -6051,6 +6060,7 @@
"$ref": "#/definitions/gettableSilence"
},
"gettableSilences": {
"description": "GettableSilences gettable silences",
"type": "array",
"items": {
"$ref": "#/definitions/gettableSilence"
@ -6156,6 +6166,15 @@
}
}
},
"postSilencesOKBody": {
"type": "object",
"properties": {
"silenceID": {
"description": "silence ID",
"type": "string"
}
}
},
"postableAlert": {
"description": "PostableAlert postable alert",
"type": "object",
@ -6232,7 +6251,6 @@
"$ref": "#/definitions/postableSilence"
},
"receiver": {
"description": "Receiver receiver",
"type": "object",
"required": [
"active",

View File

@ -239,13 +239,11 @@ func TestAMConfigAccess(t *testing.T) {
desc: "editor request should succeed",
url: "http://editor:editor@%s/api/alertmanager/grafana/api/v2/silences",
expStatus: http.StatusAccepted,
expBody: `{"id":"0","message":"silence created"}`,
},
{
desc: "admin request should succeed",
url: "http://admin:admin@%s/api/alertmanager/grafana/api/v2/silences",
expStatus: http.StatusAccepted,
expBody: `{"id":"0","message":"silence created"}`,
},
}
@ -263,8 +261,10 @@ func TestAMConfigAccess(t *testing.T) {
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
if tc.expStatus == http.StatusAccepted {
re := regexp.MustCompile(`"id":"([\w|-]+)"`)
b = re.ReplaceAll(b, []byte(`"id":"0"`))
response := apimodels.PostSilencesOKBody{}
require.NoError(t, json.Unmarshal(b, &response))
require.NotEmpty(t, response.SilenceID)
return
}
require.Contains(t, string(b), tc.expBody)
})