From 9d88c7c467f8d93af4aab9ba0b6d6c01c2ffc546 Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期四, 20 八月 2020 20:35:30 +0800
Subject: [PATCH] add get buckets contrl

---
 controllers/cluster.go |  260 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 260 insertions(+), 0 deletions(-)

diff --git a/controllers/cluster.go b/controllers/cluster.go
index 2d32936..3536041 100644
--- a/controllers/cluster.go
+++ b/controllers/cluster.go
@@ -1 +1,261 @@
 package controllers
+
+import (
+	"basic.com/dbapi.git"
+	"fmt"
+	"github.com/gin-gonic/gin"
+	"math/rand"
+	"time"
+	"webserver/extend/code"
+	"webserver/extend/util"
+)
+
+type ClusterController struct {
+
+}
+
+type Cluster struct {
+	ClusterId string `json:"cluster_id"`
+	ClusterName string `json:"cluster_name"`
+	Password string `json:"password"`
+	VirtualIp string `json:"virtual_ip"`
+}
+
+type Node struct {
+	Id string `json:"id"`
+	ClusterId string `json:"cluster_id"`
+	NodeName string `json:"node_name"`
+	NodeId string `json:"node_id"`
+	NodeIp string `json:"node_ip"`
+	CreateTime string `json:"create_time"`
+	IsAlive int
+}
+
+type ClusterVo struct {
+	ClusterInfo Cluster `json:"clusterInfo"`
+	Nodes []Node `json:"nodes"`
+}
+
+type ClusterCreateVo struct {
+	Password string `json:"password"`
+	ClusterName string `json:"clusterName"`
+	ClusterId string `json:"clusterId"`
+	VirtualIp string `json:"virtualIp"`
+}
+
+type ClusterSearchVo struct {
+	Password string `json:"password"`
+	SearchNum string `json:"searchNum"`
+}
+
+type ClusterJoinVo struct {
+	ClusterId string `json:"clusterId"`
+	Password string `json:"password"`
+	NodeIps []string `json:"nodeIps"`
+}
+
+type ClusterDb struct {
+	ClusterId   string `json:"clusterId"`
+	ClusterName string `json:"clusterName"`
+	Nodes []NodeDb     `json:"nodes"`
+}
+type NodeDb struct {
+	Id string `json:"id"`
+	ClusterId string `json:"cluster_id"`
+	NodeName string `json:"node_name"`
+	NodeId string `json:"node_id"`
+	NodeIp string `json:"node_ip"`
+	CreateTime string `json:"create_time"`
+	IsDelete bool `json:"isDelete"`
+}
+
+// @Security ApiKeyAuth
+// @Summary 鍒涘缓闆嗙兢
+// @Description 鍒涘缓闆嗙兢
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Param  clusterArg body controllers.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(c *gin.Context) {
+	var clusterVo ClusterCreateVo
+	err := c.BindJSON(&clusterVo)
+	if err !=nil || clusterVo.Password == "" || clusterVo.ClusterName == "" {
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	b, d := api.Create(clusterVo.ClusterName, clusterVo.Password, clusterVo.VirtualIp)
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鍒涘缓澶辫触")
+	}
+}
+
+// @Security ApiKeyAuth
+// @Summary 鐢熸垚闆嗙兢6浣嶉殢鏈哄瘑鐮�
+// @Description 鐢熸垚闆嗙兢6浣嶉殢鏈哄瘑鐮�
+// @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/randomPwd [get]
+func (cc ClusterController) RandomPwd(c *gin.Context) {
+	pwd := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
+	util.ResponseFormat(c,code.Success,pwd)
+}
+
+// @Security ApiKeyAuth
+// @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(c *gin.Context) {
+	var clusterApi dbapi.ClusterApi
+	b, d := clusterApi.FindCluster()
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"闆嗙兢鏌ヨ澶辫触")
+	}
+}
+
+// @Security ApiKeyAuth
+// @Summary 鎼滅储闆嗙兢
+// @Description 鎼滅储闆嗙兢
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Param searchArg body controllers.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(c *gin.Context) {
+	var csv ClusterSearchVo
+	err := c.BindJSON(&csv)
+	if err !=nil || csv.Password == ""{
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	b,d := api.Search(csv.SearchNum, csv.Password)
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鎼滅储澶辫触")
+	}
+}
+
+// @Security ApiKeyAuth
+// @Summary 閫氳繃searchNum寮傛鑾峰彇闆嗙兢鑺傜偣淇℃伅
+// @Description 閫氳繃searchNum寮傛鑾峰彇闆嗙兢鑺傜偣淇℃伅
+// @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/getNodesByNum [get]
+func (cc ClusterController) GetSearchNodes(c *gin.Context) {
+	var api dbapi.ClusterApi
+	b, d := api.GetSearchNodes()
+	if b {
+		util.ResponseFormat(c,code.Success,d)
+	} else {
+		util.ResponseFormat(c,code.ClusterNodesEmpty,[]interface{}{})
+	}
+}
+
+// @Security ApiKeyAuth
+// @Summary 閫氳繃searchNum鍋滄鎼滅储
+// @Description 閫氳繃searchNum鍋滄鎼滅储
+// @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(c *gin.Context) {
+	var api dbapi.ClusterApi
+	b, d := api.StopSearching("")
+	if b {
+		util.ResponseFormat(c,code.Success, d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鍋滄澶辫触")
+	}
+}
+
+// @Security ApiKeyAuth
+// @Summary 鍔犲叆闆嗙兢
+// @Description 鍔犲叆闆嗙兢
+// @Accept json
+// @Produce json
+// @Tags cluster
+// @Param  clusterArg body controllers.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(c *gin.Context) {
+	var arg ClusterJoinVo
+	err := c.BindJSON(&arg)
+	if err!=nil || arg.ClusterId == "" || arg.Password == "" || len(arg.NodeIps) == 0{
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	var api dbapi.ClusterApi
+	paramBody := util.Struct2Map(arg)
+	b, d := api.JoinCluster(paramBody)
+	if b {
+		util.ResponseFormat(c,code.Success,d)
+	} else {
+		util.ResponseFormat(c,code.ComError,"鍔犲叆澶辫触")
+	}
+}
+
+// @Security ApiKeyAuth
+// @Summary 淇濆瓨闆嗙兢鍚嶇О
+// @Description 淇濆瓨闆嗙兢鍚嶇О
+// @Accept x-www-form-urlencoded
+// @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(c *gin.Context) {
+	clusterName := c.PostForm("clusterName")
+	if clusterName == "" {
+		util.ResponseFormat(c,code.RequestParamError, "鍙傛暟鏈夎")
+		return
+	}
+	virtualIp := c.PostForm("virtualIp")
+	var api dbapi.ClusterApi
+	b,_ := api.UpdateClusterName(clusterName,virtualIp)
+	if b {
+		util.ResponseFormat(c,code.UpdateSuccess,"鏇存柊鎴愬姛")
+	} else {
+		util.ResponseFormat(c,code.ComError, "鏇存柊澶辫触")
+	}
+}
+
+// @Security ApiKeyAuth
+// @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(c *gin.Context) {
+	var api dbapi.ClusterApi
+	if b,_:= api.Leave();b {
+		util.ResponseFormat(c,code.Success,"閫�鍑烘垚鍔�")
+	} else {
+		util.ResponseFormat(c,code.ComError,"閫�鍑哄け璐�")
+	}
+}
\ No newline at end of file

--
Gitblit v1.8.0