apiPlugin.go 16.0 KB
Newer Older
Y
Your Name 已提交
1
package console_sqlite3
E
eoLinker API Management 已提交
2 3 4 5 6 7 8

import (
	SQL "database/sql"
	"errors"
	"fmt"
	"strings"

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

黄孟柱 已提交
11
	database2 "github.com/eolinker/goku-api-gateway/common/database"
E
eoLinker API Management 已提交
12 13 14 15 16 17

	"time"
)

var apiPlugins = []string{"goku-proxy_caching", "goku-circuit_breaker"}

Y
Your Name 已提交
18
//AddPluginToAPI 新增接口插件
Y
Your Name 已提交
19
func AddPluginToAPI(pluginName, config, strategyID string, apiID, userID int) (bool, interface{}, error) {
E
eoLinker API Management 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
	db := database2.GetConnection()
	// 查询接口是否添加该插件
	sql := "SELECT apiID FROM goku_conn_plugin_api WHERE strategyID = ? AND pluginName = ? AND apiID = ?;"
	var id int
	err := db.QueryRow(sql, strategyID, pluginName, apiID).Scan(&id)
	if err == nil {
		return false, "[ERROR]The api plugin is already exist", errors.New("[ERROR]The api plugin is already exist")
	}
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	result, err := Tx.Exec("INSERT INTO goku_conn_plugin_api (pluginName,pluginConfig,strategyID,apiID,updateTime,createTime,pluginStatus,updaterID) VALUES (?,?,?,?,?,?,?,?);", pluginName, config, strategyID, apiID, now, now, 1, userID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to insert data", errors.New("[ERROR]Fail to insert data")
	}
	connID, _ := result.LastInsertId()
	sql = "UPDATE goku_gateway_strategy SET updateTime = ? WHERE strategyID = ?;"
	_, err = Tx.Exec(sql, now, strategyID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Failed to update data!", err
	}
	Tx.Commit()
	return true, int(connID), nil
}

Y
Your Name 已提交
46
//EditAPIPluginConfig 修改接口插件配置
Y
Your Name 已提交
47
func EditAPIPluginConfig(pluginName, config, strategyID string, apiID, userID int) (bool, interface{}, error) {
E
eoLinker API Management 已提交
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
	db := database2.GetConnection()
	// 查询接口是否添加该插件
	t := time.Now()
	now := t.Format("2006-01-02 15:04:05")

	sql := "SELECT connID,apiID FROM goku_conn_plugin_api WHERE strategyID = ? AND pluginName = ? AND apiID = ?;"
	var id, aID int
	err := db.QueryRow(sql, strategyID, pluginName, apiID).Scan(&id, &aID)
	if err != nil {
		return false, "[ERROR]The api plugin is not exist", errors.New("[ERROR]The api plugin is not exist")
	}
	updateTag := t.Format("20060102150405")
	Tx, _ := db.Begin()
	_, err = Tx.Exec("UPDATE goku_conn_plugin_api SET updateTag = ?,pluginConfig = ?,updateTime = ?,updaterID = ? WHERE strategyID = ? AND apiID = ? AND pluginName = ?;", updateTag, config, now, userID, strategyID, apiID, pluginName)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data", errors.New("[ERROR]Fail to update data")
	}

	sql = "UPDATE goku_gateway_strategy SET updateTime = ? WHERE strategyID = ?;"
	_, err = Tx.Exec(sql, now, strategyID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Failed to update data!", err
	}
	Tx.Commit()
	return true, id, nil
}

