Files
aiops/backend/internal/api/handlers/alert_channels.go
T

207 lines
5.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package handlers
import (
"net/http"
"strconv"
"ezops/internal/models"
"github.com/gin-gonic/gin"
)
// 通知渠道 CRUD(AlertHandler 方法接收者,与 alert.go 同包共享) // Logan.Li
// ListNotifyChannels 获取通知渠道列表 // Logan.Li
// @Summary 获取通知渠道列表
// @Description 获取所有通知渠道列表
// @Tags 告警
// @Produce json
// @Security Bearer
// @Success 200 {object} models.Response{data=models.ListResponse}
// @Router /alerts/notify-channels [get]
func (h *AlertHandler) ListNotifyChannels(c *gin.Context) {
var channels []models.NotifyChannel
h.db.Order("created_at DESC").Find(&channels)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "success",
Data: models.ListResponse{
List: channels,
Total: int64(len(channels)),
},
})
}
// CreateNotifyChannel 创建通知渠道 // Logan.Li
// @Summary 创建通知渠道
// @Description 创建新通知渠道
// @Tags 告警
// @Accept json
// @Produce json
// @Security Bearer
// @Param request body models.CreateNotifyChannelRequest true "创建请求"
// @Success 200 {object} models.Response{data=models.NotifyChannel}
// @Router /alerts/notify-channels [post]
func (h *AlertHandler) CreateNotifyChannel(c *gin.Context) {
var req models.CreateNotifyChannelRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
channel := models.NotifyChannel{
Name: req.Name,
Type: req.Type,
Config: req.Config,
Enabled: true,
}
if err := h.db.Create(&channel).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "创建失败",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "创建成功",
Data: channel,
})
}
// UpdateNotifyChannel 更新通知渠道 // Logan.Li
// @Summary 更新通知渠道
// @Description 更新指定通知渠道
// @Tags 告警
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "渠道ID"
// @Param request body models.UpdateNotifyChannelRequest true "更新请求"
// @Success 200 {object} models.Response{data=models.NotifyChannel}
// @Router /alerts/notify-channels/{id} [put]
func (h *AlertHandler) UpdateNotifyChannel(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var req models.UpdateNotifyChannelRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, models.Response{
Code: 400,
Message: "请求参数错误: " + err.Error(),
})
return
}
var channel models.NotifyChannel
if err := h.db.First(&channel, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "通知渠道不存在",
})
return
}
updates := map[string]interface{}{}
if req.Name != "" {
updates["name"] = req.Name
}
if req.Type != "" {
updates["type"] = req.Type
}
if req.Config != "" {
updates["config"] = req.Config
}
if req.Enabled != nil {
updates["enabled"] = *req.Enabled
}
if err := h.db.Model(&channel).Updates(updates).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "更新失败",
})
return
}
h.db.First(&channel, id)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "更新成功",
Data: channel,
})
}
// DeleteNotifyChannel 删除通知渠道 // Logan.Li
// @Summary 删除通知渠道
// @Description 删除指定通知渠道
// @Tags 告警
// @Produce json
// @Security Bearer
// @Param id path int true "渠道ID"
// @Success 200 {object} models.Response
// @Router /alerts/notify-channels/{id} [delete]
func (h *AlertHandler) DeleteNotifyChannel(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
if err := h.db.Delete(&models.NotifyChannel{}, id).Error; err != nil {
c.JSON(http.StatusInternalServerError, models.Response{
Code: 500,
Message: "删除失败",
})
return
}
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: "删除成功",
})
}
// TestNotifyChannel 测试通知渠道 // Logan.Li
// @Summary 测试通知渠道连通性
// @Description 模拟测试通知渠道(返回测试状态)
// @Tags 告警
// @Produce json
// @Security Bearer
// @Param id path int true "渠道ID"
// @Success 200 {object} models.Response
// @Router /alerts/notify-channels/{id}/test [post]
func (h *AlertHandler) TestNotifyChannel(c *gin.Context) {
id, _ := strconv.Atoi(c.Param("id"))
var channel models.NotifyChannel
if err := h.db.First(&channel, id).Error; err != nil {
c.JSON(http.StatusNotFound, models.Response{
Code: 404,
Message: "通知渠道不存在",
})
return
}
// 模拟测试:校验配置非空即视为成功
status := "success"
message := "测试通知已发送"
if channel.Config == "" {
status = "warning"
message = "渠道未配置参数,请先完善配置"
}
h.db.Model(&channel).Update("test_status", status)
c.JSON(http.StatusOK, models.Response{
Code: 200,
Message: message,
Data: gin.H{
"status": status,
"channel": channel.Name,
"type": channel.Type,
},
})
}