From 5ad83d0adb5c81321efba61eb6153946ba2ec0fa Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Wed, 19 Jul 2023 18:21:47 +0300 Subject: [PATCH] fix(endpoints): filter by all edge stack states (#9218) --- api/http/handler/endpoints/filter.go | 44 +++++++++++++++++++++------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/api/http/handler/endpoints/filter.go b/api/http/handler/endpoints/filter.go index 75b5360b7..8d008fcc7 100644 --- a/api/http/handler/endpoints/filter.go +++ b/api/http/handler/endpoints/filter.go @@ -87,17 +87,9 @@ func parseQuery(r *http.Request) (EnvironmentsQuery, error) { edgeStackId, _ := request.RetrieveNumericQueryParameter(r, "edgeStackId", true) - edgeStackStatusQuery, _ := request.RetrieveQueryParameter(r, "edgeStackStatus", true) - var edgeStackStatus *portainer.EdgeStackStatusType - if edgeStackStatusQuery != "" { - edgeStackStatusNumber, err := strconv.Atoi(edgeStackStatusQuery) - if err != nil || - edgeStackStatusNumber < 0 || - edgeStackStatusNumber > int(portainer.EdgeStackStatusRemoving) { - return EnvironmentsQuery{}, errors.New("invalid edgeStackStatus parameter") - } - - edgeStackStatus = ptr(portainer.EdgeStackStatusType(edgeStackStatusNumber)) + edgeStackStatus, err := getEdgeStackStatusParam(r) + if err != nil { + return EnvironmentsQuery{}, err } return EnvironmentsQuery{ @@ -544,3 +536,33 @@ func contains(strings []string, param string) bool { return false } + +func getEdgeStackStatusParam(r *http.Request) (*portainer.EdgeStackStatusType, error) { + edgeStackStatusQuery, _ := request.RetrieveQueryParameter(r, "edgeStackStatus", true) + if edgeStackStatusQuery == "" { + return nil, nil + } + + edgeStackStatusNumber, err := strconv.Atoi(edgeStackStatusQuery) + edgeStackStatus := portainer.EdgeStackStatusType(edgeStackStatusNumber) + if err != nil { + return nil, fmt.Errorf("failed parsing edgeStackStatus: %w", err) + } + + if !slices.Contains([]portainer.EdgeStackStatusType{ + portainer.EdgeStackStatusPending, + portainer.EdgeStackStatusDeploymentReceived, + portainer.EdgeStackStatusError, + portainer.EdgeStackStatusAcknowledged, + portainer.EdgeStackStatusRemoved, + portainer.EdgeStackStatusRemoteUpdateSuccess, + portainer.EdgeStackStatusImagesPulled, + portainer.EdgeStackStatusRunning, + portainer.EdgeStackStatusDeploying, + portainer.EdgeStackStatusRemoving, + }, edgeStackStatus) { + return nil, errors.New("invalid edgeStackStatus parameter") + } + + return &edgeStackStatus, nil +}