mirror of
https://github.com/datarhei/core.git
synced 2025-10-06 00:17:07 +08:00
Refactor cluster node code
This commit is contained in:
@@ -7,15 +7,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
clientapi "github.com/datarhei/core-client-go/v16/api"
|
||||
"github.com/datarhei/core/v16/cluster"
|
||||
"github.com/datarhei/core/v16/cluster/proxy"
|
||||
"github.com/datarhei/core/v16/cluster/node"
|
||||
"github.com/datarhei/core/v16/http/api"
|
||||
"github.com/datarhei/core/v16/http/handler/util"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// GetNodes returns the list of proxy nodes in the cluster
|
||||
// NodeList returns the list of proxy nodes in the cluster
|
||||
// @Summary List of proxy nodes in the cluster
|
||||
// @Description List of proxy nodes in the cluster
|
||||
// @Tags v16.?.?
|
||||
@@ -25,17 +24,17 @@ import (
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node [get]
|
||||
func (h *ClusterHandler) GetNodes(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeList(c echo.Context) error {
|
||||
about, _ := h.cluster.About()
|
||||
|
||||
nodes := h.cluster.ListNodes()
|
||||
nodes := h.cluster.Store().NodeList()
|
||||
|
||||
list := []api.ClusterNode{}
|
||||
|
||||
for _, node := range about.Nodes {
|
||||
if dbnode, hasNode := nodes[node.ID]; hasNode {
|
||||
if dbnode.State == "maintenance" {
|
||||
node.Status = dbnode.State
|
||||
node.State = dbnode.State
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +44,7 @@ func (h *ClusterHandler) GetNodes(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, list)
|
||||
}
|
||||
|
||||
// GetNode returns the proxy node with the given ID
|
||||
// NodeGet returns the proxy node with the given ID
|
||||
// @Summary List a proxy node by its ID
|
||||
// @Description List a proxy node by its ID
|
||||
// @Tags v16.?.?
|
||||
@@ -56,12 +55,12 @@ func (h *ClusterHandler) GetNodes(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node/{id} [get]
|
||||
func (h *ClusterHandler) GetNode(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeGet(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
|
||||
about, _ := h.cluster.About()
|
||||
|
||||
nodes := h.cluster.ListNodes()
|
||||
nodes := h.cluster.Store().NodeList()
|
||||
|
||||
for _, node := range about.Nodes {
|
||||
if node.ID != id {
|
||||
@@ -70,7 +69,7 @@ func (h *ClusterHandler) GetNode(c echo.Context) error {
|
||||
|
||||
if dbnode, hasNode := nodes[node.ID]; hasNode {
|
||||
if dbnode.State == "maintenance" {
|
||||
node.Status = dbnode.State
|
||||
node.State = dbnode.State
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +79,7 @@ func (h *ClusterHandler) GetNode(c echo.Context) error {
|
||||
return api.Err(http.StatusNotFound, "", "node not found")
|
||||
}
|
||||
|
||||
// GetNodeVersion returns the proxy node version with the given ID
|
||||
// NodeGetVersion returns the proxy node version with the given ID
|
||||
// @Summary List a proxy node by its ID
|
||||
// @Description List a proxy node by its ID
|
||||
// @Tags v16.?.?
|
||||
@@ -91,29 +90,29 @@ func (h *ClusterHandler) GetNode(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node/{id}/version [get]
|
||||
func (h *ClusterHandler) GetNodeVersion(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeGetVersion(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
|
||||
v := peer.Version()
|
||||
v := peer.CoreAbout()
|
||||
|
||||
version := api.Version{
|
||||
Number: v.Number,
|
||||
Commit: v.Commit,
|
||||
Branch: v.Branch,
|
||||
Build: v.Build.Format(time.RFC3339),
|
||||
Arch: v.Arch,
|
||||
Compiler: v.Compiler,
|
||||
Number: v.Version.Number,
|
||||
Commit: v.Version.Commit,
|
||||
Branch: v.Version.Branch,
|
||||
Build: v.Version.Build.Format(time.RFC3339),
|
||||
Arch: v.Version.Arch,
|
||||
Compiler: v.Version.Compiler,
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, version)
|
||||
}
|
||||
|
||||
// GetNodeResources returns the resources from the proxy node with the given ID
|
||||
// NodeGetMedia returns the resources from the proxy node with the given ID
|
||||
// @Summary List the resources of a proxy node by its ID
|
||||
// @Description List the resources of a proxy node by its ID
|
||||
// @Tags v16.?.?
|
||||
@@ -124,10 +123,10 @@ func (h *ClusterHandler) GetNodeVersion(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node/{id}/files [get]
|
||||
func (h *ClusterHandler) GetNodeResources(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeGetMedia(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
@@ -136,7 +135,7 @@ func (h *ClusterHandler) GetNodeResources(c echo.Context) error {
|
||||
Files: make(map[string][]string),
|
||||
}
|
||||
|
||||
peerFiles := peer.ListResources()
|
||||
peerFiles := peer.Core().MediaList()
|
||||
|
||||
files.LastUpdate = peerFiles.LastUpdate.Unix()
|
||||
|
||||
@@ -176,12 +175,12 @@ func (h *ClusterHandler) NodeFSListFiles(c echo.Context) error {
|
||||
sortby := util.DefaultQuery(c, "sort", "none")
|
||||
order := util.DefaultQuery(c, "order", "asc")
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
|
||||
files, err := peer.ListFiles(name, pattern)
|
||||
files, err := peer.Core().FilesystemList(name, pattern)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusInternalServerError, "", "retrieving file list: %s", err.Error())
|
||||
}
|
||||
@@ -234,12 +233,12 @@ func (h *ClusterHandler) NodeFSGetFile(c echo.Context) error {
|
||||
storage := util.PathParam(c, "storage")
|
||||
path := util.PathWildcardParam(c)
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
|
||||
file, err := peer.GetFile(storage, path, 0)
|
||||
file, err := peer.Core().FilesystemGetFile(storage, path, 0)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "%s", err.Error())
|
||||
}
|
||||
@@ -270,14 +269,14 @@ func (h *ClusterHandler) NodeFSPutFile(c echo.Context) error {
|
||||
storage := util.PathParam(c, "storage")
|
||||
path := util.PathWildcardParam(c)
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
|
||||
req := c.Request()
|
||||
|
||||
err = peer.PutFile(storage, path, req.Body)
|
||||
err = peer.Core().FilesystemPutFile(storage, path, req.Body)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusBadRequest, "", "%s", err.Error())
|
||||
}
|
||||
@@ -303,12 +302,12 @@ func (h *ClusterHandler) NodeFSDeleteFile(c echo.Context) error {
|
||||
storage := util.PathParam(c, "storage")
|
||||
path := util.PathWildcardParam(c)
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
|
||||
err = peer.DeleteFile(storage, path)
|
||||
err = peer.Core().FilesystemDeleteFile(storage, path)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "%s", err.Error())
|
||||
}
|
||||
@@ -316,7 +315,7 @@ func (h *ClusterHandler) NodeFSDeleteFile(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, nil)
|
||||
}
|
||||
|
||||
// ListNodeProcesses returns the list of processes running on a node of the cluster
|
||||
// NodeListProcesses returns the list of processes running on a node of the cluster
|
||||
// @Summary List of processes in the cluster on a node
|
||||
// @Description List of processes in the cluster on a node
|
||||
// @Tags v16.?.?
|
||||
@@ -336,7 +335,7 @@ func (h *ClusterHandler) NodeFSDeleteFile(c echo.Context) error {
|
||||
// @Failure 500 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node/{id}/process [get]
|
||||
func (h *ClusterHandler) ListNodeProcesses(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeListProcesses(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
ctxuser := util.DefaultContext(c, "user", "")
|
||||
filter := strings.FieldsFunc(util.DefaultQuery(c, "filter", ""), func(r rune) bool {
|
||||
@@ -352,12 +351,12 @@ func (h *ClusterHandler) ListNodeProcesses(c echo.Context) error {
|
||||
ownerpattern := util.DefaultQuery(c, "ownerpattern", "")
|
||||
domainpattern := util.DefaultQuery(c, "domainpattern", "")
|
||||
|
||||
peer, err := h.proxy.GetNodeReader(id)
|
||||
peer, err := h.proxy.NodeGet(id)
|
||||
if err != nil {
|
||||
return api.Err(http.StatusNotFound, "", "node not found: %s", err.Error())
|
||||
}
|
||||
|
||||
procs, err := peer.ProcessList(proxy.ProcessListOptions{
|
||||
procs, err := peer.Core().ProcessList(node.ProcessListOptions{
|
||||
ID: wantids,
|
||||
Filter: filter,
|
||||
Domain: domain,
|
||||
@@ -371,7 +370,7 @@ func (h *ClusterHandler) ListNodeProcesses(c echo.Context) error {
|
||||
return api.Err(http.StatusInternalServerError, "", "node not available: %s", err.Error())
|
||||
}
|
||||
|
||||
processes := []clientapi.Process{}
|
||||
processes := []api.Process{}
|
||||
|
||||
for _, p := range procs {
|
||||
if !h.iam.Enforce(ctxuser, domain, "process", p.Config.ID, "read") {
|
||||
@@ -384,7 +383,7 @@ func (h *ClusterHandler) ListNodeProcesses(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, processes)
|
||||
}
|
||||
|
||||
// GetNodeState returns the state of a node with the given ID
|
||||
// NodeGetState returns the state of a node with the given ID
|
||||
// @Summary Get the state of a node with the given ID
|
||||
// @Description Get the state of a node with the given ID
|
||||
// @Tags v16.?.?
|
||||
@@ -395,7 +394,7 @@ func (h *ClusterHandler) ListNodeProcesses(c echo.Context) error {
|
||||
// @Failure 404 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node/{id}/state [get]
|
||||
func (h *ClusterHandler) GetNodeState(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeGetState(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
|
||||
about, _ := h.cluster.About()
|
||||
@@ -406,7 +405,7 @@ func (h *ClusterHandler) GetNodeState(c echo.Context) error {
|
||||
continue
|
||||
}
|
||||
|
||||
state = node.Status
|
||||
state = node.State
|
||||
break
|
||||
}
|
||||
|
||||
@@ -414,7 +413,7 @@ func (h *ClusterHandler) GetNodeState(c echo.Context) error {
|
||||
return api.Err(http.StatusNotFound, "", "node not found")
|
||||
}
|
||||
|
||||
nodes := h.cluster.ListNodes()
|
||||
nodes := h.cluster.Store().NodeList()
|
||||
if node, hasNode := nodes[id]; hasNode {
|
||||
if node.State == "maintenance" {
|
||||
state = node.State
|
||||
@@ -426,7 +425,7 @@ func (h *ClusterHandler) GetNodeState(c echo.Context) error {
|
||||
})
|
||||
}
|
||||
|
||||
// SetNodeState sets the state of a node with the given ID
|
||||
// NodeSetState sets the state of a node with the given ID
|
||||
// @Summary Set the state of a node with the given ID
|
||||
// @Description Set the state of a node with the given ID
|
||||
// @Tags v16.?.?
|
||||
@@ -440,7 +439,7 @@ func (h *ClusterHandler) GetNodeState(c echo.Context) error {
|
||||
// @Failure 500 {object} api.Error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /api/v3/cluster/node/{id}/state [put]
|
||||
func (h *ClusterHandler) SetNodeState(c echo.Context) error {
|
||||
func (h *ClusterHandler) NodeSetState(c echo.Context) error {
|
||||
id := util.PathParam(c, "id")
|
||||
|
||||
about, _ := h.cluster.About()
|
||||
@@ -478,7 +477,7 @@ func (h *ClusterHandler) SetNodeState(c echo.Context) error {
|
||||
return c.JSON(http.StatusOK, "OK")
|
||||
}
|
||||
|
||||
err := h.cluster.SetNodeState("", id, state.State)
|
||||
err := h.cluster.NodeSetState("", id, state.State)
|
||||
if err != nil {
|
||||
if errors.Is(err, cluster.ErrUnsupportedNodeState) {
|
||||
return api.Err(http.StatusBadRequest, "", "%s", err.Error())
|
||||
|
Reference in New Issue
Block a user