plugin.go 12.5 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6
package plugin

import (
	"net/http"
	"strconv"

黄孟柱 已提交
7 8 9 10 11
	"github.com/eolinker/goku-api-gateway/console/controller"
	"github.com/eolinker/goku-api-gateway/console/module/node"
	"github.com/eolinker/goku-api-gateway/console/module/plugin"
	plugin_config "github.com/eolinker/goku-api-gateway/console/module/plugin/plugin-config"
	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
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
)

// GetPluginList 获取插件列表
func GetPluginList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationREAD)
	if e != nil {
		return
	}
	httpRequest.ParseForm()
	keyword := httpRequest.Form.Get("keyword")
	condition := httpRequest.Form.Get("condition")
	op, err := strconv.Atoi(condition)
	if err != nil && condition != "" {
		controller.WriteError(httpResponse, "210006", "plugin", "[ERROR]Illegal condition!", err)
		return
	}
	flag, result, err := plugin.GetPluginList(keyword, op)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			"[ERROR]Empty plugin list!",
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "plugin", "pluginList", result)
	return
}

Y
Your Name 已提交
43
//AddPlugin 新增插件信息
E
eoLinker API Management 已提交
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
func AddPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginPriority := httpRequest.PostFormValue("pluginPriority")
	pluginName := httpRequest.PostFormValue("pluginName")
	pluginConfig := httpRequest.PostFormValue("pluginConfig")
	pluginDesc := httpRequest.PostFormValue("pluginDesc")
	isStop := httpRequest.PostFormValue("isStop")
	pluginType := httpRequest.PostFormValue("pluginType")
	version := httpRequest.PostFormValue("version")
	index, err := strconv.Atoi(pluginPriority)

	if err != nil {
		controller.WriteError(httpResponse, "210001", "plugin", "[ERROR]Illegal pluginPriority!", err)
		return
	}

	flag, err := plugin_config.CheckConfig(pluginName, []byte(pluginConfig))
	if !flag {
		controller.WriteError(httpResponse, "500000", "plugin", "[ERROR]插件配置无效:"+err.Error(), err)
		return
	}

	pStop, err := strconv.Atoi(isStop)
	if err != nil {

		controller.WriteError(httpResponse, "210005", "plugin", "[ERROR]Illegal isStop!", err)
		return
	}

	pType, err := strconv.Atoi(pluginType)
	if err != nil {

		controller.WriteError(httpResponse, "210009", "plugin", "[ERROR]Illegal pluginType!", err)
		return
	}

	exits, err := plugin.CheckIndexIsExist("", index)
	if exits {

		controller.WriteError(httpResponse, "210003", "plugin", "[ERROR]Plugin pluginPriority is existed!", err)
		return
	}

	exits, err = plugin.CheckNameIsExist(pluginName)
	if exits {

		controller.WriteError(httpResponse, "210004", "plugin", "[ERROR]Plugin name is existed!", err)
		return
	}

	flag, result, err := plugin.AddPlugin(pluginName, pluginConfig, pluginDesc, version, index, pStop, pType)
	if !flag {
		controller.WriteError(httpResponse, "210000", "plugin", result, err)
		return
	}

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

	return
}

Y
Your Name 已提交
109
//EditPlugin 修改插件信息
E
eoLinker API Management 已提交
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
func EditPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginPriority := httpRequest.PostFormValue("pluginPriority")
	pluginName := httpRequest.PostFormValue("pluginName")
	pluginConfig := httpRequest.PostFormValue("pluginConfig")

	pluginDesc := httpRequest.PostFormValue("pluginDesc")
	isStop := httpRequest.PostFormValue("isStop")
	pluginType := httpRequest.PostFormValue("pluginType")
	version := httpRequest.PostFormValue("version")

	index, err := strconv.Atoi(pluginPriority)
	if err != nil {
		controller.WriteError(httpResponse, "210001", "plugin", "[ERROR]Illegal pluginPriority!", err)
		return
	}
	pStop, err := strconv.Atoi(isStop)
	if err != nil {
		controller.WriteError(httpResponse, "210005", "plugin", "[ERROR]Illegal isStop!", err)
		return
	}
	pType, err := strconv.Atoi(pluginType)
	if err != nil {
		controller.WriteError(httpResponse, "210009", "plugin", "[ERROR]Illegal pluginType!", err)
		return
	}

	flag, err := plugin_config.CheckConfig(pluginName, []byte(pluginConfig))
	if !flag {
		controller.WriteError(httpResponse, "500000", "plugin", "[ERROR]插件配置无效:"+err.Error(), err)
		return
	}
	exits, err := plugin.CheckIndexIsExist(pluginName, index)
	if exits {
		controller.WriteError(httpResponse, "210003", "plugin", "[ERROR]Plugin priority is existed!", err)
		return
	}
	flag, err = plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse, "210004", "plugin", "[ERROR]Plugin name does not exist!", err)
		return
	}
	flag, result, err := plugin.EditPlugin(pluginName, pluginConfig, pluginDesc, version, index, pStop, pType)
	if !flag {
		controller.WriteError(httpResponse, "210000", "plugin", result, err)
		return
	}

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

