package controller import ( "errors" "github.com/gin-gonic/gin" "github.com/spf13/cast" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" ) // AddHostManage 新增 func AddHostManage(c *gin.Context) { var req request.AddHostManageReq if err := c.ShouldBindJSON(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } //参数校验 if req.HostName == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组名称为空")), nil) return } if req.HostFileUrl == "" { if len(req.HostManageList) == 0 { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量为0")), nil) return } if len(req.HostManageList) > 5 { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量超过5条")), nil) return } for _, v := range req.HostManageList { if v.Ip == "" || v.UserName == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("ip或用户名为空")), nil) return } if v.VoucherType == 0 && v.Password == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("密码为空")), nil) return } } } hostManageSvc := service.HostManageSvc{} err := hostManageSvc.AddHostManage(req) if err != nil { SendJsonResponse(c, err, nil) return } SendJsonResponse(c, resp.OK, nil) } // EditHostManage 编辑 func EditHostManage(c *gin.Context) { var req request.EditHostManageReq if err := c.ShouldBindJSON(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } //参数校验 if req.HostName == "" || req.Id == 0 { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组ID或名称为空")), nil) return } if req.HostFileUrl == "" { if len(req.HostManageList) == 0 { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量为0")), nil) return } if len(req.HostManageList) > 5 { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量超过5条")), nil) return } for _, v := range req.HostManageList { if v.Ip == "" || v.UserName == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("ip或用户名为空")), nil) return } if v.VoucherType == 0 && v.Password == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("密码为空")), nil) return } } } hostManageSvc := service.HostManageSvc{} err := hostManageSvc.EditHostManage(req) if err != nil { SendJsonResponse(c, err, nil) return } SendJsonResponse(c, resp.OK, nil) } // DelHostManage 删除 func DelHostManage(c *gin.Context) { var req request.DelHostManageReq if err := c.ShouldBindJSON(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } //参数校验 if err := util.ValidateSimple(req, "Id"); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } hostManageSvc := service.HostManageSvc{} err := hostManageSvc.DelHostManage(req) if err != nil { SendJsonResponse(c, err, nil) return } SendJsonResponse(c, resp.OK, nil) } // DetailsHostManage 详情 func DetailsHostManage(c *gin.Context) { var ( err error id string ) if id = c.Query("id"); id == "" { id = c.Param("id") } if id == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil) return } hostManageSvc := service.HostManageSvc{} data, err := hostManageSvc.DetailsHostManage(cast.ToInt(id)) if err != nil { SendJsonResponse(c, err, nil) return } SendJsonResponse(c, resp.OK, data) } // ListHostManage 列表 func ListHostManage(c *gin.Context) { var req request.ListHostManageReq if err := c.ShouldBindJSON(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } hostManageSvc := service.HostManageSvc{} total, list, err := hostManageSvc.ListHostManage(req) if err != nil { SendJsonPageResponse(c, err, nil, 0) return } SendJsonPageResponse(c, resp.OK, list, total) } // StateHostManage 状态检测 func StateHostManage(c *gin.Context) { var req request.StateHostManageReq if err := c.ShouldBindJSON(&req); err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) return } hostManageSvc := service.HostManageSvc{} total, list, err := hostManageSvc.StateHostManage(req) if err != nil { SendJsonPageResponse(c, err, nil, 0) return } SendJsonPageResponse(c, resp.OK, list, total) }