From 7b2269fbba377dea3317536991ec387429e324c2 Mon Sep 17 00:00:00 2001 From: Chaim Lev-Ari Date: Wed, 21 Jul 2021 10:16:22 +0300 Subject: [PATCH] feat(endpoints): filter endpoints by a list of types (#5308) * feat(endpoints): filter endpoints by a list of types * docs(endpoints): update api docs for endpoint list --- api/http/handler/endpoints/endpoint_list.go | 19 +++++++++++++------ .../group-form/groupFormController.js | 2 +- .../editEdgeStackViewController.js | 2 +- .../associatedEndpointsSelectorController.js | 4 ++-- app/portainer/services/api/endpointService.js | 5 +++-- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/api/http/handler/endpoints/endpoint_list.go b/api/http/handler/endpoints/endpoint_list.go index c1dd76209..92fca9f63 100644 --- a/api/http/handler/endpoints/endpoint_list.go +++ b/api/http/handler/endpoints/endpoint_list.go @@ -26,7 +26,7 @@ import ( // @param search query string false "Search query" // @param groupId query int false "List endpoints of this group" // @param limit query int false "Limit results to this value" -// @param type query int false "List endpoints of this type" +// @param types query []int false "List endpoints of this type" // @param tagIds query []int false "search endpoints with these tags (depends on tagsPartialMatch)" // @param tagsPartialMatch query bool false "If true, will return endpoint which has one of tagIds, if false (or missing) will return only endpoints that has all the tags" // @param endpointIds query []int false "will return only these endpoints" @@ -46,7 +46,9 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht groupID, _ := request.RetrieveNumericQueryParameter(r, "groupId", true) limit, _ := request.RetrieveNumericQueryParameter(r, "limit", true) - endpointType, _ := request.RetrieveNumericQueryParameter(r, "type", true) + + var endpointTypes []int + request.RetrieveJSONQueryParameter(r, "types", &endpointTypes, true) var tagIDs []portainer.TagID request.RetrieveJSONQueryParameter(r, "tagIds", &tagIDs, true) @@ -98,8 +100,8 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht filteredEndpoints = filterEndpointsBySearchCriteria(filteredEndpoints, endpointGroups, tagsMap, search) } - if endpointType != 0 { - filteredEndpoints = filterEndpointsByType(filteredEndpoints, portainer.EndpointType(endpointType)) + if endpointTypes != nil { + filteredEndpoints = filterEndpointsByTypes(filteredEndpoints, endpointTypes) } if tagIDs != nil { @@ -212,11 +214,16 @@ func endpointGroupMatchSearchCriteria(endpoint *portainer.Endpoint, endpointGrou return false } -func filterEndpointsByType(endpoints []portainer.Endpoint, endpointType portainer.EndpointType) []portainer.Endpoint { +func filterEndpointsByTypes(endpoints []portainer.Endpoint, endpointTypes []int) []portainer.Endpoint { filteredEndpoints := make([]portainer.Endpoint, 0) + typeSet := map[portainer.EndpointType]bool{} + for _, endpointType := range endpointTypes { + typeSet[portainer.EndpointType(endpointType)] = true + } + for _, endpoint := range endpoints { - if endpoint.Type == endpointType { + if typeSet[endpoint.Type] { filteredEndpoints = append(filteredEndpoints, endpoint) } } diff --git a/app/edge/components/group-form/groupFormController.js b/app/edge/components/group-form/groupFormController.js index 7b2283647..80abbb8f8 100644 --- a/app/edge/components/group-form/groupFormController.js +++ b/app/edge/components/group-form/groupFormController.js @@ -49,7 +49,7 @@ export class EdgeGroupFormController { async getDynamicEndpointsAsync() { const { pageNumber, limit, search } = this.endpoints.state; const start = (pageNumber - 1) * limit + 1; - const query = { search, type: 4, tagIds: this.model.TagIds, tagsPartialMatch: this.model.PartialMatch }; + const query = { search, types: [4], tagIds: this.model.TagIds, tagsPartialMatch: this.model.PartialMatch }; const response = await this.EndpointService.endpoints(start, limit, query); diff --git a/app/edge/views/edge-stacks/editEdgeStackView/editEdgeStackViewController.js b/app/edge/views/edge-stacks/editEdgeStackView/editEdgeStackViewController.js index f3b5eab75..02d6e3f66 100644 --- a/app/edge/views/edge-stacks/editEdgeStackView/editEdgeStackViewController.js +++ b/app/edge/views/edge-stacks/editEdgeStackView/editEdgeStackViewController.js @@ -95,7 +95,7 @@ export class EditEdgeStackViewController { async getPaginatedEndpointsAsync(lastId, limit, search) { try { - const query = { search, type: 4, endpointIds: this.stackEndpointIds }; + const query = { search, types: [4], endpointIds: this.stackEndpointIds }; const { value, totalCount } = await this.EndpointService.endpoints(lastId, limit, query); const endpoints = _.map(value, (endpoint) => { const status = this.stack.Status[endpoint.Id]; diff --git a/app/portainer/components/associated-endpoints-selector/associatedEndpointsSelectorController.js b/app/portainer/components/associated-endpoints-selector/associatedEndpointsSelectorController.js index e129552d5..7d23a6d5e 100644 --- a/app/portainer/components/associated-endpoints-selector/associatedEndpointsSelectorController.js +++ b/app/portainer/components/associated-endpoints-selector/associatedEndpointsSelectorController.js @@ -56,7 +56,7 @@ class AssoicatedEndpointsSelectorController { async getEndpointsAsync() { const { start, search, limit } = this.getPaginationData('available'); - const query = { search, type: 4 }; + const query = { search, types: [4] }; const response = await this.EndpointService.endpoints(start, limit, query); @@ -73,7 +73,7 @@ class AssoicatedEndpointsSelectorController { let response = { value: [], totalCount: 0 }; if (this.endpointIds.length > 0) { const { start, search, limit } = this.getPaginationData('associated'); - const query = { search, type: 4, endpointIds: this.endpointIds }; + const query = { search, types: [4], endpointIds: this.endpointIds }; response = await this.EndpointService.endpoints(start, limit, query); } diff --git a/app/portainer/services/api/endpointService.js b/app/portainer/services/api/endpointService.js index ef7291343..e272b8a08 100644 --- a/app/portainer/services/api/endpointService.js +++ b/app/portainer/services/api/endpointService.js @@ -17,11 +17,12 @@ angular.module('portainer.app').factory('EndpointService', [ return Endpoints.get({ id: endpointID }).$promise; }; - service.endpoints = function (start, limit, { search, type, tagIds, endpointIds, tagsPartialMatch } = {}) { + service.endpoints = function (start, limit, { search, types, tagIds, endpointIds, tagsPartialMatch } = {}) { if (tagIds && !tagIds.length) { return Promise.resolve({ value: [], totalCount: 0 }); } - return Endpoints.query({ start, limit, search, type, tagIds: JSON.stringify(tagIds), endpointIds: JSON.stringify(endpointIds), tagsPartialMatch }).$promise; + return Endpoints.query({ start, limit, search, types: JSON.stringify(types), tagIds: JSON.stringify(tagIds), endpointIds: JSON.stringify(endpointIds), tagsPartialMatch }) + .$promise; }; service.snapshotEndpoints = function () {