356 lines
10 KiB
Go
356 lines
10 KiB
Go
package system
|
||
|
||
import (
|
||
err1 "errors"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/controller"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/middleware"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/node"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/out_giving"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/project_info"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/script_info"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/workspace"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/app_manage/workspace_env"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/model/user"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/errors"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/pkg/request"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/service"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/service/app_manage/assets"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/service/app_manage/base_service"
|
||
"git.inspur.com/sbg-jszt/cfn/cfn-schedule/internal/validator"
|
||
"github.com/gin-gonic/gin"
|
||
"strings"
|
||
)
|
||
|
||
func EditWorkspace(c *gin.Context) {
|
||
w := &workspace.Workspace{}
|
||
err := validator.CheckPostParams(c, w)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
||
return
|
||
}
|
||
if w.Name == "" {
|
||
controller.FailCode(c, errors.InvalidParameter, nil, "名称不能为空!")
|
||
return
|
||
}
|
||
err = checkWorkspaceInfo(w.Id, w.Name)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.InvalidParameter, err, err.Error())
|
||
return
|
||
}
|
||
|
||
if w.Id == "" {
|
||
w.SetUserName(service.GetUserName(c))
|
||
_, err = w.Create(w)
|
||
} else {
|
||
w.SetModifyUserName(service.GetUserName(c))
|
||
_, err = w.Update(w, nil)
|
||
}
|
||
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "数据库错误!")
|
||
return
|
||
}
|
||
controller.Success(c, "")
|
||
}
|
||
|
||
func checkWorkspaceInfo(id, name string) error {
|
||
w := workspace.New()
|
||
fields := make(map[string]interface{}, 1)
|
||
fields["name"] = name
|
||
var notFields map[string]interface{}
|
||
if id != "" {
|
||
notFields = make(map[string]interface{}, 1)
|
||
notFields["id"] = id
|
||
}
|
||
result, _ := w.GetAll(w, fields, notFields)
|
||
if len(result.([]workspace.Workspace)) > 0 {
|
||
return err1.New("工作空间名称已存在")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func ListWorkSpaceGroup(c *gin.Context) {
|
||
w := &workspace.Workspace{}
|
||
result, _ := w.ListGroup()
|
||
controller.Success(c, result)
|
||
}
|
||
|
||
type WorkspacePageBody struct {
|
||
PageInfo *request.PageInfo `json:"pageInfo" binding:"required"`
|
||
Id string `json:"id"`
|
||
Name string `json:"name"`
|
||
Group string `json:"group"`
|
||
}
|
||
|
||
func PageWorkspaces(c *gin.Context) {
|
||
var pageBody WorkspacePageBody
|
||
err := c.ShouldBindJSON(&pageBody)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.InvalidParameter, err, "请检查参数!")
|
||
return
|
||
}
|
||
w := workspace.New()
|
||
fields := map[string]interface{}{}
|
||
if pageBody.Id != "" {
|
||
fields["id"] = pageBody.Id
|
||
}
|
||
if pageBody.Name != "" {
|
||
fields["name"] = pageBody.Name
|
||
}
|
||
if pageBody.Group != "" {
|
||
fields["\"group\""] = pageBody.Group
|
||
}
|
||
page := &model.PageConfig{}
|
||
page.Covert(pageBody.PageInfo)
|
||
result, err := w.Page(w, page, fields)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, err, "内部错误!")
|
||
return
|
||
}
|
||
page.Data = result
|
||
controller.Success(c, page)
|
||
}
|
||
|
||
func ListAllWorkspaces(c *gin.Context) {
|
||
userObject, _ := c.Get(middleware.LoginUserKey)
|
||
userModel := userObject.(*user.UserObj)
|
||
list := base_service.GetUserWorkspaceList(userModel)
|
||
controller.Success(c, list)
|
||
|
||
//w := workspace.New()
|
||
//result, err := w.GetAll(w, nil, nil)
|
||
//if err != nil {
|
||
// controller.FailCode(c, errors.ServerError, err, "内部错误!")
|
||
// return
|
||
//}
|
||
//controller.Success(c, result)
|
||
}
|
||
|
||
// TreeNode represents a node in a tree structure.
|
||
type TreeNode struct {
|
||
Id string `json:"id"`
|
||
ParentId string `json:"parentId"`
|
||
Weight int `json:"weight"`
|
||
Name string `json:"name"`
|
||
WorkspaceBind int `json:"workspaceBind"`
|
||
Count int `json:"count"`
|
||
Children []*TreeNode `json:"children"`
|
||
}
|
||
|
||
// NewTreeNode creates a new TreeNode with given parameters.
|
||
func NewTreeNode(id, parent, name string, weight, workspaceBind, count int) *TreeNode {
|
||
treeNode := &TreeNode{
|
||
Id: id,
|
||
ParentId: parent,
|
||
Weight: weight,
|
||
Name: name,
|
||
WorkspaceBind: workspaceBind,
|
||
Count: count,
|
||
Children: nil,
|
||
}
|
||
|
||
return treeNode
|
||
}
|
||
|
||
// Tree is a placeholder for the Tree structure.
|
||
type Tree struct {
|
||
Name string `json:"name"`
|
||
Id string `json:"id"`
|
||
Children []*TreeNode `json:"children"`
|
||
}
|
||
|
||
// BuildSingle builds a tree from a list of nodes.
|
||
func BuildSingle(nodes []*TreeNode) *Tree {
|
||
// Implementation goes here.
|
||
return &Tree{
|
||
Name: "",
|
||
Id: "",
|
||
Children: nodes,
|
||
}
|
||
}
|
||
|
||
// PreCheckDelete 删除工作空间前检查
|
||
func PreCheckDelete(c *gin.Context) {
|
||
workspaceId := c.Query("id")
|
||
|
||
var nodes []*TreeNode
|
||
|
||
{
|
||
// 节点信息
|
||
cnt, err := node.CountNodeByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "节点信息获取失败!")
|
||
return
|
||
}
|
||
treeNode := NewTreeNode("node", "", "节点信息", 0, 1, int(cnt))
|
||
nodes = append(nodes, treeNode)
|
||
}
|
||
|
||
{
|
||
// 工作空间环境变量
|
||
cnt, err := workspace_env.CountWorkspaceEnvVarByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "工作空间环境变量获取失败!")
|
||
return
|
||
}
|
||
treeNode := NewTreeNode("workspace_env_var", "", "工作空间环境变量", 0, 2, int(cnt))
|
||
nodes = append(nodes, treeNode)
|
||
}
|
||
|
||
{
|
||
// 项目信息
|
||
cnt, err := project_info.CountProjectByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "项目信息获取失败!")
|
||
return
|
||
}
|
||
treeNode := NewTreeNode("project_info", "", "项目信息", 0, 1, int(cnt))
|
||
nodes = append(nodes, treeNode)
|
||
}
|
||
|
||
{
|
||
// 节点脚本模版
|
||
// 节点脚本模版执行记录
|
||
cnt1, err := script_info.CountNodeScriptByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "节点脚本模版获取失败!")
|
||
return
|
||
}
|
||
|
||
cnt2, err := script_info.CountNodeScriptExecuteLogByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "节点脚本模版执行记录获取失败!")
|
||
return
|
||
}
|
||
treeNode := NewTreeNode("node_script_execute_log", "node_script_cache", "节点脚本模版执行记录", 0, 1, int(cnt2))
|
||
newTreeNode := NewTreeNode("node_script_cache", "", "节点脚本模版", 0, 1, int(cnt1))
|
||
newTreeNode.Children = append(newTreeNode.Children, treeNode)
|
||
nodes = append(nodes, newTreeNode)
|
||
}
|
||
|
||
{
|
||
// 节点分发
|
||
// 分发日志
|
||
cnt1, err := out_giving.CountOutGivingModelByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "节点分发获取失败")
|
||
return
|
||
}
|
||
|
||
cnt2, err := out_giving.CountOutGivingLogByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "分发日志获取失败")
|
||
return
|
||
}
|
||
treeNode := NewTreeNode("out_giving_log", "", "分发日志", 0, 3, int(cnt2))
|
||
newTreeNode := NewTreeNode("out_giving_model", "", "节点分发", 0, 1, int(cnt1))
|
||
newTreeNode.Children = append(newTreeNode.Children, treeNode)
|
||
nodes = append(nodes, newTreeNode)
|
||
}
|
||
|
||
{
|
||
// 脚本模版
|
||
cnt, err := script_info.CountScriptByWorkspaceId(workspaceId)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.ServerError, nil, "脚本模版获取失败")
|
||
return
|
||
}
|
||
treeNode := NewTreeNode("script", "", "脚本模版", 0, 1, int(cnt))
|
||
nodes = append(nodes, treeNode)
|
||
}
|
||
|
||
stringTree := BuildSingle(nodes)
|
||
controller.Success(c, stringTree)
|
||
}
|
||
|
||
func DeleteWorkspace(c *gin.Context) {
|
||
workspaceId := c.Param("id")
|
||
if workspaceId == "" {
|
||
controller.FailCode(c, errors.InvalidParameter, nil, "请检查参数:workspaceId!")
|
||
return
|
||
}
|
||
|
||
if workspaceId == assets.DefaultWorkspace {
|
||
controller.Fail(c, errors.InvalidParameter, "默认命名空间不允许删除!", "默认命名空间不允许删除!")
|
||
return
|
||
}
|
||
|
||
{
|
||
// 节点信息
|
||
cnt, _ := node.CountNodeByWorkspaceId(workspaceId)
|
||
if cnt != 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:节点信息")
|
||
return
|
||
}
|
||
|
||
// 项目信息
|
||
cnt, _ = project_info.CountProjectByWorkspaceId(workspaceId)
|
||
if cnt != 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:项目信息")
|
||
return
|
||
}
|
||
|
||
// 节点脚本模版
|
||
cnt, _ = script_info.CountNodeScriptByWorkspaceId(workspaceId)
|
||
if cnt != 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:节点脚本模版")
|
||
return
|
||
}
|
||
|
||
// 节点脚本模版执行记录
|
||
cnt, _ = script_info.CountNodeScriptExecuteLogByWorkspaceId(workspaceId)
|
||
if cnt != 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:节点脚本模版执行记录")
|
||
return
|
||
}
|
||
|
||
// 节点分发
|
||
cnt, _ = out_giving.CountOutGivingModelByWorkspaceId(workspaceId)
|
||
if cnt != 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:节点分发")
|
||
return
|
||
}
|
||
|
||
// 脚本模版
|
||
cnt, _ = script_info.CountScriptByWorkspaceId(workspaceId)
|
||
if cnt != 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:脚本模版")
|
||
return
|
||
}
|
||
}
|
||
|
||
// 判断用户绑定关系
|
||
allUsers, _ := user.NewUserModel().GetAll(nil, nil)
|
||
var users []string
|
||
for _, allUser := range allUsers {
|
||
contains := strings.Contains(allUser.Namespace, workspaceId)
|
||
if contains {
|
||
users = append(users, allUser.Displayname)
|
||
}
|
||
}
|
||
if len(users) > 0 {
|
||
controller.FailCode(c, errors.ServerError, nil, "当前工作空间下还存在关联数据:用户绑定关系,请先解除绑定关系!"+strings.Join(users, ","))
|
||
return
|
||
}
|
||
|
||
// 删除环境变量
|
||
workspace_env.DeleteWorkspaceEnvVarByWorkspaceId(workspaceId)
|
||
// 删除分发日志
|
||
out_giving.DeleteOutGivingLogByWorkspaceId(workspaceId)
|
||
|
||
w := &workspace.Workspace{}
|
||
w.Id = workspaceId
|
||
err := w.GetById(w)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.InvalidParameter, err, "无法查找到对象!")
|
||
return
|
||
}
|
||
_, err = w.Delete(w)
|
||
if err != nil {
|
||
controller.FailCode(c, errors.InvalidParameter, err, "删除失败!")
|
||
return
|
||
}
|
||
controller.Success(c, "")
|
||
}
|