request.go 2.7 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11
package utils

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"time"
)

Y
Your Name 已提交
12
//CheckPluginIsAvailiable  检查插件是否可用
E
eoLinker API Management 已提交
13 14 15 16 17 18 19 20 21
func CheckPluginIsAvailiable(pluginName string, nodeList []map[string]interface{}) (bool, []map[string]interface{}) {
	errNodeList := make([]map[string]interface{}, 0)
	for _, v := range nodeList {
		if v["nodePort"] == "" {
			v["nodePort"] = "6689"
		}
		client := &http.Client{
			Timeout: time.Second * 15,
		}
Y
Your Name 已提交
22
		var data = url.Values{}
E
eoLinker API Management 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		data.Add("pluginName", pluginName)

		request, err := http.NewRequest("POST", "http://"+v["nodeIP"].(string)+":"+v["nodePort"].(string)+"/goku-check_plugin", strings.NewReader(data.Encode()))
		if err != nil {
			errNode := map[string]interface{}{
				"nodeAddress":     v["nodeIP"].(string) + ":" + v["nodePort"].(string),
				"error":           "[ERROR] Fail to create request",
				"errorStatusCode": "210014",
			}
			errNodeList = append(errNodeList, errNode)
			continue
		}
		request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
		resp, err := client.Do(request)
		if err != nil {
			errNode := map[string]interface{}{
				"nodeAddress":     v["nodeIP"].(string) + ":" + v["nodePort"].(string),
				"error":           "[ERROR] Connect timeout",
				"errorStatusCode": "210011",
			}
			errNodeList = append(errNodeList, errNode)
			continue
		}
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			errNode := map[string]interface{}{
				"nodeAddress":     v["nodeIP"].(string) + ":" + v["nodePort"].(string),
				"error":           "[ERROR] Fail to read body",
				"errorStatusCode": "210013",
			}
			errNodeList = append(errNodeList, errNode)
			continue
		}
		resp.Body.Close()
Y
Your Name 已提交
57 58
		var bodyJSON map[string]string
		err = json.Unmarshal(body, &bodyJSON)
E
eoLinker API Management 已提交
59 60 61 62 63 64 65 66 67
		if err != nil {
			errNode := map[string]interface{}{
				"nodeAddress":     v["nodeIP"].(string) + ":" + v["nodePort"].(string),
				"error":           "[ERROR] Fail to parse json",
				"errorStatusCode": "210010",
			}
			errNodeList = append(errNodeList, errNode)
			continue
		}
Y
Your Name 已提交
68
		if _, ok := bodyJSON["statusCode"]; !ok {
E
eoLinker API Management 已提交
69 70 71 72 73 74 75 76
			errNode := map[string]interface{}{
				"nodeAddress":     v["nodeIP"].(string) + ":" + v["nodePort"].(string),
				"error":           "[ERROR] Fail to get statusCode",
				"errorStatusCode": "210012",
			}
			errNodeList = append(errNodeList, errNode)
			continue
		}
Y
Your Name 已提交
77
		if bodyJSON["statusCode"] != "000000" {
E
eoLinker API Management 已提交
78 79
			errNode := map[string]interface{}{
				"nodeAddress":     v["nodeIP"].(string) + ":" + v["nodePort"].(string),
Y
Your Name 已提交
80 81
				"error":           bodyJSON["resultDesc"],
				"errorStatusCode": bodyJSON["statusCode"],
E
eoLinker API Management 已提交
82 83 84 85 86 87 88 89
			}
			errNodeList = append(errNodeList, errNode)
			continue
		}
	}
	if len(errNodeList) > 0 {
		return false, errNodeList
	}
Y
Your Name 已提交
90
	return true, errNodeList
E
eoLinker API Management 已提交
91
}