zhangzengfei
2023-09-04 e8e536d1cb52d2126c8c7ce2ba1c7a76f7208678
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package controllers
 
import (
    "vamicro/system-service/models"
    "vamicro/system-service/service"
    "vamicro/system-service/vo"
 
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/logger.git"
    uuid "github.com/satori/go.uuid"
)
 
type ClusterController struct {
}
 
// @Summary 查询本地集群
// @Description 查询本地集群
// @Produce json
// @Tags cluster
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/findCluster [get]
func (cc ClusterController) FindCluster(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var clusterE models.Cluster
    arr, err := clusterE.FindAll()
    if err == nil {
        if arr != nil && len(arr) > 0 {
            var nodeE models.Node
            nodes, _ := nodeE.FindNodesByClusterId(arr[0].ClusterId)
            logger.Debugf("FindCluster nodes=%v", nodes)
            return &bhomeclient.Reply{Success: true, Data: map[string]interface{}{
                "clusterId":   arr[0].ClusterId,
                "clusterName": arr[0].ClusterName,
                "password":    arr[0].Password,
                "nodes":       nodes,
                "virtualIp":   arr[0].VirtualIp,
            }}
        } else {
            return &bhomeclient.Reply{Success: true}
        }
 
    } else {
        return &bhomeclient.Reply{Success: false, Msg: "集群查询失败"}
    }
}
 
// @Summary 创建集群
// @Description 创建集群
// @Accept json
// @Produce json
// @Tags cluster
// @Param  clusterArg body vo.ClusterCreateVo true "集群创建参数"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/create [post]
func (cc ClusterController) Create(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var clusterVo vo.ClusterCreateVo
    err := c.BindJSON(&clusterVo)
    if err != nil || len(clusterVo.Password) != 6 || clusterVo.ClusterName == "" {
        return &bhomeclient.Reply{Success: false, Msg: "参数有误"}
    }
 
    sv := service.NewClusterService(h.Bk)
    b, newCluterId := sv.Create(clusterVo.ClusterName, clusterVo.Password, clusterVo.VirtualIp)
    if b {
        clusterVo.ClusterId = newCluterId
        return &bhomeclient.Reply{Success: true, Data: clusterVo}
    } else {
        return &bhomeclient.Reply{Success: false, Msg: "创建集群失败"}
    }
}
 
// @Summary 搜索集群
// @Description 搜索集群
// @Accept json
// @Produce json
// @Tags cluster
// @Param searchArg body vo.ClusterSearchVo true "集群搜索参数"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/search [post]
func (cc ClusterController) Search(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var csv vo.ClusterSearchVo
    err := c.BindJSON(&csv)
    if err != nil || len(csv.Password) != 6 {
        return &bhomeclient.Reply{Success: false, Msg: "参数有误"}
    }
    searchNum := uuid.NewV4().String()
    sv := service.NewClusterService(h.Bk)
    err = sv.SearchByPwd(csv.Password)
    if err != nil {
        return &bhomeclient.Reply{Success: true, Msg: "搜索中,请稍候..."}
    } else {
        return &bhomeclient.Reply{Success: true, Data: searchNum}
    }
}
 
// @Summary 调search后,通过此接口获取查到的集群节点信息
// @Description 调search后,通过此接口获取查到的集群节点信息
// @Accept json
// @Produce json
// @Tags cluster
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/getSearchNodes [get]
func (cc ClusterController) GetSearchNodes(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    sv := service.NewClusterService(h.Bk)
    nodes := sv.SearchNodes()
    logger.Debugf("GetSearchNodes nodes=%v, len=%v", nodes, len(nodes))
    if nodes != nil && len(nodes) > 0 {
        var nodeArr []interface{}
        for _, n := range nodes {
            nodeArr = append(nodeArr, n)
        }
        return &bhomeclient.Reply{Success: true, Msg: "查询成功", Data: nodeArr}
    } else {
        return &bhomeclient.Reply{Success: true, Msg: "查询成功", Data: []interface{}{}}
    }
}
 