Y
Your Name 已提交
77 78
//GetAPIPluginList 获取接口插件列表
func GetAPIPluginList(apiID int, strategyID string) (bool, []map[string]interface{}, error) {
E
eoLinker API Management 已提交
79 80 81 82 83 84 85 86 87
	db := database2.GetConnection()
	sql := `SELECT goku_conn_plugin_api.connID,goku_conn_plugin_api.pluginName,IFNULL(goku_conn_plugin_api.createTime,""),IFNULL(goku_conn_plugin_api.updateTime,""),goku_conn_plugin_api.pluginConfig,goku_plugin.pluginPriority, IF(goku_plugin.pluginStatus=0,-1,goku_conn_plugin_api.pluginStatus) as pluginStatus,goku_gateway_api.requestURL FROM goku_conn_plugin_api INNER JOIN goku_plugin ON goku_plugin.pluginName = goku_conn_plugin_api.pluginName INNER goku_gateway_api.apiID = goku_conn_plugin_api.apiID WHERE goku_conn_plugin_api.apiID = ? AND goku_conn_plugin_api.strategyID = ? ORDER BY pluginStatus DESC,goku_conn_plugin_api.updateTime DESC;`
	rows, err := db.Query(sql, apiID, strategyID)
	if err != nil {
		return false, nil, err
	}
	defer rows.Close()
	pluginList := make([]map[string]interface{}, 0)
	//获取记录列
Y
Your Name 已提交
88

Y
Your Name 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	for rows.Next() {
		var pluginPriority, pluginStatus, connID int
		var pluginName, pluginConfig, createTime, updateTime, requestURL string
		err = rows.Scan(&connID, &pluginName, &pluginConfig, &createTime, &updateTime, &pluginPriority, &pluginStatus, &requestURL)
		if err != nil {
			info := err.Error()
			log.Info(info)
		}
		pluginInfo := map[string]interface{}{
			"connID":         connID,
			"pluginName":     pluginName,
			"pluginConfig":   pluginConfig,
			"pluginPriority": pluginPriority,
			"pluginStatus":   pluginStatus,
			"createTime":     createTime,
			"updateTime":     updateTime,
			"requestURL":     requestURL,
E
eoLinker API Management 已提交
106
		}
Y
Your Name 已提交
107
		pluginList = append(pluginList, pluginInfo)
E
eoLinker API Management 已提交
108
	}
Y
Your Name 已提交
109
	return true, pluginList, nil
E
eoLinker API Management 已提交
110 111
}

Y
Your Name 已提交
112
//GetPluginIndex 获取插件优先级
E
eoLinker API Management 已提交
113 114 115 116 117 118 119 120 121 122 123
func GetPluginIndex(pluginName string) (bool, int, error) {
	db := database2.GetConnection()
	var pluginPriority int
	sql := "SELECT pluginPriority FROM goku_plugin WHERE pluginName = ?;"
	err := db.QueryRow(sql, pluginName).Scan(pluginPriority)
	if err != nil {
		return false, 0, err
	}
	return true, pluginPriority, nil
}

Y
Your Name 已提交
124
//GetAPIPluginConfig 通过APIID获取配置信息
Y
Your Name 已提交
125
func GetAPIPluginConfig(apiID int, strategyID, pluginName string) (bool, map[string]string, error) {
E
eoLinker API Management 已提交
126 127 128 129 130 131 132 133
	db := database2.GetConnection()
	sql := "SELECT goku_gateway_api.apiName,goku_gateway_api.requestURL,goku_conn_plugin_api.pluginConfig FROM goku_conn_plugin_api INNER JOIN goku_gateway_api ON goku_gateway_api.apiID = goku_conn_plugin_api.apiID WHERE goku_conn_plugin_api.apiID = ? AND goku_conn_plugin_api.strategyID = ? AND goku_conn_plugin_api.pluginName = ?;"
	var p, apiName, requestURL string
	err := db.QueryRow(sql, apiID, strategyID, pluginName).Scan(&apiName, &requestURL, &p)
	if err != nil {
		if err == SQL.ErrNoRows {
			return false, nil, errors.New("[ERROR]Can not find the plugin")
		}
Y
Your Name 已提交
134
		return false, nil, err
E
eoLinker API Management 已提交
135
	}
Y
Your Name 已提交
136 137 138 139 140 141
	apiPluginInfo := map[string]string{
		"pluginConfig": p,
		"apiName":      apiName,
		"requestURL":   requestURL,
	}
	return true, apiPluginInfo, nil
E
eoLinker API Management 已提交
142 143
}

