From 263ca5c9438e3b2e2af95a2f1e4f53d32ab18e5c Mon Sep 17 00:00:00 2001 From: Emmanuel Lodovice Date: Fri, 5 May 2023 06:06:26 -0700 Subject: [PATCH] api/v2: Delete silence respond with 404 when silence is not found (#3352) Signed-off-by: Emmanuel Lodovice --- api/v2/api.go | 3 + api/v2/api_test.go | 2 +- .../silence/delete_silence_responses.go | 57 +++++++++++++++++++ api/v2/openapi.yaml | 2 + api/v2/restapi/embedded_spec.go | 6 ++ .../silence/delete_silence_responses.go | 25 ++++++++ 6 files changed, 94 insertions(+), 1 deletion(-) diff --git a/api/v2/api.go b/api/v2/api.go index a808976c74..1ddb2bcbae 100644 --- a/api/v2/api.go +++ b/api/v2/api.go @@ -634,6 +634,9 @@ func (api *API) deleteSilenceHandler(params silence_ops.DeleteSilenceParams) mid sid := params.SilenceID.String() if err := api.silences.Expire(sid); err != nil { level.Error(logger).Log("msg", "Failed to expire silence", "err", err) + if err == silence.ErrNotFound { + return silence_ops.NewDeleteSilenceNotFound() + } return silence_ops.NewDeleteSilenceInternalServerError().WithPayload(err.Error()) } return silence_ops.NewDeleteSilenceOK() diff --git a/api/v2/api_test.go b/api/v2/api_test.go index 1bc4dd807f..d520dbde13 100644 --- a/api/v2/api_test.go +++ b/api/v2/api_test.go @@ -178,7 +178,7 @@ func TestDeleteSilenceHandler(t *testing.T) { }{ { "unknownSid", - 500, + 404, }, { unexpiredSid, diff --git a/api/v2/client/silence/delete_silence_responses.go b/api/v2/client/silence/delete_silence_responses.go index 40a61a7c30..c8b2bba1c7 100644 --- a/api/v2/client/silence/delete_silence_responses.go +++ b/api/v2/client/silence/delete_silence_responses.go @@ -41,6 +41,12 @@ func (o *DeleteSilenceReader) ReadResponse(response runtime.ClientResponse, cons return nil, err } return result, nil + case 404: + result := NewDeleteSilenceNotFound() + if err := result.readResponse(response, consumer, o.formats); err != nil { + return nil, err + } + return nil, result case 500: result := NewDeleteSilenceInternalServerError() if err := result.readResponse(response, consumer, o.formats); err != nil { @@ -103,6 +109,57 @@ func (o *DeleteSilenceOK) readResponse(response runtime.ClientResponse, consumer return nil } +// NewDeleteSilenceNotFound creates a DeleteSilenceNotFound with default headers values +func NewDeleteSilenceNotFound() *DeleteSilenceNotFound { + return &DeleteSilenceNotFound{} +} + +/* +DeleteSilenceNotFound describes a response with status code 404, with default header values. + +A silence with the specified ID was not found +*/ +type DeleteSilenceNotFound struct { +} + +// IsSuccess returns true when this delete silence not found response has a 2xx status code +func (o *DeleteSilenceNotFound) IsSuccess() bool { + return false +} + +// IsRedirect returns true when this delete silence not found response has a 3xx status code +func (o *DeleteSilenceNotFound) IsRedirect() bool { + return false +} + +// IsClientError returns true when this delete silence not found response has a 4xx status code +func (o *DeleteSilenceNotFound) IsClientError() bool { + return true +} + +// IsServerError returns true when this delete silence not found response has a 5xx status code +func (o *DeleteSilenceNotFound) IsServerError() bool { + return false +} + +// IsCode returns true when this delete silence not found response a status code equal to that given +func (o *DeleteSilenceNotFound) IsCode(code int) bool { + return code == 404 +} + +func (o *DeleteSilenceNotFound) Error() string { + return fmt.Sprintf("[DELETE /silence/{silenceID}][%d] deleteSilenceNotFound ", 404) +} + +func (o *DeleteSilenceNotFound) String() string { + return fmt.Sprintf("[DELETE /silence/{silenceID}][%d] deleteSilenceNotFound ", 404) +} + +func (o *DeleteSilenceNotFound) readResponse(response runtime.ClientResponse, consumer runtime.Consumer, formats strfmt.Registry) error { + + return nil +} + // NewDeleteSilenceInternalServerError creates a DeleteSilenceInternalServerError with default headers values func NewDeleteSilenceInternalServerError() *DeleteSilenceInternalServerError { return &DeleteSilenceInternalServerError{} diff --git a/api/v2/openapi.yaml b/api/v2/openapi.yaml index 7f5392fc96..801edf00bb 100644 --- a/api/v2/openapi.yaml +++ b/api/v2/openapi.yaml @@ -127,6 +127,8 @@ paths: responses: '200': description: Delete silence response + '404': + description: A silence with the specified ID was not found '500': $ref: '#/responses/InternalServerError' /alerts: diff --git a/api/v2/restapi/embedded_spec.go b/api/v2/restapi/embedded_spec.go index 4a697a211f..caf371d884 100644 --- a/api/v2/restapi/embedded_spec.go +++ b/api/v2/restapi/embedded_spec.go @@ -272,6 +272,9 @@ func init() { "200": { "description": "Delete silence response" }, + "404": { + "description": "A silence with the specified ID was not found" + }, "500": { "$ref": "#/responses/InternalServerError" } @@ -1077,6 +1080,9 @@ func init() { "200": { "description": "Delete silence response" }, + "404": { + "description": "A silence with the specified ID was not found" + }, "500": { "description": "Internal server error", "schema": { diff --git a/api/v2/restapi/operations/silence/delete_silence_responses.go b/api/v2/restapi/operations/silence/delete_silence_responses.go index 0878e35dd3..cb88bfeb7f 100644 --- a/api/v2/restapi/operations/silence/delete_silence_responses.go +++ b/api/v2/restapi/operations/silence/delete_silence_responses.go @@ -50,6 +50,31 @@ func (o *DeleteSilenceOK) WriteResponse(rw http.ResponseWriter, producer runtime rw.WriteHeader(200) } +// DeleteSilenceNotFoundCode is the HTTP code returned for type DeleteSilenceNotFound +const DeleteSilenceNotFoundCode int = 404 + +/* +DeleteSilenceNotFound A silence with the specified ID was not found + +swagger:response deleteSilenceNotFound +*/ +type DeleteSilenceNotFound struct { +} + +// NewDeleteSilenceNotFound creates DeleteSilenceNotFound with default headers values +func NewDeleteSilenceNotFound() *DeleteSilenceNotFound { + + return &DeleteSilenceNotFound{} +} + +// WriteResponse to the client +func (o *DeleteSilenceNotFound) WriteResponse(rw http.ResponseWriter, producer runtime.Producer) { + + rw.Header().Del(runtime.HeaderContentType) //Remove Content-Type on empty responses + + rw.WriteHeader(404) +} + // DeleteSilenceInternalServerErrorCode is the HTTP code returned for type DeleteSilenceInternalServerError const DeleteSilenceInternalServerErrorCode int = 500