// @Summary 停止搜索集群
// @Description 停止搜索集群
// @Produce json
// @Tags cluster
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/stopSearching [post]
func (cc ClusterController) StopSearching(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    sv := service.NewClusterService(h.Bk)
    if sv.StopSearching() {
        return &bhomeclient.Reply{Success: true}
    } else {
        return &bhomeclient.Reply{Success: false, Msg: "停止失败"}
    }
}
 
// @Summary 加入集群
// @Description 加入集群
// @Accept json
// @Produce json
// @Tags cluster
// @Param  clusterArg body vo.ClusterJoinVo true "集群创建参数"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/joinCluster [post]
func (cc ClusterController) JoinCluster(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var arg vo.ClusterJoinVo
    err := c.BindJSON(&arg)
    logger.Debugf("JoinCluster reqParam=%v, err=%v", arg, err)
    if err != nil {
        return &bhomeclient.Reply{Success: false, Msg: "参数有误"}
    } else if arg.ClusterId == "" {
        return &bhomeclient.Reply{Success: false, Msg: "集群id为空错误"}
    } else if len(arg.Password) != 6 {
        return &bhomeclient.Reply{Success: false, Msg: "集群密码错误,密码必须为6位"}
    } else if len(arg.NodeIps) == 0 {
        return &bhomeclient.Reply{Success: false, Msg: "集群节点参数为空错误"}
    }
 
    sv := service.NewClusterService(h.Bk)
    if b, err := sv.JoinCluster(&arg); b {
        return &bhomeclient.Reply{Success: true, Msg: "加入成功"}
    } else {
        return &bhomeclient.Reply{Success: false, Msg: err.Error()}
    }
}
 
// @Summary 保存集群名称
// @Description 保存集群名称
// @Produce json
// @Tags cluster
// @Param  clusterName formData string true "集群名称"
// @Param virtualIp formData string false "虚拟ip"
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/updateClusterName [post]
func (cc ClusterController) UpdateClusterName(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    clusterName := c.PostForm("clusterName")
    if clusterName == "" {
        return &bhomeclient.Reply{Success: false, Msg: "参数有误"}
    }
    virtualIp := c.PostForm("virtualIp")
    sv := service.NewClusterService(h.Bk)
    b := sv.UpdateClusterName(clusterName, virtualIp)
    if b {
        return &bhomeclient.Reply{Success: true, Msg: "更新成功"}
    } else {
        return &bhomeclient.Reply{Success: false, Msg: "更新失败"}
    }
}
 
type LeaveArg struct {
    IsDel bool `json:"isDel"` //是否删除数据
}
 
// @Summary 退出集群
// @Description 退出集群
// @Produce json
// @Tags cluster
// @Success 200 {string} json "{"code":200, success:true, msg:"", data:""}"
// @Failure 500 {string} json "{"code":500, success:false, msg:"",data:""}"
// @Router /data/api-v/cluster/leave [post]
func (cc ClusterController) Leave(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var reqBody LeaveArg
    c.BindJSON(&reqBody)
    sv := service.NewClusterService(h.Bk)
    if b, err := sv.Leave(reqBody.IsDel); b {
        return &bhomeclient.Reply{Success: true, Msg: "退出成功"}
    } else {
        return &bhomeclient.Reply{Success: false, Msg: err.Error()}
    }
}
 
func (cc ClusterController) TestSyncSql(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    sv := service.NewClusterService(h.Bk)
    if sv.TestSyncSql() {
        return &bhomeclient.Reply{Success: true, Msg: "测试成功"}
    } else {
        return &bhomeclient.Reply{Success: false, Msg: "测试失败"}
    }
}
 
type IpsArgs struct {
    NodeId string `json:"node_id"` //ip列表
}
 
func (cc ClusterController) FindIpByNode(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
    var reqBody IpsArgs
    c.BindJSON(&reqBody)
    if reqBody.NodeId == "" {
        return &bhomeclient.Reply{Success: false, Msg: "NodeId不能为空"}
    }
    sv := service.NewClusterService(h.Bk)
    ip, err := sv.FindIpByNode(reqBody.NodeId)
    if err != nil {
        return &bhomeclient.Reply{Success: false, Msg: "ip查询失败"}
    }
    return &bhomeclient.Reply{Success: true, Msg: "IP查询成功", Data: ip}
}