Y
Your Name 已提交
144
//CheckPluginIsExistInAPI 检查策略组是否绑定插件
Y
Your Name 已提交
145
func CheckPluginIsExistInAPI(strategyID, pluginName string, apiID int) (bool, error) {
E
eoLinker API Management 已提交
146 147 148 149 150 151 152
	db := database2.GetConnection()
	sql := "SELECT apiID FROM goku_conn_plugin_api WHERE strategyID = ? AND pluginName = ? AND apiID = ?;"
	var id int
	err := db.QueryRow(sql, strategyID, pluginName, apiID).Scan(&id)
	if err != nil {
		return false, err
	}
Y
Your Name 已提交
153
	return true, err
E
eoLinker API Management 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
}

// GetAPIPluginInStrategyByAPIID 通过接口ID获取策略组中接口插件列表
func GetAPIPluginInStrategyByAPIID(strategyID string, apiID int, keyword string, condition int) (bool, []map[string]interface{}, map[string]interface{}, error) {
	db := database2.GetConnection()
	var (
		apiName       string
		requestURL    string
		targetURL     string
		target        string
		rewriteTarget string
	)
	sql := "SELECT A.apiName,A.requestURL,IFNULL(A.targetURL,''),IFNULL(A.balanceName,''),IFNULL(B.target,'') FROM goku_gateway_api A INNER JOIN goku_conn_strategy_api B ON A.apiID = B.apiID WHERE B.apiID = ? AND B.strategyID = ?;"
	err := db.QueryRow(sql, apiID, strategyID).Scan(&apiName, &requestURL, &targetURL, &target, &rewriteTarget)
	if err != nil {
		return false, nil, nil, err
	}
	apiInfo := map[string]interface{}{
		"apiID":         apiID,
		"apiName":       apiName,
		"requestURL":    requestURL,
		"targetURL":     targetURL,
		"target":        target,
		"rewriteTarget": rewriteTarget,
	}

	rule := make([]string, 0, 3)

	rule = append(rule, fmt.Sprintf("A.strategyID = '%s'", strategyID))
	rule = append(rule, fmt.Sprintf("A.apiID = %d", apiID))
	if keyword != "" {
		searchRule := "(A.pluginName LIKE '%" + keyword + "%' OR C.pluginDesc LIKE '%" + keyword + "%')"
		rule = append(rule, searchRule)
	}
	if condition > 0 {
Y
Your Name 已提交
189
		rule = append(rule, fmt.Sprintf("CASE WHEN C.pluginStatus=0 THEN -1 ELSE A.pluginStatus END = %d", condition-1))
E
eoLinker API Management 已提交
190 191 192 193 194
	}
	ruleStr := ""
	if len(rule) > 0 {
		ruleStr += "WHERE " + strings.Join(rule, " AND ")
	}
Y
Your Name 已提交
195
	sql = fmt.Sprintf(`SELECT A.connID,A.pluginName,IFNULL(A.createTime,""),IFNULL(A.updateTime,""),CASE WHEN C.pluginStatus=0 THEN -1 ELSE A.pluginStatus END as pluginStatus,IFNULL(C.pluginDesc,""),CASE WHEN B.remark is null or B.remark = "" THEN B.loginCall ELSE B.remark END AS updaterName FROM goku_conn_plugin_api A LEFT JOIN goku_admin B ON A.updaterID=B.userID INNER JOIN goku_plugin C ON C.pluginName = A.pluginName %s ORDER BY pluginStatus DESC,A.updateTime DESC;`, ruleStr)
E
eoLinker API Management 已提交
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
	rows, err := db.Query(sql)
	if err != nil {
		return false, nil, nil, err
	}
	defer rows.Close()
	pluginList := make([]map[string]interface{}, 0)
	//获取记录列
	for rows.Next() {
		var updaterName SQL.NullString
		var pluginStatus, connID int
		var pluginName, pluginDesc, createTime, updateTime string
		err = rows.Scan(&connID, &pluginName, &createTime, &updateTime, &pluginStatus, &pluginDesc, &updaterName)
		if err != nil {
			return false, nil, nil, err
		}

		pluginInfo := map[string]interface{}{
			"connID":       connID,
			"pluginName":   pluginName,
			"pluginStatus": pluginStatus,
			"createTime":   createTime,
			"updateTime":   updateTime,
			"updaterName":  updaterName.String,
			"pluginDesc":   pluginDesc,
		}
		pluginList = append(pluginList, pluginInfo)
	}
	return true, pluginList, apiInfo, nil
}

