node.go 8.9 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5
package node

import (
	"encoding/json"
	"errors"
Y
Your Name 已提交
6
	"github.com/eolinker/goku-api-gateway/common/auto-form"
E
eoLinker API Management 已提交
7

Y
Your Name 已提交
8 9
	"github.com/eolinker/goku-api-gateway/console/module/cluster"

黄孟柱 已提交
10
	log "github.com/eolinker/goku-api-gateway/goku-log"
E
eoLinker API Management 已提交
11 12 13 14

	"net/http"
	"strconv"

黄孟柱 已提交
15 16 17
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/node"
	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
18 19
)

Y
Your Name 已提交
20
//AddNode 新增节点信息
E
eoLinker API Management 已提交
21 22 23 24 25 26 27 28 29
func AddNode(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationEDIT)
	if e != nil {
		return
	}

	cluserName := httpRequest.PostFormValue("cluster")

Y
Your Name 已提交
30 31
	clusterID := cluster.GetClusterIDByName(cluserName)
	if clusterID == 0 {
E
eoLinker API Management 已提交
32 33 34 35 36
		controller.WriteError(httpResponse, "340003", "", "[ERROR]Illegal cluster!", nil)
		return
	}

	//nodeNumber := rsa.CertConf["nodeNumber"].(int)
Y
Your Name 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
	type NodeParam struct {
		NodeName string `opt:"nodeName,require"`
		ListenAddress string `opt:"listenAddress,require"`
		AdminAddress string `opt:"adminAddress,require"`
		GroupID int `opt:"groupID,require"`
		Path string `opt:"gatewayPath"`
	}
	//
	//nodeName := httpRequest.PostFormValue("nodeName")
	//listenAddress := httpRequest.PostFormValue("listenAddress")
	//adminAddress := httpRequest.PostFormValue("adminAddress")
	//groupID := httpRequest.PostFormValue("groupID")
	//gatewayPath := httpRequest.PostFormValue("gatewayPath")
	//
	//gID, err := strconv.Atoi(groupID)
	//if err != nil && groupID != "" {
	//	controller.WriteError(httpResponse, "230015", "", "[ERROR]Illegal groupID!", err)
	//	return
	//}
	param:=new(NodeParam)
	err:=auto.SetValues(httpRequest.Form,param)
	if err!= nil{
		controller.WriteError(httpResponse, "230015", "", "[ERROR]", err)
E
eoLinker API Management 已提交
60 61
		return
	}
Y
Your Name 已提交
62
	if !utils.ValidateRemoteAddr(param.ListenAddress) {
E
eoLinker API Management 已提交
63 64
		controller.WriteError(httpResponse,
			"230006",
Y
Your Name 已提交
65 66
			"node", "[ERROR]Illegal listenAddress!",
			errors.New("illegal listenAddress"))
E
eoLinker API Management 已提交
67 68
		return
	}
Y
Your Name 已提交
69 70 71 72 73 74 75 76
	if !utils.ValidateRemoteAddr(param.AdminAddress) {
		controller.WriteError(httpResponse,
			"230007",
			"node", "[ERROR]Illegal listenAddress!",
			errors.New("illegal listenAddress"))
		return
	}
	if param.GroupID != 0 {
E
eoLinker API Management 已提交
77
		// 检查分组是否存在
Y
Your Name 已提交
78
		flag, err := node.CheckNodeGroupIsExist(param.GroupID)
E
eoLinker API Management 已提交
79 80 81 82 83 84 85 86 87 88 89 90

		if !flag {
			controller.WriteError(
				httpResponse,
				"230014",
				"node",
				"[ERROR]The node group does not exist!",
				err)
			return
		}
	}

Y
Your Name 已提交
91
	flag, result, err := node.AddNode(clusterID, param.NodeName, param.ListenAddress, param.AdminAddress, param.Path, param.GroupID)
E
eoLinker API Management 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

	if !flag {
		controller.WriteError(httpResponse,
			"330000",
			"node",
			result["error"].(string),
			err)
		return
	}

	res := map[string]interface{}{
		"nodeID":     result["nodeID"],
		"version":    result["version"],
		"statusCode": "000000",
		"type":       "node",
		"resultDesc": "",
	}
	data, _ := json.Marshal(res)

Y
Your Name 已提交
111
	_,_=httpResponse.Write(data)
E
eoLinker API Management 已提交
112 113
}

Y
Your Name 已提交
114
//EditNode 修改节点信息
E
eoLinker API Management 已提交
115 116 117 118 119 120 121 122
func EditNode(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationEDIT)
	if e != nil {
		return
	}

	nodeName := httpRequest.PostFormValue("nodeName")
Y
Your Name 已提交
123 124
	listenAddress := httpRequest.PostFormValue("listenAddress")
	adminAddress := httpRequest.PostFormValue("adminAddress")
E
eoLinker API Management 已提交
125 126 127 128 129 130
	groupID := httpRequest.PostFormValue("groupID")
	nodeID := httpRequest.PostFormValue("nodeID")

	gatewayPath := httpRequest.PostFormValue("gatewayPath")
	// key := httpRequest.PostFormValue("key")

