2021-03-24 18:27:32 +00:00
|
|
|
package endpointutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
)
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// IsLocalEndpoint returns true if this is a local environment(endpoint)
|
2021-03-24 18:27:32 +00:00
|
|
|
func IsLocalEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return strings.HasPrefix(endpoint.URL, "unix://") || strings.HasPrefix(endpoint.URL, "npipe://") || endpoint.Type == 5
|
|
|
|
}
|
2021-06-16 05:28:44 +00:00
|
|
|
|
2021-11-22 18:05:09 +00:00
|
|
|
// IsKubernetesEndpoint returns true if this is a kubernetes environment(endpoint)
|
2021-06-16 05:28:44 +00:00
|
|
|
func IsKubernetesEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.KubernetesLocalEnvironment ||
|
|
|
|
endpoint.Type == portainer.AgentOnKubernetesEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
|
|
|
}
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// IsDockerEndpoint returns true if this is a docker environment(endpoint)
|
2021-06-16 05:28:44 +00:00
|
|
|
func IsDockerEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.DockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.AgentOnDockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnDockerEnvironment
|
|
|
|
}
|
2021-09-24 04:56:22 +00:00
|
|
|
|
2021-09-29 23:58:10 +00:00
|
|
|
// IsEdgeEndpoint returns true if this is an Edge endpoint
|
2021-09-24 04:56:22 +00:00
|
|
|
func IsEdgeEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.EdgeAgentOnDockerEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
|
|
|
}
|
2021-11-22 18:05:09 +00:00
|
|
|
|
2022-01-23 19:48:04 +00:00
|
|
|
// IsAgentEndpoint returns true if this is an Agent endpoint
|
|
|
|
func IsAgentEndpoint(endpoint *portainer.Endpoint) bool {
|
|
|
|
return endpoint.Type == portainer.AgentOnDockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnDockerEnvironment ||
|
|
|
|
endpoint.Type == portainer.AgentOnKubernetesEnvironment ||
|
|
|
|
endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment
|
|
|
|
}
|
|
|
|
|
2021-11-22 18:05:09 +00:00
|
|
|
// FilterByExcludeIDs receives an environment(endpoint) array and returns a filtered array using an excludeIds param
|
|
|
|
func FilterByExcludeIDs(endpoints []portainer.Endpoint, excludeIds []portainer.EndpointID) []portainer.Endpoint {
|
|
|
|
if len(excludeIds) == 0 {
|
|
|
|
return endpoints
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredEndpoints := make([]portainer.Endpoint, 0)
|
|
|
|
|
|
|
|
idsSet := make(map[portainer.EndpointID]bool)
|
|
|
|
for _, id := range excludeIds {
|
|
|
|
idsSet[id] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if !idsSet[endpoint.ID] {
|
|
|
|
filteredEndpoints = append(filteredEndpoints, endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredEndpoints
|
|
|
|
}
|
2022-04-08 14:27:38 +00:00
|
|
|
|
|
|
|
// EndpointSet receives an environment(endpoint) array and returns a set
|
|
|
|
func EndpointSet(endpointIDs []portainer.EndpointID) map[portainer.EndpointID]bool {
|
|
|
|
set := map[portainer.EndpointID]bool{}
|
|
|
|
|
|
|
|
for _, endpointID := range endpointIDs {
|
|
|
|
set[endpointID] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return set
|
|
|
|
}
|