Y
Your Name 已提交
226
//GetAllAPIPluginInStrategy 获取策略组中所有接口插件列表
Y
Your Name 已提交
227
func GetAllAPIPluginInStrategy(strategyID string) (bool, []map[string]interface{}, error) {
E
eoLinker API Management 已提交
228 229 230 231 232 233 234 235 236
	db := database2.GetConnection()
	sql := `SELECT goku_conn_plugin_api.connID,goku_conn_plugin_api.apiID,goku_gateway_api.apiName,goku_gateway_api.requestURL,goku_conn_plugin_api.pluginName,IFNULL(goku_conn_plugin_api.createTime,""),IFNULL(goku_conn_plugin_api.updateTime,""),IF(goku_plugin.pluginStatus=0,-1,goku_conn_plugin_api.pluginStatus) as pluginStatus,IFNULL(goku_plugin.pluginDesc,"") FROM goku_conn_plugin_api INNER JOIN goku_gateway_api ON goku_gateway_api.apiID = goku_conn_plugin_api.apiID INNER JOIN goku_plugin ON goku_plugin.pluginName = goku_conn_plugin_api.pluginName WHERE goku_conn_plugin_api.strategyID = ? ORDER BY pluginStatus DESC,goku_conn_plugin_api.updateTime DESC;`
	rows, err := db.Query(sql, strategyID)
	if err != nil {
		return false, make([]map[string]interface{}, 0), err
	}
	defer rows.Close()
	pluginList := make([]map[string]interface{}, 0)
	//获取记录列
Y
Your Name 已提交
237

Y
Your Name 已提交
238 239 240 241 242 243
	for rows.Next() {
		var pluginStatus, apiID, connID int
		var apiName, pluginName, pluginDesc, createTime, updateTime, requestURL string
		err = rows.Scan(&connID, &apiID, &apiName, &requestURL, &pluginName, &createTime, &updateTime, &pluginStatus, &pluginDesc)
		if err != nil {
			return false, make([]map[string]interface{}, 0), err
E
eoLinker API Management 已提交
244
		}
Y
Your Name 已提交
245 246 247 248 249 250 251 252 253 254 255 256
		pluginInfo := map[string]interface{}{
			"connID":       connID,
			"apiID":        apiID,
			"apiName":      apiName,
			"pluginName":   pluginName,
			"pluginStatus": pluginStatus,
			"createTime":   createTime,
			"updateTime":   updateTime,
			"requestURL":   requestURL,
			"pluginDesc":   pluginDesc,
		}
		pluginList = append(pluginList, pluginInfo)
E
eoLinker API Management 已提交
257
	}
Y
Your Name 已提交
258
	return true, pluginList, nil
E
eoLinker API Management 已提交
259 260
}