Y
Your Name 已提交
131 132 133 134 135 136 137 138 139 140 141 142
	if !utils.ValidateRemoteAddr(listenAddress) {
		controller.WriteError(httpResponse,
			"230006",
			"node", "[ERROR]Illegal listenAddress!",
			errors.New("illegal listenAddress"))
		return
	}
	if !utils.ValidateRemoteAddr(adminAddress) {
		controller.WriteError(httpResponse,
			"230007",
			"node", "[ERROR]Illegal listenAddress!",
			errors.New("illegal listenAddress"))
E
eoLinker API Management 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		return
	}

	id, err := strconv.Atoi(nodeID)
	if err != nil {

		controller.WriteError(httpResponse, "230001", "node", "[ERROR]Illegal nodeID!", err)
		return
	}
	gID, err := strconv.Atoi(groupID)
	if err != nil && groupID != "" {
		controller.WriteError(httpResponse, "230015", "node", "[ERROR]Illegal groupID!", err)
		return
	}

	if gID != 0 {
		// 检查分组是否存在
Y
Your Name 已提交
160
		flag, err := node.CheckNodeGroupIsExist(gID)
E
eoLinker API Management 已提交
161 162 163 164 165 166 167 168 169 170 171 172

		if !flag {
			controller.WriteError(
				httpResponse,
				"230014",
				"node",
				"[ERROR]The node group does not exist!",
				err)
			return
		}
	}

Y
Your Name 已提交
173 174 175 176 177 178 179
	//exits := node.CheckIsExistRemoteAddr(id, listenAddress, adminAddress)
	//if exits {
	//
	//	controller.WriteError(httpResponse, "230005", "node", "[ERROR]The remote address is existed!", nil)
	//	return
	//
	//}
E
eoLinker API Management 已提交
180

Y
Your Name 已提交
181
	flag, result, _ := node.EditNode(nodeName, listenAddress, adminAddress, gatewayPath, id, gID)
E
eoLinker API Management 已提交
182 183 184 185 186 187 188 189 190 191

	if !flag {
		controller.WriteError(httpResponse, "330000", "node", result, nil)
		return
	}

	controller.WriteResultInfo(httpResponse, "node", "", nil)
	return
}

Y
Your Name 已提交
192
//DeleteNode 删除节点信息
E
eoLinker API Management 已提交
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 240 241 242 243 244 245 246 247
func DeleteNode(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationEDIT)
	if e != nil {

		return
	}

	nodeID := httpRequest.PostFormValue("nodeID")

	id, err := strconv.Atoi(nodeID)
	if err != nil {
		controller.WriteError(httpResponse,
			"230001",
			"node",
			"[ERROR]Illegal nodeID!",
			err)
		return

	}
	flag, result, err := node.DeleteNode(id)
	if !flag {

		controller.WriteError(httpResponse,
			"330000",
			"node",
			result,
			err)
		return

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

// GetNodeList 获取节点列表
func GetNodeList(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationREAD)
	if e != nil {
		return
	}
	httpRequest.ParseForm()
	cluserName := httpRequest.Form.Get("cluster")
	groupID := httpRequest.Form.Get("groupID")
	keyword := httpRequest.Form.Get("keyword")

	gID, err := strconv.Atoi(groupID)
	if err != nil && groupID != "" {
		if groupID != "" {
			controller.WriteError(httpResponse, "330002", "node", "[ERROR]Illegal groupID!", nil)
			return
		}
		gID = -1
	}

Y
Your Name 已提交
248 249
	clusterID := cluster.GetClusterIDByName(cluserName)
	if clusterID == 0 {
E
eoLinker API Management 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
		controller.WriteError(httpResponse, "330003", "node", "[ERROR]The cluster dosen't exist!", nil)
		return
	}

	flag, result, err := node.GetNodeList(clusterID, gID, keyword)
	if !flag {
		controller.WriteError(httpResponse,
			"330000",
			"node",
			"[ERROR]Empty node list!",
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "node", "nodeList", result)
	// controller.WriteResultInfo(httpResponse, "nodeList", result)

}

Y
Your Name 已提交
269
//GetNodeInfo 获取节点信息
E
eoLinker API Management 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
func GetNodeInfo(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationREAD)
	if e != nil {
		return
	}

	nodeID := httpRequest.PostFormValue("nodeID")

	id, err := strconv.Atoi(nodeID)
	if err != nil {

		log.Info("[ERROR] ", httpRequest.RequestURI, ":", err.Error())
		controller.WriteError(httpResponse,
			"230001",
			"node",
			"[ERROR]Illegal nodeID!",
			err)
		return
	}
Y
Your Name 已提交
289 290
	  result, err := node.GetNodeInfo(id)
	if err!= nil {
E
eoLinker API Management 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

		controller.WriteError(httpResponse,
			"330000",
			"node",
			"[ERROR]The node does not exist!",
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "node", "nodeInfo", result)

	return
}



Y
Your Name 已提交
307
//BatchEditNodeGroup 批量修改节点分组
E
eoLinker API Management 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
func BatchEditNodeGroup(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationEDIT)
	if e != nil {
		return
	}

	nodeIDList := httpRequest.PostFormValue("nodeIDList")
	groupID := httpRequest.PostFormValue("groupID")

	gID, err := strconv.Atoi(groupID)
	if err != nil {
		controller.WriteError(httpResponse,
			"230015",
			"node",
			"[ERROR]Illegal groupID!",
			err)
		return

	}
	flag, result, err := node.BatchEditNodeGroup(nodeIDList, gID)
	if !flag {

		controller.WriteError(httpResponse,
			"330000",
			"node",
			result,
			err)
		return

	}

	controller.WriteResultInfo(httpResponse, "node", "", nil)

	return
}

Y
Your Name 已提交
345
//BatchDeleteNode 批量删除节点
E
eoLinker API Management 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
func BatchDeleteNode(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationNode, controller.OperationEDIT)
	if e != nil {

		return
	}

	nodeIDList := httpRequest.PostFormValue("nodeIDList")

	flag, result, err := node.BatchDeleteNode(nodeIDList)
	if !flag {

		if result == "230013" {
			controller.WriteError(httpResponse,
				"230013",
				"node",
				"[ERROR]Can not find the avaliable node",
				err)
			return

		}
		controller.WriteError(httpResponse,
			"330000",
			"node",
			result,
			err)
		return

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

	return
}