feat: Implement node management features including creation, updating, and deletion

- Added CreateNode, UpdateNode, and DeleteNode functions in the controller for managing nodes.
- Introduced NodeInput struct for input validation during node creation and updates.
- Enhanced the Node model to include DiscoveryToken and Pending status.
- Updated the AgentRegister and AgentHeartbeat functions to utilize the new node management logic.
- Refactored the API router to include new routes for node management.
- Improved the frontend Node component to support node creation, editing, and deletion with appropriate UI feedback.
- Added tests to ensure the new functionality works as expected.
This commit is contained in:
ryan
2026-03-10 15:33:06 +08:00
parent 861d759f97
commit 05e75549d2
16 changed files with 916 additions and 296 deletions
+24 -3
View File
@@ -2,6 +2,7 @@ package controller
import (
"encoding/json"
"gin-template/model"
"gin-template/service"
"github.com/gin-gonic/gin"
"net/http"
@@ -16,7 +17,15 @@ func AgentRegister(c *gin.Context) {
})
return
}
node, err := service.RegisterNode(payload)
discoveryNode, ok := c.Get("discovery_node")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,Discovery Token 无效",
})
return
}
result, err := service.RegisterNode(discoveryNode.(*model.Node), payload)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
@@ -27,7 +36,7 @@ func AgentRegister(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": node,
"data": result,
})
}
@@ -40,7 +49,15 @@ func AgentHeartbeat(c *gin.Context) {
})
return
}
node, err := service.HeartbeatNode(payload)
authNode, ok := c.Get("agent_node")
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"message": "无权进行此操作,Agent Token 无效",
})
return
}
node, err := service.HeartbeatNode(authNode.(*model.Node), payload)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
@@ -80,6 +97,10 @@ func AgentReportApplyLog(c *gin.Context) {
})
return
}
authNode, ok := c.Get("agent_node")
if ok {
payload.NodeID = authNode.(*model.Node).NodeID
}
log, err := service.ReportApplyLog(payload)
if err != nil {
c.JSON(http.StatusOK, gin.H{
+87
View File
@@ -0,0 +1,87 @@
package controller
import (
"encoding/json"
"gin-template/service"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
func CreateNode(c *gin.Context) {
var input service.NodeInput
if err := json.NewDecoder(c.Request.Body).Decode(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的参数",
})
return
}
node, err := service.CreateNode(input)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": node,
})
}
func UpdateNode(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的参数",
})
return
}
var input service.NodeInput
if err = json.NewDecoder(c.Request.Body).Decode(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的参数",
})
return
}
node, err := service.UpdateNode(uint(id), input)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
"data": node,
})
}
func DeleteNode(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "无效的参数",
})
return
}
if err = service.DeleteNode(uint(id)); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": "",
})
}