Y
Your Name 已提交
261
//BatchEditAPIPluginStatus 批量修改策略组插件状态
Y
Your Name 已提交
262
func BatchEditAPIPluginStatus(connIDList, strategyID string, pluginStatus, userID int) (bool, string, error) {
E
eoLinker API Management 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	db := database2.GetConnection()
	t := time.Now()
	now := t.Format("2006-01-02 15:04:05")
	updateTag := t.Format("20060102150405")
	Tx, _ := db.Begin()
	sql := "UPDATE goku_conn_plugin_api SET updateTag = ?,pluginStatus = ?,updateTime = ?,updaterID = ? WHERE connID IN (" + connIDList + ");"
	_, err := Tx.Exec(sql, updateTag, pluginStatus, now, userID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}

	// 根据connID获取apiID
	sql = "SELECT apiID FROM goku_conn_plugin_api WHERE connID IN (" + connIDList + ");"
	rows, err := db.Query(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Illegal SQL Statement!", err
	}
	defer rows.Close()
	//获取记录列
Y
Your Name 已提交
284

Y
Your Name 已提交
285 286 287
	for rows.Next() {
		var apiID int
		err = rows.Scan(&apiID)
E
eoLinker API Management 已提交
288 289
		if err != nil {
			Tx.Rollback()
Y
Your Name 已提交
290
			return false, "[ERROR]Fail to get data!", err
E
eoLinker API Management 已提交
291 292
		}
	}
Y
Your Name 已提交
293 294 295 296 297 298 299
	sql = "UPDATE goku_gateway_strategy SET updateTime = ? WHERE strategyID = ?;"
	_, err = Tx.Exec(sql, now, strategyID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
E
eoLinker API Management 已提交
300 301 302
	return true, "", nil
}

Y
Your Name 已提交
303
//BatchDeleteAPIPlugin 批量删除策略组插件
Y
Your Name 已提交
304
func BatchDeleteAPIPlugin(connIDList, strategyID string) (bool, string, error) {
E
eoLinker API Management 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317
	db := database2.GetConnection()
	now := time.Now().Format("2006-01-02 15:04:05")
	Tx, _ := db.Begin()
	apiIDList := make([]int, 0)
	// 根据connID获取apiID
	sql := "SELECT apiID FROM goku_conn_plugin_api WHERE connID IN (" + connIDList + ");"
	rows, err := Tx.Query(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Illegal SQL Statement!", err
	}
	defer rows.Close()
	//获取记录列
Y
Your Name 已提交
318

Y
Your Name 已提交
319 320 321 322 323 324
	for rows.Next() {
		var apiID int
		err = rows.Scan(&apiID)
		if err != nil {
			Tx.Rollback()
			return false, "[ERROR]Fail to get data!", err
E
eoLinker API Management 已提交
325
		}
Y
Your Name 已提交
326
		apiIDList = append(apiIDList, apiID)
E
eoLinker API Management 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	}
	sql = "DELETE FROM goku_conn_plugin_api WHERE connID IN (" + connIDList + ");"
	_, err = Tx.Exec(sql)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to excute SQL statement!", err
	}

	sql = "UPDATE goku_gateway_strategy SET updateTime = ? WHERE strategyID = ?;"
	_, err = Tx.Exec(sql, now, strategyID)
	if err != nil {
		Tx.Rollback()
		return false, "[ERROR]Fail to update data!", err
	}
	Tx.Commit()
	return true, "", nil
}

Y
Your Name 已提交
345
//GetAPIPluginName 通过connID获取插件名称
Y
Your Name 已提交
346
func GetAPIPluginName(connID int) (bool, string, error) {
E
eoLinker API Management 已提交
347 348 349 350 351 352 353 354 355 356
	db := database2.GetConnection()
	var pluginName string
	sql := "SELECT pluginName FROM goku_conn_plugin_api WHERE connID = ?"
	err := db.QueryRow(sql, connID).Scan(&pluginName)
	if err != nil {
		return false, "[ERROR]The plugin is not existing!", err
	}
	return true, "", nil
}

