2022-12-11 06:58:22 +00:00
|
|
|
package system
|
2022-11-16 16:38:39 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2025-01-23 23:41:09 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2022-11-16 16:38:39 +00:00
|
|
|
statusutil "github.com/portainer/portainer/api/internal/nodes"
|
|
|
|
"github.com/portainer/portainer/api/internal/snapshot"
|
2023-09-01 22:27:02 +00:00
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
2022-11-16 16:38:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nodesCountResponse struct {
|
|
|
|
Nodes int `json:"nodes"`
|
|
|
|
}
|
|
|
|
|
2022-12-11 06:58:22 +00:00
|
|
|
// @id systemNodesCount
|
2022-11-16 16:38:39 +00:00
|
|
|
// @summary Retrieve the count of nodes
|
|
|
|
// @description **Access policy**: authenticated
|
|
|
|
// @security ApiKeyAuth
|
|
|
|
// @security jwt
|
2022-12-11 06:58:22 +00:00
|
|
|
// @tags system
|
2022-11-16 16:38:39 +00:00
|
|
|
// @produce json
|
|
|
|
// @success 200 {object} nodesCountResponse "Success"
|
|
|
|
// @failure 500 "Server error"
|
2022-12-11 06:58:22 +00:00
|
|
|
// @router /system/nodes [get]
|
|
|
|
func (handler *Handler) systemNodesCount(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2022-11-16 16:38:39 +00:00
|
|
|
endpoints, err := handler.dataStore.Endpoint().Endpoints()
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Failed to get environment list", err)
|
|
|
|
}
|
|
|
|
|
2025-01-23 23:41:09 +00:00
|
|
|
var nodes int
|
|
|
|
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if err := snapshot.FillSnapshotData(handler.dataStore, &endpoint); err != nil {
|
2022-11-16 16:38:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to add snapshot data", err)
|
|
|
|
}
|
|
|
|
|
2025-01-23 23:41:09 +00:00
|
|
|
nodes += statusutil.NodesCount([]portainer.Endpoint{endpoint})
|
|
|
|
}
|
2022-11-16 16:38:39 +00:00
|
|
|
|
|
|
|
return response.JSON(w, &nodesCountResponse{Nodes: nodes})
|
|
|
|
}
|