diff --git a/go.mod b/go.mod index 7d58b7ae7e5b2f98b2825144917527a973e258db..50992abd49a08caa0e63868fa3cb45fa38ef5990 100644 --- a/go.mod +++ b/go.mod @@ -12,6 +12,8 @@ require ( github.com/go-playground/validator/v10 v10.13.0 github.com/go-redis/redis v6.15.9+incompatible github.com/google/uuid v1.3.0 + github.com/jinzhu/copier v0.3.5 + github.com/json-iterator/go v1.1.12 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lib/pq v1.10.9 github.com/minio/minio-go/v7 v7.0.58 @@ -20,6 +22,7 @@ require ( github.com/satori/go.uuid v1.2.0 github.com/spf13/cast v1.5.0 github.com/spf13/pflag v1.0.5 + github.com/tealeg/xlsx v1.0.5 github.com/thoas/go-funk v0.9.3 github.com/valyala/fasthttp v1.47.0 go.uber.org/zap v1.24.0 @@ -41,8 +44,6 @@ require ( github.com/golang/snappy v0.0.4 // indirect github.com/henrylee2cn/ameda v1.5.0 // indirect github.com/henrylee2cn/goutil v0.0.0-20220704075712-42f2ec55fe8d // indirect - github.com/jinzhu/copier v0.3.5 // indirect - github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/compress v1.16.5 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.3 // indirect diff --git a/go.sum b/go.sum index f939272446a08044b4db91029d284b9f3db3da10..2d958ffb7102d886ef75f5dcc1af119e683a4925 100644 --- a/go.sum +++ b/go.sum @@ -436,6 +436,8 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= +github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE= +github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM= github.com/thoas/go-funk v0.9.3 h1:7+nAEx3kn5ZJcnDm2Bh23N2yOtweO14bi//dvRtgLpw= github.com/thoas/go-funk v0.9.3/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= @@ -628,8 +630,9 @@ google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175 google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/cheggaaa/pb.v1 v1.0.25/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= diff --git a/src/bean/entity/host_manage.go b/src/bean/entity/host_manage.go index 08c1db639bce17b648152ad757910f71e28fcd74..4a9e18b1d1be87cffc44b99866e2a49dcebd2f78 100644 --- a/src/bean/entity/host_manage.go +++ b/src/bean/entity/host_manage.go @@ -3,36 +3,36 @@ package entity import "time" type HostManage struct { - Id int `json:"id" xorm:"pk autoincr" ` // id - HostName string `json:"hostName" xorm:"host_name"` // 主机分组名称 - IsDelete int `json:"isDelete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除) - CreateUser string `json:"createUser" xorm:"create_user" ` // 创建人 - CreateTime time.Time `json:"createTime" xorm:"create_time"` // 创建时间 - UpdateUser string `json:"updateUser" xorm:"update_user" ` // 修改人 - UpdateTime time.Time `json:"updateTime" xorm:"update_time"` // 修改时间 + Id int `json:"id" xorm:"pk autoincr" ` // id + HostName string `json:"host_name" xorm:"host_name"` // 主机分组名称 + IsDelete int `json:"is_delete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除) + CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人 + CreateTime time.Time `json:"create_time" xorm:"create_time"` // 创建时间 + UpdateUser string `json:"update_user" xorm:"update_user" ` // 修改人 + UpdateTime time.Time `json:"update_time" xorm:"update_time"` // 修改时间 } type HostManageList struct { - Id int `json:"id" xorm:"pk autoincr" ` // id - Ip string `json:"ip" xorm:"ip"` // ip - Port string `json:"port" xorm:"port"` // 端口 - VoucherType int `json:"voucherType" xorm:"voucher_type"` // 凭证类型(0密码验证 密钥验证) - UserName string `json:"userName" xorm:"user_name"` // 用户名 - Password string `json:"password" xorm:"password"` // 密码 - HostGroupId int `json:"hostGroupId" xorm:"host_group_id"` // 主机分组id - HostFileUrl string `json:"hostFileUrl" xorm:"host_file_url"` // 主机文件url - IsDelete int `json:"isDelete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除) - ConnStatus int `json:"connStatus" xorm:"conn_status" ` // 连接状态(0正常 1异常) - IpGroup int `json:"ipGroup" xorm:"ip_group" ` // ip分组 + Id int `json:"id" xorm:"pk autoincr" ` // id + Ip string `json:"ip" xorm:"ip"` // ip + Port string `json:"port" xorm:"port"` // 端口 + VoucherType int `json:"voucher_type" xorm:"voucher_type"` // 凭证类型(0密码验证 1密钥验证) + UserName string `json:"user_name" xorm:"user_name"` // 用户名 + Password string `json:"password" xorm:"password"` // 密码 + HostGroupId int `json:"host_group_id" xorm:"host_group_id"` // 主机分组id + HostFileUrl string `json:"host_file_url" xorm:"host_file_url"` // 主机文件url + IsDelete int `json:"is_delete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除) + ConnStatus int `json:"conn_status" xorm:"conn_status" ` // 连接状态(0正常 1异常) + IpGroup int `json:"ip_group" xorm:"ip_group" ` // ip分组 } type HostManageListCache struct { - Id string `json:"id" xorm:"id" ` // id - Ip string `json:"ip" xorm:"ip"` // ip - Port string `json:"port" xorm:"port"` // 端口 - VoucherType int `json:"voucherType" xorm:"voucher_type"` // 凭证类型(0密码验证 密钥验证) - UserName string `json:"userName" xorm:"user_name"` // 用户名 - Password string `json:"password" xorm:"password"` // 密码 - ConnStatus int `json:"connStatus" xorm:"conn_status" ` // 连接状态(0正常 1异常) - IpGroup int `json:"ipGroup" xorm:"ip_group" ` // ip分组 + Id string `json:"id" xorm:"id" ` // id + Ip string `json:"ip" xorm:"ip"` // ip + Port string `json:"port" xorm:"port"` // 端口 + VoucherType int `json:"voucher_type" xorm:"voucher_type"` // 凭证类型(0密码验证 密钥验证) + UserName string `json:"user_name" xorm:"user_name"` // 用户名 + Password string `json:"password" xorm:"password"` // 密码 + ConnStatus int `json:"conn_status" xorm:"conn_status" ` // 连接状态(0正常 1异常) + IpGroup int `json:"ip_group" xorm:"ip_group" ` // ip分组 } diff --git a/src/bean/entity/task_manage.go b/src/bean/entity/task_manage.go index 8d10753f83809eafb3023d10a1766db39a1cd533..95894a2cd756af58e317d5e1a9d8475350121b66 100644 --- a/src/bean/entity/task_manage.go +++ b/src/bean/entity/task_manage.go @@ -3,18 +3,18 @@ package entity import "time" type TaskManage struct { - Id int `json:"id" xorm:"pk autoincr" ` // id - TaskName string `json:"taskName" xorm:"task_name"` // 任务名称 - TaskDesc string `json:"taskDesc" xorm:"task_desc"` // 任务描述 - YamlDesc string `json:"yamlDesc" xorm:"yaml_desc"` // yaml文件 - YamlUrl string `json:"yamlUrl" xorm:"yaml_url"` // yaml文件地址 - ExecCnt int `json:"execCnt" xorm:"exec_cnt"` // 执行次数 - SuccessCnt int `json:"successCnt" xorm:"success_cnt"` // 执行成功次数 - FailCnt int `json:"failCnt" xorm:"fail_cnt"` // 执行失败次数 - HostGroupId int `json:"hostGroupId" xorm:"host_group_id"` // 主机分组ID - IsDelete int `json:"isDelete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除) - CreateUser string `json:"createUser" xorm:"create_user" ` // 创建人 - CreateTime time.Time `json:"createTime" xorm:"create_time"` // 创建时间 - UpdateUser string `json:"updateUser" xorm:"update_user" ` // 修改人 - UpdateTime time.Time `json:"updateTime" xorm:"update_time"` // 修改时间 + Id int `json:"id" xorm:"pk autoincr" ` // id + TaskName string `json:"task_name" xorm:"task_name"` // 任务名称 + TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述 + YamlDesc string `json:"yaml_desc" xorm:"yaml_desc"` // yaml文件 + YamlUrl string `json:"yaml_url" xorm:"yaml_url"` // yaml文件地址 + ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数 + SuccessCnt int `json:"success_cnt" xorm:"success_cnt"` // 执行成功次数 + FailCnt int `json:"fail_cnt" xorm:"fail_cnt"` // 执行失败次数 + HostGroupId int `json:"host_group_id" xorm:"host_group_id"` // 主机分组ID + IsDelete int `json:"is_delete" xorm:"is_delete" ` // 是否删除(0 未删除 1已删除) + CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人 + CreateTime time.Time `json:"create_time" xorm:"create_time"` // 创建时间 + UpdateUser string `json:"update_user" xorm:"update_user" ` // 修改人 + UpdateTime time.Time `json:"update_time" xorm:"update_time"` // 修改时间 } diff --git a/src/bean/vo/request/host_manage.go b/src/bean/vo/request/host_manage.go index b6eedc318d80bf8630eb8b6d170926cae1526dcb..42872afb3b156a28882a5537d276037fc303e6a5 100644 --- a/src/bean/vo/request/host_manage.go +++ b/src/bean/vo/request/host_manage.go @@ -1,24 +1,26 @@ package request type AddHostManageReq struct { - HostName string `json:"hostName" binding:"required"` // 主机分组名称 - HostFileUrl string `json:"hostFileUrl"` // 主机文件url - HostManageList []HostManageList `json:"hostManageList"` //主机列表 + HostName string `json:"host_name" binding:"required"` // 主机分组名称 + HostFileUrl string `json:"host_file_url"` // 主机文件url + Uuid string `json:"uuid"` //临时缓存uuid + //HostManageList []HostManageList `json:"host_manage_list"` //主机列表 } type HostManageList struct { - Ip string `json:"ip"` // ip - Port string `json:"port"` // 端口 - VoucherType int `json:"voucherType"` // 凭证类型(0密码验证 1密钥验证) - UserName string `json:"userName"` // 用户名 - Password string `json:"password"` // 密码 - IpGroup int `json:"ipGroup"` // ip分组 + Ip string `json:"ip"` // ip + Port string `json:"port"` // 端口 + VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 1密钥验证) + UserName string `json:"user_name"` // 用户名 + Password string `json:"password"` // 密码 + IpGroup int `json:"ip_group"` // ip分组 } type EditHostManageReq struct { - Id int `json:"id" binding:"required"` // 主键ID - HostFileUrl string `json:"hostFileUrl"` // 主机文件url - HostManageList []HostManageList `json:"hostManageList"` //主机列表 + Id int `json:"id" binding:"required"` // 主键ID + HostFileUrl string `json:"host_file_url"` // 主机文件url + Uuid string `json:"uuid"` //临时缓存uuid + //HostManageList []HostManageList `json:"host_manage_list"` //主机列表 } type DelHostManageReq struct { @@ -26,24 +28,24 @@ type DelHostManageReq struct { } type ListHostManageReq struct { - Search string `json:"search"` //关键词 - CreateDateFrom string `json:"createDateFrom"` //创建时间从 - CreateDateTo string `json:"createDateTo"` //创建时间至 + Search string `json:"search"` //关键词 + CreateDateFrom string `json:"create_date_from"` //创建时间从 + CreateDateTo string `json:"create_date_to"` //创建时间至 Pagination } // StateHostManageReq 状态检测 type StateHostManageReq struct { - DetectionType int `json:"detectionType" form:"detectionType" binding:"oneof=1 2 3"` //1主页列表手动检测 2输入ip列表保存时检测 3上传ip列表文件保存时检测 - Id int `json:"id" form:"id"` //主机分组ID - //HostFile string `form:"hostFile"` // 主机文件 - HostManageList string `json:"hostManageList" form:"hostManageList"` //主机ip列表 + DetectionType int `json:"detection_type" form:"detection_type" binding:"oneof=1 2 3"` //1主页列表手动检测 2输入ip列表保存时检测 3上传ip列表文件保存时检测 + Id int `json:"id" form:"id"` //主机分组ID + //HostFile file `form:"host_file"` // 主机文件 + HostManageList string `json:"host_manage_list" form:"host_manage_list"` //主机ip列表 } // HostIpExceptionListReq 异常列表 type HostIpExceptionListReq struct { - DetectionType int `json:"detectionType" binding:"oneof=1 2"` //1主页异常ip列表 2保存检测时异常列表 - Id int `json:"id"` //主机分组ID - Uuid string `json:"uuid"` //临时缓存uuid + DetectionType int `json:"detection_type" binding:"oneof=1 2"` //1主页异常ip列表 2保存检测时异常列表 + Id int `json:"id"` //主机分组ID + Uuid string `json:"uuid"` //临时缓存uuid Pagination } diff --git a/src/bean/vo/request/task_manage.go b/src/bean/vo/request/task_manage.go index a33ecb6acfc96b20be2bb2815235c0f146e7577a..fae25e24a10a65b2fb80bfbcee04876595d6d995 100644 --- a/src/bean/vo/request/task_manage.go +++ b/src/bean/vo/request/task_manage.go @@ -1,19 +1,19 @@ package request type AddTaskManageReq struct { - TaskName string `json:"taskName" vd:"len($)>0;msg:'请输入任务名称'"` // 任务名称 - TaskDesc string `json:"taskDesc"` // 任务描述 - YamlDesc string `json:"yamlDesc"` // yaml内容 - YamlUrl string `json:"yamlUrl"` // yaml文件url - HostGroupId int `json:"hostGroupId" vd:"$>0;msg:'请选择主机分组'"` // 主机分组ID + TaskName string `json:"task_name" vd:"len($)>0;msg:'请输入任务名称'"` // 任务名称 + TaskDesc string `json:"task_desc"` // 任务描述 + YamlDesc string `json:"yaml_desc"` // yaml内容 + YamlUrl string `json:"yaml_url"` // yaml文件url + HostGroupId int `json:"host_group_id" vd:"$>0;msg:'请选择主机分组'"` // 主机分组ID } type EditTaskManageReq struct { - Id int `json:"id" vd:"$>0;msg:'请输入id'"` // 主键ID - TaskDesc string `json:"taskDesc"` // 任务描述 - YamlDesc string `json:"yamlDesc"` // yaml内容 - YamlUrl string `json:"yamlUrl"` // yaml文件url - HostGroupId int `json:"hostGroupId" vd:"$>0;msg:'请选择主机分组'"` // 主机分组ID + Id int `json:"id" vd:"$>0;msg:'请输入id'"` // 主键ID + TaskDesc string `json:"task_desc"` // 任务描述 + YamlDesc string `json:"yaml_desc"` // yaml内容 + YamlUrl string `json:"yaml_url"` // yaml文件url + HostGroupId int `json:"host_group_id" vd:"$>0;msg:'请选择主机分组'"` // 主机分组ID } type DelTaskManageReq struct { @@ -25,9 +25,9 @@ type DetailsTaskManageReq struct { } type ListTaskManageReq struct { - Search string `json:"search" form:"search"` //关键词 - CreateDateFrom string `json:"createDateFrom" form:"createDateFrom"` //创建时间从 - CreateDateTo string `json:"createDateTo" form:"createDateTo"` //创建时间至 + Search string `json:"search" form:"search"` //关键词 + CreateDateFrom string `json:"create_date_from" form:"createDateFrom"` //创建时间从 + CreateDateTo string `json:"create_date_to" form:"createDateTo"` //创建时间至 Pagination } diff --git a/src/bean/vo/response/host_manage.go b/src/bean/vo/response/host_manage.go index d46b9f115e6e8dfcf36d635419120ddc9134ce5e..701b962ea02ecedcf4c452b9b444e7e5cb009a43 100644 --- a/src/bean/vo/response/host_manage.go +++ b/src/bean/vo/response/host_manage.go @@ -3,71 +3,71 @@ package response import "time" type HostManageRes struct { - Id int `json:"id"` // id - HostName string `json:"hostName"` // 主机分组名称 - TaskCnt int `json:"taskCnt"` // 任务数量 - IpCnt int `json:"ipCnt"` // ip数量 - CreateUser string `json:"createUser"` // 创建人 - CreateTime time.Time `json:"createTime"` // 创建时间 - UpdateUser string `json:"updateUser"` // 修改人 - UpdateTime time.Time `json:"updateTime"` // 修改时间 - HostFileUrl string `json:"hostFileUrl"` // 主机文件url - HostList []HostList `json:"hostList"` // 主机列表 - TaskList []TaskList `json:"taskList"` // 主机列表 + Id int `json:"id"` // id + HostName string `json:"host_name"` // 主机分组名称 + TaskCnt int `json:"task_cnt"` // 任务数量 + IpCnt int `json:"ip_cnt"` // ip数量 + CreateUser string `json:"create_user"` // 创建人 + CreateTime time.Time `json:"create_time"` // 创建时间 + UpdateUser string `json:"update_user"` // 修改人 + UpdateTime time.Time `json:"update_time"` // 修改时间 + HostFileUrl string `json:"host_file_url"` // 主机文件url + HostList []HostList `json:"host_list"` // 主机列表 + TaskList []TaskList `json:"task_list"` // 任务列表 } type HostManage struct { - Id int `json:"id"` // id - HostName string `json:"hostName"` // 主机分组名称 - CreateUser string `json:"createUser"` // 创建人 - CreateTime time.Time `json:"createTime"` // 创建时间 - UpdateUser string `json:"updateUser"` // 修改人 - UpdateTime time.Time `json:"updateTime"` // 修改时间 + Id int `json:"id"` // id + HostName string `json:"host_name"` // 主机分组名称 + CreateUser string `json:"create_user"` // 创建人 + CreateTime time.Time `json:"create_time"` // 创建时间 + UpdateUser string `json:"update_user"` // 修改人 + UpdateTime time.Time `json:"update_time"` // 修改时间 } type HostList struct { - Id int `json:"id"` // id - Ip string `json:"ip"` // ip - Port string `json:"port"` // 端口 - HostFileUrl string `json:"hostFileUrl"` // 主机文件url + Id int `json:"id"` // id + Ip string `json:"ip"` // ip + Port string `json:"port"` // 端口 + HostFileUrl string `json:"host_file_url"` // 主机文件url } type TaskList struct { - Id int `json:"id"` // id - TaskName string `json:"taskName"` // 任务名称 - TaskDesc string `json:"taskDesc"` // 任务描述 - ExecCnt int `json:"execCnt"` // 执行次数 + Id int `json:"id"` // id + TaskName string `json:"task_name"` // 任务名称 + TaskDesc string `json:"task_desc"` // 任务描述 + ExecCnt int `json:"exec_cnt"` // 执行次数 } type HostManagesRes struct { - Id int `json:"id"` // id - HostName string `json:"hostName"` // 主机分组名称 - CreateUser string `json:"createUser"` // 创建人 - CreateTime time.Time `json:"createTime"` // 创建时间 - TaskCnt int `json:"taskCnt"` // 任务数量 - IpCntErr int `json:"ipCntErr"` // ip数量(异常) - IpCnt int `json:"ipCnt"` // ip数量(全部) + Id int `json:"id"` // id + HostName string `json:"host_name"` // 主机分组名称 + CreateUser string `json:"create_user"` // 创建人 + CreateTime time.Time `json:"create_time"` // 创建时间 + TaskCnt int `json:"task_cnt"` // 任务数量 + IpCntErr int `json:"ip_cnt_err"` // ip数量(异常) + IpCnt int `json:"ip_cnt"` // ip数量(全部) } type HostManageListRes struct { - Id int `json:"id"` // id - Ip string `json:"ip"` // ip - Port string `json:"port"` // 端口 - VoucherType int `json:"voucherType"` // 凭证类型(0密码验证 密钥验证) - UserName string `json:"userName"` // 用户名 - Password string `json:"password"` // 密码 + 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"` // 密码 } -type HostManageListCacheRes struct { - AormalHost []HostManageListCache `json:"normalHost"` // 正常ip - AbnormalHost []HostManageListCache `json:"abnormalHost"` // 异常ip -} +//type HostManageListCacheRes struct { +// AormalHost []HostManageListCache `json:"normal_host"` // 正常ip +// AbnormalHost []HostManageListCache `json:"abnormal_host"` // 异常ip +//} type HostManageListCache struct { - Ip string `json:"ip"` // ip - Port string `json:"port"` // 端口 - VoucherType int `json:"voucherType"` // 凭证类型(0密码验证 密钥验证) - UserName string `json:"userName"` // 用户名 - Password string `json:"password"` // 密码 - IpGroup int `json:"ipGroup"` // ip分组 + Ip string `json:"ip"` // ip + Port string `json:"port"` // 端口 + VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 1密钥验证) + UserName string `json:"user_name"` // 用户名 + Password string `json:"password"` // 密码 + IpGroup int `json:"ip_group"` // ip分组 } diff --git a/src/bean/vo/response/task_manage.go b/src/bean/vo/response/task_manage.go index 0876c10ae1b371ac66fa715053ebde424a291cdf..cfdf6850d52e703d23e09faf316821a7da779434 100644 --- a/src/bean/vo/response/task_manage.go +++ b/src/bean/vo/response/task_manage.go @@ -3,17 +3,17 @@ package response import "time" type TaskManageRes struct { - Id int `json:"id" xorm:"pk autoincr" ` // id - TaskName string `json:"taskName" xorm:"task_name"` // 任务名称 - TaskDesc string `json:"taskDesc" xorm:"task_desc"` // 任务描述 - YamlDesc string `json:"yamlDesc" xorm:"yaml_desc"` // yaml文件 - YamlUrl string `json:"yamlUrl" xorm:"yaml_url"` // yaml文件地址 - ExecCnt int `json:"execCnt" xorm:"exec_cnt"` // 执行次数 - SuccessCnt int `json:"successCnt" xorm:"success_cnt"` // 执行成功次数 - FailCnt int `json:"failCnt" xorm:"fail_cnt"` // 执行失败次数 - HostGroupId int `json:"hostGroupId" xorm:"host_group_id"` // 主机分组ID - CreateUser string `json:"createUser" xorm:"create_user" ` // 创建人 - CreateTime time.Time `json:"createTime" xorm:"create_time"` // 创建时间 - UpdateUser string `json:"updateUser" xorm:"update_user" ` // 修改人 - UpdateTime time.Time `json:"updateTime" xorm:"update_time"` // 修改时间 + Id int `json:"id" xorm:"pk autoincr" ` // id + TaskName string `json:"task_name" xorm:"task_name"` // 任务名称 + TaskDesc string `json:"task_desc" xorm:"task_desc"` // 任务描述 + YamlDesc string `json:"yaml_desc" xorm:"yaml_desc"` // yaml文件 + YamlUrl string `json:"yaml_url" xorm:"yaml_url"` // yaml文件地址 + ExecCnt int `json:"exec_cnt" xorm:"exec_cnt"` // 执行次数 + SuccessCnt int `json:"success_cnt" xorm:"success_cnt"` // 执行成功次数 + FailCnt int `json:"fail_cnt" xorm:"fail_cnt"` // 执行失败次数 + HostGroupId int `json:"host_group_id" xorm:"host_group_id"` // 主机分组ID + CreateUser string `json:"create_user" xorm:"create_user" ` // 创建人 + CreateTime time.Time `json:"create_time" xorm:"create_time"` // 创建时间 + UpdateUser string `json:"update_user" xorm:"update_user" ` // 修改人 + UpdateTime time.Time `json:"update_time" xorm:"update_time"` // 修改时间 } diff --git a/src/common/conf/options.go b/src/common/conf/options.go index bb46ac7a2836e70d5da753aaff273dc4dd477d70..726daa05cb7630900198032514e3065f5875a807 100644 --- a/src/common/conf/options.go +++ b/src/common/conf/options.go @@ -30,6 +30,7 @@ type Config struct { MinioAccessKey string MinioSecretKey string MinioBucket string + TempDirPrefix string } const ( diff --git a/src/controller/host_manage.go b/src/controller/host_manage.go index 14a2b1a7483b20c9867fcdecee9fa849d2f0ccbb..7fd76d08dd2f9acf37fa7ded82f96ac790734fc6 100644 --- a/src/controller/host_manage.go +++ b/src/controller/host_manage.go @@ -7,6 +7,7 @@ import ( json "github.com/json-iterator/go" "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/common/conf" "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" @@ -15,7 +16,7 @@ import ( // ParsingHostFiles 解析主机文件 func ParsingHostFiles(c *gin.Context) (hostManageList []request.HostManageList, err error) { //获取文件流 - metaData, _, err := c.Request.FormFile("hostFile") + metaData, _, err := c.Request.FormFile("host_file") if err != nil { err = resp.GetFileStreamError.WithError(err) return @@ -72,20 +73,20 @@ func AddHostManage(c *gin.Context) { SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组名称为空")), nil) return } - if len(req.HostManageList) == 0 { - SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量为0")), 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 - } - } + //if len(req.HostManageList) == 0 { + // SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量为0")), 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) @@ -108,20 +109,20 @@ func EditHostManage(c *gin.Context) { } //参数校验 - if len(req.HostManageList) == 0 { - SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量为0")), 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 - } - } + //if len(req.HostManageList) == 0 { + // SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机分组数量为0")), 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) @@ -211,6 +212,10 @@ func StateHostManage(c *gin.Context) { switch req.DetectionType { case 1: + if req.Id == 0 { + SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil) + return + } //列表点击状态检测 err = hostManageSvc.ListStateHostManage(req) if err != nil { @@ -221,6 +226,10 @@ func StateHostManage(c *gin.Context) { return case 2: //解析主机列表 + if req.HostManageList == "" { + SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("主机IP列表为空")), nil) + return + } err = json.Unmarshal([]byte(req.HostManageList), &hostManageList) if err != nil { SendJsonResponse(c, resp.InvalidParam.WithError(err), nil) @@ -303,3 +312,35 @@ func HostIpExceptionList(c *gin.Context) { SendJsonPageResponse(c, err, list, total) } } + +// 导出 +func ExportIp(c *gin.Context) { + detectionType := c.Query("detection_type") + id := c.Query("id") + uuid := c.Query("uuid") + + if cast.ToInt(detectionType) != 0 && cast.ToInt(detectionType) != 1 { + SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("导出类型解析错误")), nil) + return + } + if id == "" { + SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil) + return + } + if uuid == "" { + SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("uuid为空")), nil) + return + } + + hostManageSvc := service.HostManageSvc{} + fileName, err := hostManageSvc.ExportIp(id, uuid, cast.ToInt(detectionType)) + if err != nil { + SendJsonResponse(c, err, nil) + return + } + c.Writer.Header().Add("Content-Type", "application/octet-stream") + c.Writer.Header().Add("Content-Transfer-Encoding", "binary") + c.Writer.Header().Add("Content-Disposition", "filename="+fileName) + + c.File(conf.Options.TempDirPrefix + fileName) +} diff --git a/src/main.go b/src/main.go index a3d09886e11274ba7ac65b3660362f037b417c66..3abb23edfaccb75d04c3c48e294520e985fa203c 100644 --- a/src/main.go +++ b/src/main.go @@ -51,19 +51,20 @@ func initConfig() { Prefix: *prefix, DbURL: util.SetEnvStr("DB_URL", "host=localhost port=9010 user=postgres password=passwd123 dbname=postgres sslmode=disable"), DbDriver: util.SetEnvStr("DB_DRIVER", "postgres"), - RedisURL: util.SetEnvStr("REDIS_URL", "localhost:6379"), + RedisURL: util.SetEnvStr("REDIS_URL", "localhost:7001"), RedisDB: 0, RedisTag: "bg", - LogDirPrefix: util.SetEnvStr("LOG_DIR_PREFIX", "/app/log"), // 日志目录 - LogDirName: util.SetEnvStr("LOG_NAME", "syslog"), // 日志名称 - LogSaveDays: util.SetEnvInt("LOG_SAVE_DAYS", 7), // 日志最大存储天数 - LogMode: util.SetEnvInt("LOG_MODE", 1), // 1.标准打印 2.输出文件 - ArgBool: util.SetEnvBool("ARG_BOOL", false), // 示例参数 - ArgInt: util.SetEnvInt("ARG_INT", 10), // 示例参数 - MinioServer: util.SetEnvStr("MINIO_SERVER", "https://cache.wodcloud.com"), // Minio 服务地址 - MinioAccessKey: util.SetEnvStr("MINIO_ACCESS_KEY", "beagleadmin"), // Minio Access Key - MinioSecretKey: util.SetEnvStr("MINIO_SECRET_KEY", "H76cPmwvH7vJ"), // Minio Secret - MinioBucket: util.SetEnvStr("MINIO_BUCKET", "so-operation"), // Minio Bucket + LogDirPrefix: util.SetEnvStr("LOG_DIR_PREFIX", "/app/log"), // 日志目录 + LogDirName: util.SetEnvStr("LOG_NAME", "syslog"), // 日志名称 + LogSaveDays: util.SetEnvInt("LOG_SAVE_DAYS", 7), // 日志最大存储天数 + LogMode: util.SetEnvInt("LOG_MODE", 1), // 1.标准打印 2.输出文件 + ArgBool: util.SetEnvBool("ARG_BOOL", false), // 示例参数 + ArgInt: util.SetEnvInt("ARG_INT", 10), // 示例参数 + MinioServer: util.SetEnvStr("MINIO_SERVER", "cache.wodcloud.com"), // Minio 服务地址 + MinioAccessKey: util.SetEnvStr("MINIO_ACCESS_KEY", "beagleadmin"), // Minio Access Key + MinioSecretKey: util.SetEnvStr("MINIO_SECRET_KEY", "H76cPmwvH7vJ"), // Minio Secret + MinioBucket: util.SetEnvStr("MINIO_BUCKET", "so-operation"), // Minio Bucket + TempDirPrefix: util.SetEnvStr("TEMP_DIR_PREFIX", "/app/xlsx/"), //模板目录前缀 } } diff --git a/src/router/automatedmaintenrouter.go b/src/router/automatedmaintenrouter.go index 65d00d04d0b0238dce3cbe275871f7f3cf55fb6a..019f0fdff16bd0e2dde0acb2e76a345710a342b1 100644 --- a/src/router/automatedmaintenrouter.go +++ b/src/router/automatedmaintenrouter.go @@ -9,9 +9,9 @@ import ( // InitAutomatedMaintenRouter 初始化自动化运维路由 func InitAutomatedMaintenRouter(e *gin.Engine) { - so := e.Group(fmt.Sprintf("%s/automatedMainten", conf.Options.Prefix)) + so := e.Group(fmt.Sprintf("%s/automated_mainten", conf.Options.Prefix)) //任务管理 - task := so.Group("/taskManage") + task := so.Group("/task_manage") { task.POST("/add", controller.AddTaskManage) // 新增 task.PUT("/edit", controller.EditTaskManage) // 编辑 @@ -21,14 +21,15 @@ func InitAutomatedMaintenRouter(e *gin.Engine) { task.POST("/exec/script", controller.ExecScript) // 立即执行 } //主机管理 - host := so.Group("/hostManage") + host := so.Group("/host_manage") { - host.POST("/add", controller.AddHostManage) // 新增 - host.PUT("/edit", controller.EditHostManage) // 编辑 - host.DELETE("/del", controller.DelHostManage) // 删除 - host.GET("/details", controller.DetailsHostManage) // 详情 - host.GET("/list", controller.ListHostManage) // 列表 - host.POST("/state", controller.StateHostManage) // 状态检测 - host.GET("/ipExceptionList", controller.HostIpExceptionList) // 主机ip异常列表 + host.POST("/add", controller.AddHostManage) // 新增 + host.PUT("/edit", controller.EditHostManage) // 编辑 + host.DELETE("/del", controller.DelHostManage) // 删除 + host.GET("/details", controller.DetailsHostManage) // 详情 + host.GET("/list", controller.ListHostManage) // 列表 + host.POST("/state", controller.StateHostManage) // 状态检测 + host.GET("/ip_exception_list", controller.HostIpExceptionList) // 主机ip异常列表 + host.GET("/export", controller.ExportIp) // 导出ip列表 } } diff --git a/src/router/router.go b/src/router/router.go index c5e80c01f7de651f4d012848101b8d1c77635997..918243fad6e19e180b896f768dd6e08b601708e6 100644 --- a/src/router/router.go +++ b/src/router/router.go @@ -28,7 +28,7 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { base.GET("/example/list", controller.GetExampleList) // 示例获取列表 } - base.POST("addFile", controller.AddFile) //文件上传 + base.POST("add_file", controller.AddFile) //文件上传 // 初始化自动化运维路由 InitAutomatedMaintenRouter(r) diff --git a/src/service/doc_lib.go b/src/service/doc_lib.go index c5382860b5610a892f0042c8ae7c01b586a25c4c..62a8d3dd0882849dfa212d43058de5d7718f4f6c 100644 --- a/src/service/doc_lib.go +++ b/src/service/doc_lib.go @@ -35,7 +35,7 @@ func (d *DocLibSvc) AddFile(c *gin.Context) (fileUrl string, err error) { ext string contentType string ) - uploadFile, err = c.FormFile("uploadFile") + uploadFile, err = c.FormFile("upload_file") if err != nil { err = errors.Wrap(err, "上传的文件不存在") return diff --git a/src/service/host_manage.go b/src/service/host_manage.go index f50cb9f2aba45db37227c814d667978e6cb1f3ef..2f9f00dfb00855aec7fb09cfa64a872b47617eec 100644 --- a/src/service/host_manage.go +++ b/src/service/host_manage.go @@ -4,10 +4,13 @@ import ( "errors" "fmt" "github.com/google/uuid" + "github.com/spf13/cast" + "github.com/tealeg/xlsx" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" + "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "os" "os/exec" @@ -60,8 +63,27 @@ func (h *HostManageSvc) AddHostManage(req request.AddHostManageReq) (err error) return } + //查询临时表中的ip列表 + hostManageLists := make([]request.HostManageList, 0) + finder1 := session.Table("host_manage_list_cache").Where("conn_status = 0 AND id = ?", req.Uuid) + //查询 + err = finder1.OrderBy("ip_group").Find(&hostManageLists) + if err != nil { + err = resp.DbSelectError.WithError(err) + session.Rollback() + return + } + + //删除临时表数据 + _, err = session.Table("host_manage_list_cache").Where("id = ?", req.Uuid).Delete() + if err != nil { + err = resp.DbDeleteError.WithError(err) + session.Rollback() + return + } + //新增主机分组列表 - for _, v := range req.HostManageList { + for _, v := range hostManageLists { port := "22" if v.Port != "" { port = v.Port @@ -114,6 +136,24 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq) (err error return } + //正常ip列表 + hostManageLists := make([]request.HostManageList, 0) + finder1 := session.Table("host_manage_list_cache").Where("conn_status = 0 AND id = ?", req.Uuid) + //查询 + err = finder1.OrderBy("ip_group").Find(&hostManageLists) + if err != nil { + err = resp.DbSelectError.WithError(err) + session.Rollback() + return + } + //删除临时数据 + _, err = session.Table("host_manage_list_cache").Where("id = ?", req.Uuid).Delete() + if err != nil { + err = resp.DbDeleteError.WithError(err) + session.Rollback() + return + } + //删除主机列表数据 _, err = session.Table("host_manage_list").Where("host_group_id = ?", req.Id).Cols("is_delete").Update(&entity.TaskManage{ IsDelete: 1, @@ -125,7 +165,7 @@ func (h *HostManageSvc) EditHostManage(req request.EditHostManageReq) (err error } //新增主机分组列表 - for _, v := range req.HostManageList { + for _, v := range hostManageLists { port := "22" if v.Port != "" { port = v.Port @@ -276,7 +316,7 @@ func (h *HostManageSvc) ListHostManage(req request.ListHostManageReq) (total int //查询任务 total, err = finder.Select("hm.id,hm.host_name,hm.create_user,hm.create_time,(SELECT COUNT(*) FROM "+ "task_manage tm WHERE tm.is_delete = 0 AND tm.host_group_id = hm.ID) AS task_cnt,(SELECT COUNT(*) FROM "+ - "host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 0 AND hml.host_group_id = hm.ID) AS "+ + "host_manage_list hml WHERE hml.is_delete = 0 AND hml.conn_status = 1 AND hml.host_group_id = hm.ID) AS "+ "ip_cnt_err,(SELECT COUNT(*) FROM host_manage_list hml WHERE hml.is_delete = 0 AND hml.host_group_id = hm.ID) AS ip_cnt"). Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListRes) if err != nil { @@ -325,7 +365,6 @@ func (h *HostManageSvc) ListStateHostManage(req request.StateHostManageReq) (err for _, v := range hostManageList { //状态检测 ipConn := StatusDetection(v.Ip) - var state int if ipConn { //连接正常 @@ -382,23 +421,38 @@ func (h *HostManageSvc) SaveStateHostManage(hostManageList []request.HostManageL ipGroup += 1 } + //往hosts文件中写入主机组ip + f, err := os.OpenFile("/etc/ansible/hosts", os.O_APPEND|os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0777) + if err != nil { + err = resp.FileExecError.WithError(err) + return + } + defer f.Close() + for _, v := range hostManageList { + _, err = f.Write([]byte(fmt.Sprintf("%s ansible_ssh_host=%s ansible_ssh_port=%s ansible_ssh_user=\"%s\" ansible_ssh_pass=\"%s\"\n", v.Ip, v.Ip, v.Port, v.UserName, v.Password))) + if err != nil { + err = resp.FileExecError.WithError(err) + return + } + } + //状态检测 id = uuid.New().String() if err != nil { err = resp.FAIL.WithError(err) return } - for k, _ := range HostManageListCaches { + for k, v := range HostManageListCaches { //调用状态检测函数 - cc := true - - if cc { - //正常 + ipConn := StatusDetection(v.Ip) + if ipConn { + //连接正常 HostManageListCaches[k].ConnStatus = 0 } else { - //异常 + //连接异常 HostManageListCaches[k].ConnStatus = 1 } + HostManageListCaches[k].Id = id } @@ -431,7 +485,7 @@ func (h *HostManageSvc) HostIpExceptionList(req request.HostIpExceptionListReq) } // SaveIpExceptionList 保存时状态检测异常列表 -func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq) (total int64, hostManageListCacheRes response.HostManageListCacheRes, err error) { +func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq) (total int64, hostManageListCacheRes []response.HostManageListCache, err error) { db, err := client.GetDbClient() if err != nil { err = resp.DbConnectError.WithError(err) @@ -441,27 +495,27 @@ func (h *HostManageSvc) SaveIpExceptionList(req request.HostIpExceptionListReq) //异常ip列表-分页 finder := db.Table("host_manage_list_cache").Where("conn_status = 1 AND id = ?", req.Uuid) //查询 - total, err = finder.OrderBy("ip_group").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListCacheRes.AbnormalHost) + total, err = finder.OrderBy("ip_group").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&hostManageListCacheRes) if err != nil { err = resp.DbSelectError.WithError(err) return } - //正常ip列表 - finder1 := db.Table("host_manage_list_cache").Where("conn_status = 0 AND id = ?", req.Uuid) - //查询 - err = finder1.OrderBy("ip_group").Find(&hostManageListCacheRes.AormalHost) - if err != nil { - err = resp.DbSelectError.WithError(err) - return - } - - //删除临时数据 - _, err = db.Table("host_manage_list_cache").Where("id = ?", req.Uuid).Delete() - if err != nil { - err = resp.DbDeleteError.WithError(err) - return - } + ////正常ip列表 + //finder1 := db.Table("host_manage_list_cache").Where("conn_status = 0 AND id = ?", req.Uuid) + ////查询 + //err = finder1.OrderBy("ip_group").Find(&hostManageListCacheRes.AormalHost) + //if err != nil { + // err = resp.DbSelectError.WithError(err) + // return + //} + // + ////删除临时数据 + //_, err = db.Table("host_manage_list_cache").Where("id = ?", req.Uuid).Delete() + //if err != nil { + // err = resp.DbDeleteError.WithError(err) + // return + //} return } @@ -483,3 +537,100 @@ func StatusDetection(ip string) (ipConn bool) { return true } + +// ExportIpStr 结果导出 +type ExportIpStr struct { + Ip string `json:"ip" index:"0"` // ip + Port string `json:"port" index:"1"` // 端口 + VoucherType string `json:"voucher_type" index:"2"` // 凭证类型 + UserName string `json:"user_name" index:"3"` // 用户名 + Password string `json:"password" index:"4"` // 密码 + ConnStatus string `json:"conn_status" index:"5" ` // 连接状态 +} + +// ExportIp 结果导出 +func (h *HostManageSvc) ExportIp(id, uuid string, detectionType int) (fileName string, err error) { + var ( + push = make([]ExportIpStr, 0) + hostManageList = make([]entity.HostManageList, 0) + ) + + db, err := client.GetDbClient() + if err != nil { + err = resp.DbConnectError.WithError(err) + return + } + + if detectionType == 1 { + //列表检测导出 + finder := db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", cast.ToInt(id)) + err = finder.Select("").Find(&hostManageList) + if err != nil { + err = resp.DbSelectError.WithError(err) + return + } + } else { + //新增/编辑检测导出 + finder := db.Table("host_manage_list_cache").Where("id = ?", uuid) + err = finder.Select("").Find(&hostManageList) + if err != nil { + err = resp.DbSelectError.WithError(err) + return + } + } + + for _, v := range hostManageList { + voucherType := "" + switch v.VoucherType { + case 0: + voucherType = "密码验证" + case 1: + voucherType = "密钥验证" + } + connStatus := "" + switch v.ConnStatus { + case 0: + connStatus = "正常" + case 1: + connStatus = "异常" + } + staff := ExportIpStr{ + Ip: v.Ip, + Port: v.Port, + VoucherType: voucherType, + UserName: v.UserName, + Password: v.Password, + ConnStatus: connStatus, + } + + push = append(push, staff) + + } + saveFileName, err := generatePushExportXlsx(push) + if err != nil { + err = resp.FileExecError.WithError(err) + return + } + return saveFileName, nil +} + +func generatePushExportXlsx(push []ExportIpStr) (string, error) { + file := xlsx.NewFile() + sheet, _ := file.AddSheet("Sheet1") + + titles := []string{"IP", "端口", "凭证类型", "用户名", "密码", "状态"} + row := sheet.AddRow() + var cell *xlsx.Cell + for _, title := range titles { + cell = row.AddCell() + cell.Value = title + } + for _, v := range push { + row = sheet.AddRow() + row.WriteStruct(&v, -1) + } + + saveFileName := "IP列表导出数据.xlsx" + err := file.Save(conf.Options.TempDirPrefix + saveFileName) + return saveFileName, err +}