cluster.go 4.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4
package cluster

import (
	"net/http"
Y
Your Name 已提交
5 6
	"regexp"

Y
Your Name 已提交
7 8
	goku_handler "github.com/eolinker/goku-api-gateway/goku-handler"

Y
Your Name 已提交
9
	"github.com/pkg/errors"
E
eoLinker API Management 已提交
10

黄孟柱 已提交
11
	"github.com/eolinker/goku-api-gateway/console/controller"
Y
Your Name 已提交
12
	"github.com/eolinker/goku-api-gateway/console/module/cluster"
E
eoLinker API Management 已提交
13 14
)

Y
Your Name 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
const operationCluster = "nodeManagement"

//Handlers handlers
type Handlers struct {
}

//Handlers handlers
func (h *Handlers) Handlers(factory *goku_handler.AccountHandlerFactory) map[string]http.Handler {
	return map[string]http.Handler{
		"/add":        factory.NewAccountHandleFunction(operationCluster, true, AddCluster),
		"/edit":       factory.NewAccountHandleFunction(operationCluster, true, EditCluster),
		"/delete":     factory.NewAccountHandleFunction(operationCluster, true, DeleteCluster),
		"/list":       factory.NewAccountHandleFunction(operationCluster, false, GetClusterInfoList),
		"/simpleList": factory.NewAccountHandleFunction(operationCluster, false, GetClusterList),
	}
}

//NewHandlers new handlers
func NewHandlers() *Handlers {
	return &Handlers{}
}

Y
Your Name 已提交
37
//GetClusterList 获取集群列表
E
eoLinker API Management 已提交
38
func GetClusterList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
39

Y
Your Name 已提交
40
	list, _ := cluster.GetClusters()
E
eoLinker API Management 已提交
41 42 43 44 45

	controller.WriteResultInfo(httpResponse,
		"cluster",
		"clusters",
		list)
Y
Your Name 已提交
46 47 48 49
}

//GetCluster 获取集群信息
func GetCluster(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
50

Y
Your Name 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	httpRequest.ParseForm()
	name := httpRequest.Form.Get("name")

	result, err := cluster.GetCluster(name)
	if err != nil {
		controller.WriteError(httpResponse, "370000", "cluster", "[ERROR]The cluster does not exist", err)
		return
	}

	controller.WriteResultInfo(httpResponse,
		"cluster",
		"clusterInfo",
		result)
}

//AddCluster 新增集群
func AddCluster(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
68

Y
Your Name 已提交
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
	httpRequest.ParseForm()
	name := httpRequest.Form.Get("name")
	title := httpRequest.Form.Get("title")
	note := httpRequest.Form.Get("note")
	match, err := regexp.MatchString(`^[a-zA-Z][a-zA-z0-9_]*$`, name)
	if err != nil || !match {
		controller.WriteError(httpResponse, "370001", "cluster", "[ERROR]Illegal cluster name", err)
		return
	}
	if cluster.CheckClusterNameIsExist(name) {
		controller.WriteError(httpResponse, "370003", "cluster", "[ERROR]Cluster name already exists", errors.New("[error]cluster name already exists"))
		return
	}
	err = cluster.AddCluster(name, title, note)
	if err != nil {
		controller.WriteError(httpResponse, "370000", "cluster", err.Error(), err)
		return
	}

	controller.WriteResultInfo(httpResponse,
		"cluster",
		"",
		nil)
}

//EditCluster 新增集群
func EditCluster(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
96

Y
Your Name 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	httpRequest.ParseForm()
	name := httpRequest.Form.Get("name")
	title := httpRequest.Form.Get("title")
	note := httpRequest.Form.Get("note")
	match, err := regexp.MatchString(`^[a-zA-Z][a-zA-z0-9_]*$`, name)
	if err != nil || !match {
		controller.WriteError(httpResponse, "370001", "cluster", "[ERROR]Illegal cluster name", err)
		return
	}
	err = cluster.EditCluster(name, title, note)
	if err != nil {
		controller.WriteError(httpResponse, "370000", "cluster", err.Error(), err)
		return
	}

	controller.WriteResultInfo(httpResponse,
		"cluster",
		"",
		nil)
}
E
eoLinker API Management 已提交
117

Y
Your Name 已提交
118 119
//DeleteCluster 新增集群
func DeleteCluster(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
120

Y
Your Name 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	httpRequest.ParseForm()
	name := httpRequest.Form.Get("name")
	match, err := regexp.MatchString(`^[a-zA-Z][a-zA-z0-9_]*$`, name)
	if err != nil || !match {
		controller.WriteError(httpResponse, "370001", "cluster", "[ERROR]Illegal cluster name", err)
		return
	}
	nodeCount := cluster.GetClusterNodeCount(name)
	if nodeCount > 0 {
		controller.WriteError(httpResponse, "370002", "cluster", "[ERROR]There are nodes in the cluster", errors.New("[error]there are nodes in the cluster"))
		return
	}
	err = cluster.DeleteCluster(name)
	if err != nil {
		controller.WriteError(httpResponse, "370000", "cluster", err.Error(), err)
		return
	}

	controller.WriteResultInfo(httpResponse,
		"cluster",
		"",
		nil)
E
eoLinker API Management 已提交
143 144
}

Y
Your Name 已提交
145
//GetClusterInfoList 获取集群信息列表
E
eoLinker API Management 已提交
146
func GetClusterInfoList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
147

Y
Your Name 已提交
148
	result, _ := cluster.GetClusters()
E
eoLinker API Management 已提交
149 150 151 152

	controller.WriteResultInfo(httpResponse,
		"cluster",
		"clusters",
Y
Your Name 已提交
153
		result)
E
eoLinker API Management 已提交
154
}