feat: K8s集群拓扑图接入真实后端数据 — 分层可视化Cluster→Ingress→Service→Deployment→Node + http.ts类型修复
This commit is contained in:
1 parent
4b094f4645
commit
9f65faff6f
17 files changed
+3021
-1205
No files matched your search
@@ -0,0 +1,134 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"ezops/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ResourceModelHandler 资源模型处理器
|
||||
type ResourceModelHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewResourceModelHandler(db *gorm.DB) *ResourceModelHandler {
|
||||
return &ResourceModelHandler{db: db}
|
||||
}
|
||||
|
||||
// resourceModelRequest 资源模型创建/更新请求
|
||||
type resourceModelRequest struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
Type string `json:"type"`
|
||||
Icon string `json:"icon"`
|
||||
Attributes []map[string]interface{} `json:"attributes"`
|
||||
}
|
||||
|
||||
// ListResourceModels 获取资源模型列表
|
||||
func (h *ResourceModelHandler) ListResourceModels(c *gin.Context) {
|
||||
var models_ []models.ResourceModel
|
||||
h.db.Order("id ASC").Find(&models_)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: models.ListResponse{
|
||||
List: models_,
|
||||
Total: int64(len(models_)),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// CreateResourceModel 创建资源模型
|
||||
func (h *ResourceModelHandler) CreateResourceModel(c *gin.Context) {
|
||||
var req resourceModelRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(req.Name) == "" || strings.TrimSpace(req.Code) == "" {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "模型名称和编码不能为空"})
|
||||
return
|
||||
}
|
||||
|
||||
modelType := strings.TrimSpace(req.Type)
|
||||
if modelType == "" {
|
||||
modelType = "normal"
|
||||
}
|
||||
|
||||
var attrsJSON string
|
||||
if len(req.Attributes) > 0 {
|
||||
b, _ := json.Marshal(req.Attributes)
|
||||
attrsJSON = string(b)
|
||||
}
|
||||
|
||||
rm := models.ResourceModel{
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Code: strings.TrimSpace(req.Code),
|
||||
Description: req.Description,
|
||||
Type: modelType,
|
||||
Attributes: attrsJSON,
|
||||
Icon: req.Icon,
|
||||
}
|
||||
if err := h.db.Create(&rm).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "创建失败,编码可能已存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "创建成功", Data: rm})
|
||||
}
|
||||
|
||||
// UpdateResourceModel 更新资源模型
|
||||
func (h *ResourceModelHandler) UpdateResourceModel(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
var req resourceModelRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var rm models.ResourceModel
|
||||
if err := h.db.First(&rm, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "资源模型不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.Name) != "" {
|
||||
rm.Name = strings.TrimSpace(req.Name)
|
||||
}
|
||||
if strings.TrimSpace(req.Code) != "" {
|
||||
rm.Code = strings.TrimSpace(req.Code)
|
||||
}
|
||||
if strings.TrimSpace(req.Type) != "" {
|
||||
rm.Type = strings.TrimSpace(req.Type)
|
||||
}
|
||||
rm.Description = req.Description
|
||||
rm.Icon = req.Icon
|
||||
|
||||
if len(req.Attributes) > 0 {
|
||||
b, _ := json.Marshal(req.Attributes)
|
||||
rm.Attributes = string(b)
|
||||
}
|
||||
|
||||
if err := h.db.Save(&rm).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "更新失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "更新成功", Data: rm})
|
||||
}
|
||||
|
||||
// DeleteResourceModel 删除资源模型
|
||||
func (h *ResourceModelHandler) DeleteResourceModel(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
if err := h.db.Delete(&models.ResourceModel{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "删除成功"})
|
||||
}
|
||||
Reference in new issue
Block a user