Exemple #1
0
func TokenValidation(tokenId string) (errorCode string, err error) {
	userUrl, err := common.UTIL.LbClient.GetUserMgmtEndpoint()
	if err != nil {
		logrus.Errorf("get userMgmt endpoint err is %v", err)
		return COMMON_ERROR_INTERNAL, err
	}
	url := strings.Join([]string{"http://", userUrl, "/v1/token/?", "token=", tokenId}, "")
	logrus.Debugln("token validation url=" + url)

	resp, err := httpclient.Http_get(url, "",
		httpclient.Header{"Content-Type", "application/json"})
	if err != nil {
		logrus.Errorf("http get token validate error %v", err)
		return COMMON_ERROR_INTERNAL, err
	}
	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode >= 400 {
		logrus.Errorf("token validation failed %v", string(data))
		errorCode, err = getErrorFromResponse(data)
		return
	}

	return "", nil
}
Exemple #2
0
func GetTokenById(token string) (currentToken *entity.Token, err error) {
	userUrl, err := common.UTIL.LbClient.GetUserMgmtEndpoint()
	if err != nil {
		logrus.Errorf("get userMgmt endpoint err is %v", err)
		return nil, err
	}
	url := strings.Join([]string{"http://", userUrl, "/v1/token/", token}, "")
	logrus.Debugln("get token url=" + url)

	resp, err := httpclient.Http_get(url, "",
		httpclient.Header{"Content-Type", "application/json"}, httpclient.Header{"X-Auth-Token", token})
	if err != nil {
		logrus.Errorf("http get token error %v", err)
		return nil, err
	}
	defer resp.Body.Close()

	data, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode >= 400 {
		logrus.Errorf("get token by id failed %v", string(data))
		return nil, errors.New("get token by id failed")
	}

	currentToken = new(entity.Token)
	err = getRetFromResponse(data, currentToken)
	return
}
func (m *MarathonService) IsDeploymentDone(deploymentId, marathonEndpoint string) (isDone bool, err error) {
	isDone = true
	url := strings.Join([]string{"http://", marathonEndpoint, "/v2/deployments"}, "")
	logrus.Debugf("start to check deployment %v status from  marathon %v", deploymentId, marathonEndpoint)
	resp, err := httpclient.Http_get(url, "",
		httpclient.Header{"Content-Type", "application/json"})
	if err != nil {
		logrus.Errorf("get deployments from marathon failed, error is %v", err)
		return
	}
	defer resp.Body.Close()

	// if response status is greater than 400, means marathon returns error
	// else returned body, findout deploymentId, and return
	data, _ := ioutil.ReadAll(resp.Body)
	datastr := string(data)
	if resp.StatusCode >= 400 {
		logrus.Errorf("marathon returned error code is %v", resp.StatusCode)
		logrus.Errorf("detail is %v", datastr)
		err = errors.New(datastr)
		return
	}

	// Parse data: marathon json data
	jsondata := []map[string]interface{}{}
	result := json.NewDecoder(strings.NewReader(datastr))
	result.Decode(&jsondata)
	for i := 0; i < len(jsondata); i++ {
		jq := jsonq.NewQuery(jsondata[i])
		inDeployingId, err := jq.String("id")
		logrus.Debugf("found inDeployingId %v", inDeployingId)
		if strings.EqualFold(inDeployingId, deploymentId) {
			isDone = false
			return isDone, err
		}
	}
	return
}
Exemple #4
0
func GetClusterByUser(userid string, x_auth_token string) (cluster []entity.Cluster, err error) {
	clusterurl := common.UTIL.Props.GetString("nodebanlancer.url", "")
	url := strings.Join([]string{"http://", clusterurl, ":10002", "/v1/cluster?user_id=", userid, "&status=unterminated"}, "")
	logrus.Debugln("get cluster url=" + url)

	resp, err := httpclient.Http_get(url, "",
		httpclient.Header{"Content-Type", "application/json"}, httpclient.Header{"X-Auth-Token", x_auth_token})
	if err != nil {
		logrus.Errorf("http get cluster error %v", err)
		return nil, err
	}
	defer resp.Body.Close()
	data, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode >= 400 {
		logrus.Errorf("get cluster by username failed %v", string(data))
		return nil, errors.New("get cluster by username failed")
	}

	cluster = []entity.Cluster{}
	err = getRetFromResponse(data, cluster)
	return

}