strategyPlugin.go 9.2 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6
package strategy

import (
	"net/http"
	"strconv"

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

黄孟柱 已提交
9 10 11 12
	"github.com/eolinker/goku-api-gateway/console/controller"
	"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/console/module/strategy"
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 37 38 39 40 41
const operationStrategyPlugin = "strategyManagement"

//PluginHandlers 策略插件处理器
type PluginHandlers struct {
}

//Handlers handlers
func (h *PluginHandlers) Handlers(factory *goku_handler.AccountHandlerFactory) map[string]http.Handler {
	return map[string]http.Handler{
		"/addPluginToStrategy": factory.NewAccountHandleFunction(operationAPIStrategy, true, AddPluginToStrategy),
		"/edit":                factory.NewAccountHandleFunction(operationAPIStrategy, true, EditStrategyPluginConfig),
		"/getInfo":             factory.NewAccountHandleFunction(operationAPIStrategy, false, GetStrategyPluginConfig),
		"/getList":             factory.NewAccountHandleFunction(operationAPIStrategy, false, GetStrategyPluginList),
		"/checkPluginIsExist":  factory.NewAccountHandleFunction(operationAPIStrategy, false, CheckPluginIsExistInStrategy),
		"/getStatus":           factory.NewAccountHandleFunction(operationAPIStrategy, false, GetStrategyPluginStatus),
		"/batchStart":          factory.NewAccountHandleFunction(operationAPIStrategy, false, BatchStartStrategyPlugin),
		"/batchStop":           factory.NewAccountHandleFunction(operationAPIStrategy, true, BatchStopStrategyPlugin),
		"/batchDelete":         factory.NewAccountHandleFunction(operationAPIStrategy, true, BatchDeleteStrategyPlugin),
	}

}

//NewPluginHandlers new pluginHandlers
func NewPluginHandlers() *PluginHandlers {
	return &PluginHandlers{}
}

Y
Your Name 已提交
42
//AddPluginToStrategy 新增插件到接口
E
eoLinker API Management 已提交
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
func AddPluginToStrategy(httpResponse http.ResponseWriter, httpRequest *http.Request) {

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

	// 查询该插件是否存在,如果存在,则修改信息,不存在,则新增
	flag, err = plugin.CheckNameIsExist(pluginName)
	if !flag {

		controller.WriteError(httpResponse,
			"200005",
			"strategyPlugin",
			"[ERROR]The plugin does not exist!",
			err)
		return
	}
	// 检查插件是否已经绑定该插件
	exits, _ := strategy.CheckPluginIsExistInStrategy(strategyID, pluginName)
	if exits {
		flag, resultDesc, err := strategy.EditStrategyPluginConfig(pluginName, pluginConfig, strategyID)
		if !flag {
			controller.WriteError(httpResponse,
				"270000",
				"strategyPlugin",
				resultDesc,
				err)
			return
		}
		_, connID, _ := strategy.GetConnIDFromStrategyPlugin(pluginName, strategyID)
		controller.WriteResultInfo(httpResponse, "strategyPlugin", "connID", connID)
		return
	}
	flag, resultDesc, err := strategy.AddPluginToStrategy(pluginName, pluginConfig, strategyID)
	if !flag {
		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			resultDesc.(string),
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "strategyPlugin", "connID", resultDesc)

}

Y
Your Name 已提交
95
//EditStrategyPluginConfig 修改插件信息
E
eoLinker API Management 已提交
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
func EditStrategyPluginConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {

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

	// 查询该插件是否存在
	flag, err = plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"200005",
			"strategyPlugin",
			"[ERROR]The plugin does not exist!",
			err)
		return

	}
	flag, resultDesc, err := strategy.EditStrategyPluginConfig(pluginName, pluginConfig, strategyID)
	if !flag {

		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			resultDesc,
			err)
		return

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

}

Y
Your Name 已提交
133
//GetStrategyPluginList 获取策略组插件列表
E
eoLinker API Management 已提交
134
func GetStrategyPluginList(httpResponse http.ResponseWriter, httpRequest *http.Request) {
Y
Your Name 已提交
135

E
eoLinker API Management 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	httpRequest.ParseForm()
	strategyID := httpRequest.Form.Get("strategyID")
	keyword := httpRequest.Form.Get("keyword")
	condition := httpRequest.Form.Get("condition")
	op, err := strconv.Atoi(condition)
	if err != nil && condition != "" {
		controller.WriteError(httpResponse, "270002", "plugin", "[ERROR]Illegal condition!", err)
		return
	}

	// 查询该插件是否存在
	flag, result, err := strategy.GetStrategyPluginList(strategyID, keyword, op)
	if !flag {
		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			"[ERROR]Empty strategy plugin list!",
			err)
		return

	}
	controller.WriteResultInfo(httpResponse, "strategyPlugin", "strategyPluginList", result)
}

