diff --git a/src/bean/vo/request/doc_lib.go b/src/bean/vo/request/doc_lib.go index 725b8fc210d39a62328ca5206e351ffd995fc29e..7b227c95e75a0348379f05f8a3795cdd57e3d40c 100644 --- a/src/bean/vo/request/doc_lib.go +++ b/src/bean/vo/request/doc_lib.go @@ -1 +1,7 @@ package request + +type DocLibGetReq struct { + FileName string `uri:"file_name" binding:"required"` //文档名称 + Ext string `uri:"ext" binding:"required"` //后缀格式 + Opt string `uri:"opt" binding:"required,oneof=preview download"` //操作 +} diff --git a/src/bean/vo/response/host_manage.go b/src/bean/vo/response/host_manage.go index c659c63adb27377af86224b7bc036f1d79e99a0c..909ed279601fe8795d94fefb6d50049f1fbe9f55 100644 --- a/src/bean/vo/response/host_manage.go +++ b/src/bean/vo/response/host_manage.go @@ -26,13 +26,14 @@ type HostManage struct { } type HostList struct { - Id int `json:"id"` // id + //Id int `json:"id"` // id Ip string `json:"ip"` // ip Port string `json:"port"` // 端口 VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证) UserName string `json:"user_name"` // 用户名 Password string `json:"password"` // 密码 HostFileUrl string `json:"-"` // 主机文件url + Cnt int `json:"cnt""` //主机ip数量 } type TaskList struct { diff --git a/src/bean/vo/response/task_manage.go b/src/bean/vo/response/task_manage.go index b231330db4720f79be0e0a55e78341ffbcdfc69b..9a620245e6822fd6828ea82a3f3a2a78108c42f5 100644 --- a/src/bean/vo/response/task_manage.go +++ b/src/bean/vo/response/task_manage.go @@ -13,6 +13,7 @@ type TaskManageListRes struct { type TaskManageRes struct { Id int `json:"id"` // id + HostGroupId int `json:"host_group_id"` // 主机分组id TaskName string `json:"task_name"` // 任务名称 TaskDesc string `json:"task_desc"` // 任务描述 YamlDesc string `json:"yaml_desc"` // yaml文件 diff --git a/src/controller/doc_lib.go b/src/controller/doc_lib.go index 78a17e686e483c7ba4eeac7c7db6692d5b0ba4c7..2d7f70650b844567d4daf6ec815996c8eef9a09f 100644 --- a/src/controller/doc_lib.go +++ b/src/controller/doc_lib.go @@ -2,8 +2,12 @@ package controller import ( "github.com/gin-gonic/gin" + "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" + "net/http" + "net/http/httputil" + "net/url" ) func AddFile(c *gin.Context) { @@ -16,3 +20,32 @@ func AddFile(c *gin.Context) { } SendJsonResponse(c, resp.OK, doc) } + +func DownloadFile(c *gin.Context) { + var req request.DocLibGetReq + if err := c.ShouldBindUri(&req); err != nil { + SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) + return + } + docLibSvc := service.DocLibSvc{} + docUrl, err := docLibSvc.DownloadFile(c, req) + if err != nil { + SendJsonResponse(c, resp.FAIL.WithError(err), nil) + return + } + switch req.Opt { + case "download": + //c.Redirect(http.StatusMovedPermanently, docUrl.String()) + docUrlParse, _ := url.Parse(docUrl.String()) + proxy := httputil.ReverseProxy{ + Director: func(req *http.Request) { + req.Header = c.Request.Header + req.Host = docUrlParse.Host + req.URL = docUrlParse + }, + } + proxy.ServeHTTP(c.Writer, c.Request) + case "preview": + c.Redirect(http.StatusMovedPermanently, docUrl.String()) + } +} diff --git a/src/controller/host_manage.go b/src/controller/host_manage.go index 86c1ffbf2963072b753b4b93c4b06b6d8121985f..537b5a6752e3b4c4a604a30b63bd5332ef793cb5 100644 --- a/src/controller/host_manage.go +++ b/src/controller/host_manage.go @@ -80,8 +80,8 @@ func GetMinioFiles(fileName string) (hostManageList []request.HostManageList, er } for i := 0; i < len(rows); i++ { - //默认跳过第一行 - if i < 1 { + //默认跳过前两行 + if i < 2 { continue } @@ -371,11 +371,11 @@ func ExportIp(c *gin.Context) { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("导出类型解析错误")), nil) return } - if id == "" { + if cast.ToInt(detectionType) == 1 && id == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil) return } - if uuid == "" { + if cast.ToInt(detectionType) == 0 && uuid == "" { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("uuid为空")), nil) return } diff --git a/src/router/router.go b/src/router/router.go index c6eca32bea94b60fd508fc5625732ba37ed3eae3..187738e6a3a295e27ac40a8906385fb5c42a2adf 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -28,7 +28,8 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { base.GET("/example/list", controller.GetExampleList) // 示例获取列表 } - base.POST("/add_file", controller.AddFile) //文件上传 + base.POST("/add_file", controller.AddFile) //文件上传 + base.GET("/download_file/:file_name/:ext/:opt", controller.DownloadFile) //文件下载 // 初始化自动化运维路由 InitAutomatedMaintenRouter(r) @@ -46,4 +47,6 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { InitMetricConfigRouter(r) // 初始化prometheus路由 InitPrometheusRouter(r) + // 初始化工单管理路由 + //InitAutomatedMaintenRouter(r) } diff --git a/src/service/doc_lib.go b/src/service/doc_lib.go index b1fc7e8632e34828fd83bbbad2c06a5acf36cadd..75d20526bdcdc821f89c9a66bce7ecea0e891449 100644 --- a/src/service/doc_lib.go +++ b/src/service/doc_lib.go @@ -7,12 +7,15 @@ import ( "github.com/google/uuid" "github.com/minio/minio-go/v7" "github.com/pkg/errors" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "io" "mime/multipart" "net/http" + "net/url" "path" + "time" ) var maxFileByte int64 = 20971520 //20M @@ -111,3 +114,33 @@ func (d DocLibSvc) GetFile(fileName string) (obj io.Reader, err error) { // return //} } + +func (d DocLibSvc) DownloadFile(ctx *gin.Context, req request.DocLibGetReq) (docUrl *url.URL, err error) { + var ( + minioClient *minio.Client + ) + minioClient, err = client.GetMinioConnect() + if err != nil { + err = errors.Wrap(err, "getMinio") + return + } + + remotePath := fmt.Sprintf("%s%s", req.FileName, req.Ext) + reqParams := make(url.Values) + switch req.Opt { + case "info": + return + case "preview": + reqParams.Set("response-Content-Type", req.Ext) + case "download": + reqParams.Set("response-Content-Disposition", fmt.Sprintf("attachment; filename=%s%s", req.FileName, req.Ext)) + reqParams.Set("response-Content-Type", "application/octet-stream") + reqParams.Set("response-Content-Transfer-Encoding", "binary") + } + docUrl, err = minioClient.PresignedGetObject(ctx, conf.Options.MinioBucket, remotePath, time.Second*60*60, reqParams) + if err != nil { + err = errors.Wrap(err, "get file url from minio fail") + return + } + return +} diff --git a/src/service/host_manage.go b/src/service/host_manage.go index 39a647e917332d5eb78325227dadafd22064c37a..b77c72ea9a63230be68669474ee143513fdb1bb8 100644 --- a/src/service/host_manage.go +++ b/src/service/host_manage.go @@ -305,7 +305,9 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa return } //查询主机列表 - err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList) + err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id). + Select("string_agg(ip,',') as ip,port,voucher_type,user_name,password,host_file_url,count(1) as cnt"). + GroupBy("ip_group,port,voucher_type,user_name,PASSWORD,host_file_url").Find(&hostList) if err != nil { err = resp.DbSelectError.WithError(err) return @@ -321,18 +323,20 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa if v.HostFileUrl != "" { hostManageRes.HostFileUrl = v.HostFileUrl } - + hostManageRes.IpCnt = hostManageRes.IpCnt + v.Cnt } hostManageRes.Id = hostManage.Id hostManageRes.HostName = hostManage.HostName hostManageRes.TaskCnt = len(taskList) - hostManageRes.IpCnt = len(hostList) + //hostManageRes.IpCnt = len(hostList) hostManageRes.CreateUser = hostManage.CreateUser hostManageRes.CreateTime = hostManage.CreateTime hostManageRes.UpdateUser = hostManage.UpdateUser hostManageRes.UpdateTime = hostManage.UpdateTime - hostManageRes.HostList = hostList + if hostManageRes.HostFileUrl == "" { + hostManageRes.HostList = hostList + } hostManageRes.TaskList = taskList return diff --git a/src/service/task_manage.go b/src/service/task_manage.go index e2809409e65f93afdde52080cbd961e89fbca486..0199ebccb7233191d402a5ef2c262b5fb4738b51 100644 --- a/src/service/task_manage.go +++ b/src/service/task_manage.go @@ -117,7 +117,7 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa //查询任务详情 finder := db.Table("task_manage").Alias("tm"). Where("is_delete = 0 AND id = ?", id) - _, err = finder.Select("tm.id,tm.task_name,tm.task_desc,tm.yaml_desc,tm.yaml_url,tm.create_user,tm.create_time," + + _, err = finder.Select("tm.id,tm.task_name,tm.task_desc,tm.yaml_desc,tm.yaml_url,tm.create_user,tm.create_time,tm.host_group_id," + "(select count(1) from task_history th where th.task_id = tm.id) as exec_cnt," + "(select count(1) from task_history th where th.task_id = tm.id and th.state = 1) as success_cnt," + "(select count(1) from task_history th where th.task_id = tm.id and th.state = 2) as fail_cnt").Get(&taskManageRes) @@ -128,18 +128,27 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa //查询主机列表 hostList := make([]response.HostList, 0) - err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList) + err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", taskManageRes.HostGroupId). + Select("string_agg(ip,',') as ip,port,voucher_type,user_name,password,host_file_url"). + GroupBy("ip_group,port,voucher_type,user_name,PASSWORD,host_file_url").Find(&hostList) if err != nil { err = resp.DbSelectError.WithError(err) return } + + //err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList) + //if err != nil { + // err = resp.DbSelectError.WithError(err) + // return + //} for _, v := range hostList { if v.HostFileUrl != "" { taskManageRes.HostFileUrl = v.HostFileUrl } - } - taskManageRes.HostList = hostList + if taskManageRes.HostFileUrl == "" { + taskManageRes.HostList = hostList + } return }