F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
203 lines
5.1 KiB
Go
203 lines
5.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"ezops/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CloudAccountHandler 云账号处理器
|
|
type CloudAccountHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCloudAccountHandler 创建云账号处理器
|
|
func NewCloudAccountHandler(db *gorm.DB) *CloudAccountHandler {
|
|
return &CloudAccountHandler{db: db}
|
|
}
|
|
|
|
// ListCloudAccounts 获取云账号列表
|
|
// @Summary 获取云账号列表
|
|
// @Description 获取所有云账号列表
|
|
// @Tags 云账号
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Success 200 {object} model.Response{data=model.ListResponse}
|
|
// @Router /cloud-accounts [get]
|
|
func (h *CloudAccountHandler) ListCloudAccounts(c *gin.Context) {
|
|
var accounts []model.CloudAccount
|
|
h.db.Find(&accounts)
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: model.ListResponse{
|
|
List: accounts,
|
|
Total: int64(len(accounts)),
|
|
},
|
|
})
|
|
}
|
|
|
|
// createCloudAccountRequest 创建云账号请求(独立 request struct,避免 CloudAccount.AccessKey/SecretKey 的 json:"-" 导致无法反序列化)
|
|
type createCloudAccountRequest struct {
|
|
Name string `json:"name"`
|
|
Provider string `json:"provider"`
|
|
AccessKey string `json:"access_key"`
|
|
SecretKey string `json:"secret_key"`
|
|
MonthlyCost float64 `json:"monthly_cost"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// CreateCloudAccount 创建云账号
|
|
// @Summary 创建云账号
|
|
// @Description 创建新云账号
|
|
// @Tags 云账号
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param request body createCloudAccountRequest true "创建请求"
|
|
// @Success 200 {object} model.Response{data=model.CloudAccount}
|
|
// @Router /cloud-accounts [post]
|
|
func (h *CloudAccountHandler) CreateCloudAccount(c *gin.Context) {
|
|
var req createCloudAccountRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
if strings.TrimSpace(req.Name) == "" || strings.TrimSpace(req.Provider) == "" {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "账号名称和云平台不能为空",
|
|
})
|
|
return
|
|
}
|
|
|
|
status := strings.TrimSpace(req.Status)
|
|
if status == "" {
|
|
status = "active"
|
|
}
|
|
|
|
account := model.CloudAccount{
|
|
Name: strings.TrimSpace(req.Name),
|
|
Provider: strings.TrimSpace(req.Provider),
|
|
AccessKey: req.AccessKey,
|
|
SecretKey: req.SecretKey,
|
|
MonthlyCost: req.MonthlyCost,
|
|
Status: status,
|
|
}
|
|
|
|
if err := h.db.Create(&account).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "创建失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "创建成功",
|
|
Data: account,
|
|
})
|
|
}
|
|
|
|
// UpdateCloudAccount 更新云账号
|
|
// @Summary 更新云账号
|
|
// @Description 更新指定云账号信息
|
|
// @Tags 云账号
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "云账号ID"
|
|
// @Param request body createCloudAccountRequest true "更新请求"
|
|
// @Success 200 {object} model.Response{data=model.CloudAccount}
|
|
// @Router /cloud-accounts/{id} [put]
|
|
func (h *CloudAccountHandler) UpdateCloudAccount(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
var req createCloudAccountRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "请求参数错误: " + err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
var account model.CloudAccount
|
|
if err := h.db.First(&account, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, model.Response{
|
|
Code: 404,
|
|
Message: "云账号不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
// 只在提供非空值时更新凭证字段
|
|
if strings.TrimSpace(req.Name) != "" {
|
|
account.Name = strings.TrimSpace(req.Name)
|
|
}
|
|
if strings.TrimSpace(req.Provider) != "" {
|
|
account.Provider = strings.TrimSpace(req.Provider)
|
|
}
|
|
if strings.TrimSpace(req.AccessKey) != "" {
|
|
account.AccessKey = req.AccessKey
|
|
}
|
|
if strings.TrimSpace(req.SecretKey) != "" {
|
|
account.SecretKey = req.SecretKey
|
|
}
|
|
if strings.TrimSpace(req.Status) != "" {
|
|
account.Status = strings.TrimSpace(req.Status)
|
|
}
|
|
account.MonthlyCost = req.MonthlyCost
|
|
|
|
if err := h.db.Save(&account).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "更新失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "更新成功",
|
|
Data: account,
|
|
})
|
|
}
|
|
|
|
// DeleteCloudAccount 删除云账号
|
|
// @Summary 删除云账号
|
|
// @Description 删除指定云账号
|
|
// @Tags 云账号
|
|
// @Produce json
|
|
// @Security Bearer
|
|
// @Param id path int true "云账号ID"
|
|
// @Success 200 {object} model.Response
|
|
// @Router /cloud-accounts/{id} [delete]
|
|
func (h *CloudAccountHandler) DeleteCloudAccount(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
|
|
if err := h.db.Delete(&model.CloudAccount{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "删除失败",
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "删除成功",
|
|
})
|
|
}
|
|
|