Y
Your Name 已提交
165
//DeletePlugin 删除插件信息
E
eoLinker API Management 已提交
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
func DeletePlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")
	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"210004",
			"plugin",
			"[ERROR]Plugin name does not exist!",
			err)
		return

	}
	flag, result, err := plugin.DeletePlugin(pluginName)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			result,
			err)
		return

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

Y
Your Name 已提交
198
//GetPluginInfo 获取插件信息
E
eoLinker API Management 已提交
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
func GetPluginInfo(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationREAD)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")

	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"210004",
			"plugin",
			"[ERROR]Plugin name does not exist!",
			err)
		return

	}
	flag, result, err := plugin.GetPluginInfo(pluginName)
	if !flag {

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

	}
	controller.WriteResultInfo(httpResponse, "plugin", "pluginInfo", result)
}

Y
Your Name 已提交
231
//GetPluginConfig 获取插件配置
E
eoLinker API Management 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
func GetPluginConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationREAD)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")
	flag, result, err := plugin.GetPluginConfig(pluginName)
	if !flag {

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

	}
	controller.WriteResultInfo(httpResponse, "plugin", "pluginConfig", result)

	return
}

Y
Your Name 已提交
255
//CheckIndexIsExist 判断插件优先级是否存在
E
eoLinker API Management 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
func CheckIndexIsExist(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginPriority := httpRequest.PostFormValue("pluginPriority")

	index, err := strconv.Atoi(pluginPriority)
	if err != nil {
		controller.WriteError(httpResponse,
			"210001",
			"plugin",
			"[ERROR]Illegal pluginPriority!",
			err)
		return

	}
	flag, err := plugin.CheckIndexIsExist("", index)
	if !flag {

		controller.WriteError(httpResponse,
			"210011",
			"plugin",
			"[ERROR]Plugin pluginPriority does not exist!",
			err)
		return

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

Y
Your Name 已提交
289
//CheckNameIsExist 检查插件名称是否存在
E
eoLinker API Management 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
func CheckNameIsExist(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")

	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			"[ERROR]Plugin name does not exist!",
			err)
		return
	}
	controller.WriteResultInfo(httpResponse, "plugin", "", nil)

}

Y
Your Name 已提交
312
//StartPlugin 开启插件
E
eoLinker API Management 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
func StartPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")

	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"210010",
			"plugin",
			"[ERROR]Plugin name does not exist!",
			err)
		return

	}
	_, _ = plugin.EditPluginStatus(pluginName, 1)
	controller.WriteResultInfo(httpResponse, "plugin", "", nil)
}

Y
Your Name 已提交
335
//StopPlugin 关闭插件
E
eoLinker API Management 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
func StopPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")

	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"210010",
			"plugin",
			"[ERROR]Plugin name does not exist!",
			err)
		return
	}
	_, _ = plugin.EditPluginStatus(pluginName, 0)
	controller.WriteResultInfo(httpResponse, "plugin", "", nil)
}

Y
Your Name 已提交
357
//GetPluginListByPluginType 获取不同类型的插件列表
E
eoLinker API Management 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
func GetPluginListByPluginType(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationREAD)
	if e != nil {
		return
	}

	pluginType := httpRequest.PostFormValue("pluginType")

	pt, err := strconv.Atoi(pluginType)
	if err != nil {
		controller.WriteError(httpResponse,
			"210009",
			"plugin",
			"[ERROR]Illegal pluginType",
			err)
		return

	}
	flag, result, err := plugin.GetPluginListByPluginType(pt)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			"[ERROR]Empty plugin list",
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "plugin", "pluginList", result)
}

Y
Your Name 已提交
390
//BatchStopPlugin 批量关闭插件
E
eoLinker API Management 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
func BatchStopPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginNameList := httpRequest.PostFormValue("pluginNameList")

	flag, result, err := plugin.BatchStopPlugin(pluginNameList)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			result,
			err)
		return

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

Y
Your Name 已提交
414
//BatchStartPlugin 批量关闭插件
E
eoLinker API Management 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
func BatchStartPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginNameList := httpRequest.PostFormValue("pluginNameList")

	flag, result, err := plugin.BatchStartPlugin(pluginNameList)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			result,
			err)
		return

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

}

Y
Your Name 已提交
438
//CheckPluginIsAvailable 判断插件是否可用
E
eoLinker API Management 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
func CheckPluginIsAvailable(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationPlugin, controller.OperationEDIT)
	if e != nil {
		return
	}

	pluginName := httpRequest.PostFormValue("pluginName")
	if len(pluginName) < 2 {
		controller.WriteError(httpResponse,
			"210002",
			"plugin",
			"[ERROR]Illegal pluginName",
			nil)
		return
	}
	flag, nodeList, err := node.GetNodeIPList()
	if !flag {
		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			"[ERROR]The Open node list is empty",
			err)
		return
	}
	flag, errPluginList := utils.CheckPluginIsAvailiable(pluginName, nodeList)
	if !flag {

		controller.WriteResultInfoWithCode(httpResponse,
			"210000",
			"plugin",
			"errNodeList",
			errPluginList)
		return
	}
	flag, res, _ := plugin.EditPluginCheckStatus(pluginName, 1)
	if !flag {

		controller.WriteError(httpResponse,
			"210000",
			"plugin",
			res,
			err)
		return
	}

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

	return
}