From 6c98271e436d1e7505cd46aa96b26f991169bea8 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Fri, 17 May 2024 16:45:12 -0300 Subject: [PATCH] fix(endpoints): remove all the endpoints in the same transaction EE-7095 (#11840) --- api/http/handler/endpoints/endpoint_delete.go | 41 +++++++++++-------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/api/http/handler/endpoints/endpoint_delete.go b/api/http/handler/endpoints/endpoint_delete.go index 1755c29bc..1dbe8477b 100644 --- a/api/http/handler/endpoints/endpoint_delete.go +++ b/api/http/handler/endpoints/endpoint_delete.go @@ -30,6 +30,7 @@ func (payload *DeleteMultiplePayload) Validate(r *http.Request) error { if payload == nil || len(payload.Endpoints) == 0 { return fmt.Errorf("invalid request payload; you must provide a list of nodes to delete") } + return nil } @@ -53,6 +54,7 @@ type DeleteMultipleResp struct { // @failure 500 "Server error" // @router /endpoints/{id} [delete] // @deprecated +// Deprecated: use endpointDeleteMultiple instead. func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id") if err != nil { @@ -102,32 +104,39 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * // @router /endpoints/remove [post] func (handler *Handler) endpointDeleteMultiple(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { var p DeleteMultiplePayload - err := request.DecodeAndValidateJSONPayload(r, &p) - if err != nil { + if err := request.DecodeAndValidateJSONPayload(r, &p); err != nil { return httperror.BadRequest("Invalid request payload", err) } var resps []DeleteMultipleResp - for _, e := range p.Endpoints { - // Demo endpoints cannot be deleted. - if handler.demoService.IsDemoEnvironment(portainer.EndpointID(e.ID)) { - resps = append(resps, DeleteMultipleResp{ - Name: e.Name, - Err: httperrors.ErrNotAvailableInDemo, - }) - continue - } - // Attempt deletion. - err = handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { - return handler.deleteEndpoint( + err := handler.DataStore.UpdateTx(func(tx dataservices.DataStoreTx) error { + for _, e := range p.Endpoints { + // Demo endpoints cannot be deleted. + if handler.demoService.IsDemoEnvironment(portainer.EndpointID(e.ID)) { + resps = append(resps, DeleteMultipleResp{ + Name: e.Name, + Err: httperrors.ErrNotAvailableInDemo, + }) + continue + } + + // Attempt deletion. + err := handler.deleteEndpoint( tx, portainer.EndpointID(e.ID), e.DeleteCluster, ) - }) - resps = append(resps, DeleteMultipleResp{Name: e.Name, Err: err}) + + resps = append(resps, DeleteMultipleResp{Name: e.Name, Err: err}) + } + + return nil + }) + if err != nil { + return httperror.InternalServerError("Unable to delete environments", err) } + return response.JSON(w, resps) }