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
@@ -775,3 +775,237 @@ func (h *DomainHandler) UpdateDomain(c *gin.Context) {
|
||||
Data: domain,
|
||||
})
|
||||
}
|
||||
|
||||
// ==================== 云平台(Platform / Cloud)====================
|
||||
|
||||
// PlatformHandler 云平台处理器
|
||||
type PlatformHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPlatformHandler(db *gorm.DB) *PlatformHandler {
|
||||
return &PlatformHandler{db: db}
|
||||
}
|
||||
|
||||
// platformRequest 云平台创建/更新请求
|
||||
type platformRequest struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Website string `json:"website"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
// ListPlatforms 获取云平台列表
|
||||
func (h *PlatformHandler) ListPlatforms(c *gin.Context) {
|
||||
var platforms []models.Cloud
|
||||
h.db.Find(&platforms)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: models.ListResponse{
|
||||
List: platforms,
|
||||
Total: int64(len(platforms)),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// CreatePlatform 创建云平台
|
||||
func (h *PlatformHandler) CreatePlatform(c *gin.Context) {
|
||||
var req platformRequest
|
||||
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
|
||||
}
|
||||
|
||||
cloudType := strings.TrimSpace(req.Type)
|
||||
if cloudType == "" {
|
||||
cloudType = "public"
|
||||
}
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
|
||||
platform := models.Cloud{
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Code: strings.TrimSpace(req.Code),
|
||||
Type: cloudType,
|
||||
Status: status,
|
||||
Website: req.Website,
|
||||
Description: req.Description,
|
||||
}
|
||||
if err := h.db.Create(&platform).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "创建失败,编码可能已存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "创建成功", Data: platform})
|
||||
}
|
||||
|
||||
// UpdatePlatform 更新云平台
|
||||
func (h *PlatformHandler) UpdatePlatform(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
var req platformRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var platform models.Cloud
|
||||
if err := h.db.First(&platform, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "云平台不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.Name) != "" {
|
||||
platform.Name = strings.TrimSpace(req.Name)
|
||||
}
|
||||
if strings.TrimSpace(req.Code) != "" {
|
||||
platform.Code = strings.TrimSpace(req.Code)
|
||||
}
|
||||
if strings.TrimSpace(req.Type) != "" {
|
||||
platform.Type = strings.TrimSpace(req.Type)
|
||||
}
|
||||
if strings.TrimSpace(req.Status) != "" {
|
||||
platform.Status = strings.TrimSpace(req.Status)
|
||||
}
|
||||
platform.Website = req.Website
|
||||
platform.Description = req.Description
|
||||
|
||||
if err := h.db.Save(&platform).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "更新失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "更新成功", Data: platform})
|
||||
}
|
||||
|
||||
// DeletePlatform 删除云平台
|
||||
func (h *PlatformHandler) DeletePlatform(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
if err := h.db.Delete(&models.Cloud{}, id).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "删除失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "删除成功"})
|
||||
}
|
||||
|
||||
// ==================== 区域管理(Region)====================
|
||||
|
||||
// RegionHandler 区域处理器
|
||||
type RegionHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRegionHandler(db *gorm.DB) *RegionHandler {
|
||||
return &RegionHandler{db: db}
|
||||
}
|
||||
|
||||
// regionRequest 区域创建/更新请求
|
||||
type regionRequest struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Provider string `json:"provider"`
|
||||
Location string `json:"location"`
|
||||
Status string `json:"status"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
}
|
||||
|
||||
// ListRegions 获取区域列表
|
||||
func (h *RegionHandler) ListRegions(c *gin.Context) {
|
||||
var regions []models.Region
|
||||
h.db.Find(®ions)
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
Data: models.ListResponse{
|
||||
List: regions,
|
||||
Total: int64(len(regions)),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// CreateRegion 创建区域
|
||||
func (h *RegionHandler) CreateRegion(c *gin.Context) {
|
||||
var req regionRequest
|
||||
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
|
||||
}
|
||||
|
||||
status := strings.TrimSpace(req.Status)
|
||||
if status == "" {
|
||||
status = "active"
|
||||
}
|
||||
|
||||
region := models.Region{
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
Code: strings.TrimSpace(req.Code),
|
||||
Provider: strings.TrimSpace(req.Provider),
|
||||
Location: req.Location,
|
||||
Status: status,
|
||||
Endpoint: req.Endpoint,
|
||||
}
|
||||
if err := h.db.Create(®ion).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "创建失败,编码可能已存在"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "创建成功", Data: region})
|
||||
}
|
||||
|
||||
// UpdateRegion 更新区域
|
||||
func (h *RegionHandler) UpdateRegion(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
var req regionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, models.Response{Code: 400, Message: "请求参数错误: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var region models.Region
|
||||
if err := h.db.First(®ion, id).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, models.Response{Code: 404, Message: "区域不存在"})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.Name) != "" {
|
||||
region.Name = strings.TrimSpace(req.Name)
|
||||
}
|
||||
if strings.TrimSpace(req.Code) != "" {
|
||||
region.Code = strings.TrimSpace(req.Code)
|
||||
}
|
||||
if strings.TrimSpace(req.Provider) != "" {
|
||||
region.Provider = strings.TrimSpace(req.Provider)
|
||||
}
|
||||
region.Location = req.Location
|
||||
if strings.TrimSpace(req.Status) != "" {
|
||||
region.Status = strings.TrimSpace(req.Status)
|
||||
}
|
||||
region.Endpoint = req.Endpoint
|
||||
|
||||
if err := h.db.Save(®ion).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "更新失败"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, models.Response{Code: 200, Message: "更新成功", Data: region})
|
||||
}
|
||||
|
||||
// DeleteRegion 删除区域
|
||||
func (h *RegionHandler) DeleteRegion(c *gin.Context) {
|
||||
id, _ := strconv.Atoi(c.Param("id"))
|
||||
if err := h.db.Delete(&models.Region{}, 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