Y
Your Name 已提交
160
//GetStrategyPluginConfig 获取策略组插件信息
E
eoLinker API Management 已提交
161 162 163 164 165 166 167 168 169 170
func GetStrategyPluginConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	strategyID := httpRequest.PostFormValue("strategyID")
	pluginName := httpRequest.PostFormValue("pluginName")
	_, result, _ := strategy.GetStrategyPluginConfig(strategyID, pluginName)

	controller.WriteResultInfo(httpResponse, "strategyPlugin", "strategyPluginConfig", result)

}

Y
Your Name 已提交
171
//CheckPluginIsExistInStrategy 检查策略组是否绑定插件
E
eoLinker API Management 已提交
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
func CheckPluginIsExistInStrategy(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	strategyID := httpRequest.PostFormValue("strategyID")
	pluginName := httpRequest.PostFormValue("pluginName")

	// 查询该插件是否存在
	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"200005",
			"strategyPlugin",
			"[ERROR]The plugin does not exist!",
			err)
		return
	}
	flag, err = strategy.CheckPluginIsExistInStrategy(strategyID, pluginName)
	if !flag {

		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			"[ERROR]The api plugin is existed!",
			err)
		return

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

Y
Your Name 已提交
201
//GetStrategyPluginStatus 检查策略组插件是否开启
E
eoLinker API Management 已提交
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
func GetStrategyPluginStatus(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	strategyID := httpRequest.PostFormValue("strategyID")
	pluginName := httpRequest.PostFormValue("pluginName")

	// 查询该插件是否存在
	flag, err := plugin.CheckNameIsExist(pluginName)
	if !flag {
		controller.WriteError(httpResponse,
			"200005",
			"strategyPlugin",
			"[ERROR]The plugin does not exist!",
			err)
		return

	}
	flag, err = strategy.GetStrategyPluginStatus(strategyID, pluginName)
	if !flag {

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

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

}

Y
Your Name 已提交
233
//BatchStartStrategyPlugin 批量开启策略组插件
E
eoLinker API Management 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
func BatchStartStrategyPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	strategyID := httpRequest.PostFormValue("strategyID")
	connIDList := httpRequest.PostFormValue("connIDList")
	if connIDList == "" {
		controller.WriteError(httpResponse,
			"270001",
			"strategyPlugin",
			"[ERROR]Illegal connIDList",
			nil)
		return
	}
	flag, result, err := strategy.BatchEditStrategyPluginStatus(connIDList, strategyID, 1)
	if !flag {
		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			result,
			err)
		return

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

Y
Your Name 已提交
259
//BatchStopStrategyPlugin 批量修改策略组插件状态
E
eoLinker API Management 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
func BatchStopStrategyPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	strategyID := httpRequest.PostFormValue("strategyID")
	connIDList := httpRequest.PostFormValue("connIDList")
	if connIDList == "" {
		controller.WriteError(httpResponse,
			"270001",
			"strategyPlugin",
			"[ERROR]Illegal connIDList",
			nil)
		return
	}
	flag, result, err := strategy.BatchEditStrategyPluginStatus(connIDList, strategyID, 0)
	if !flag {
		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			result,
			err)
		return
	}
	controller.WriteResultInfo(httpResponse, "strategyPlugin", "", nil)
}

Y
Your Name 已提交
284
//BatchDeleteStrategyPlugin 批量删除策略组插件
E
eoLinker API Management 已提交
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
func BatchDeleteStrategyPlugin(httpResponse http.ResponseWriter, httpRequest *http.Request) {

	strategyID := httpRequest.PostFormValue("strategyID")
	connIDList := httpRequest.PostFormValue("connIDList")
	if connIDList == "" {
		controller.WriteError(httpResponse,
			"270001",
			"strategyPlugin",
			"[ERROR]Illegal connIDList",
			nil)
		return
	}
	flag, result, err := strategy.BatchDeleteStrategyPlugin(connIDList, strategyID)
	if !flag {

		controller.WriteError(httpResponse,
			"270000",
			"strategyPlugin",
			result,
			err)
		return

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