Y
Your Name 已提交
357
//CheckAPIPluginIsExistByConnIDList 通过connIDList判断插件是否存在
Y
Your Name 已提交
358
func CheckAPIPluginIsExistByConnIDList(connIDList, pluginName string) (bool, []int, error) {
E
eoLinker API Management 已提交
359 360 361 362 363 364 365 366
	db := database2.GetConnection()
	sql := "SELECT apiID FROM goku_conn_plugin_api WHERE connID IN (" + connIDList + ") AND pluginName = ?;"
	rows, err := db.Query(sql, pluginName)
	if err != nil {
		return false, make([]int, 0), err
	}
	defer rows.Close()
	apiIDList := make([]int, 0)
Y
Your Name 已提交
367 368 369 370 371 372

	for rows.Next() {
		var apiID int
		err = rows.Scan(&apiID)
		if err != nil {
			return false, make([]int, 0), err
E
eoLinker API Management 已提交
373
		}
Y
Your Name 已提交
374
		apiIDList = append(apiIDList, apiID)
E
eoLinker API Management 已提交
375 376 377 378
	}
	return true, apiIDList, nil
}

Y
Your Name 已提交
379
//GetAPIPluginListWithNotAssignAPIList 获取没有绑定嵌套插件列表
Y
Your Name 已提交
380
func GetAPIPluginListWithNotAssignAPIList(strategyID string) (bool, []map[string]interface{}, error) {
E
eoLinker API Management 已提交
381 382 383 384 385 386 387 388 389
	db := database2.GetConnection()
	sql := "SELECT pluginID,pluginDesc,pluginName FROM goku_plugin WHERE pluginType = 2 AND pluginStatus = 1;"
	rows, err := db.Query(sql)
	if err != nil {
		return false, make([]map[string]interface{}, 0), err
	}
	defer rows.Close()
	pluginList := make([]map[string]interface{}, 0)
	//获取记录列
Y
Your Name 已提交
390

Y
Your Name 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
	sql = "SELECT goku_gateway_api.apiID,goku_gateway_api.apiName,goku_gateway_api.requestURL FROM goku_gateway_api INNER JOIN goku_conn_strategy_api ON goku_gateway_api.apiID = goku_conn_strategy_api.apiID WHERE goku_conn_strategy_api.strategyID = ? AND goku_gateway_api.apiID NOT IN (SELECT goku_conn_plugin_api.apiID FROM goku_conn_plugin_api WHERE goku_conn_plugin_api.strategyID = ? AND goku_conn_plugin_api.pluginName = ?);"
	for rows.Next() {
		var pluginID int
		var pluginName, chineseName string
		err = rows.Scan(&pluginID, &chineseName, &pluginName)
		if err != nil {
			info := err.Error()
			log.Info(info)
			return false, make([]map[string]interface{}, 0), err
		}
		r, err := db.Query(sql, strategyID, strategyID, pluginName)
		if err != nil {
			return false, make([]map[string]interface{}, 0), err
		}
		defer r.Close()
		apiList := make([]map[string]interface{}, 0)
		for r.Next() {
			var (
				apiID      int
				apiName    string
				requestURL string
			)
			err = r.Scan(&apiID, &apiName, &requestURL)
E
eoLinker API Management 已提交
414 415 416
			if err != nil {
				return false, make([]map[string]interface{}, 0), err
			}
Y
Your Name 已提交
417 418 419 420 421
			apiList = append(apiList, map[string]interface{}{
				"apiID":      apiID,
				"apiName":    apiName,
				"requestURL": requestURL,
			})
E
eoLinker API Management 已提交
422 423

		}
Y
Your Name 已提交
424 425 426 427 428 429 430
		pluginInfo := map[string]interface{}{
			"chineseName": chineseName,
			"pluginName":  pluginName,
			"pluginID":    pluginID,
			"apiList":     apiList,
		}
		pluginList = append(pluginList, pluginInfo)
E
eoLinker API Management 已提交
431
	}
Y
Your Name 已提交
432
	return true, pluginList, nil
E
eoLinker API Management 已提交
433
}