當前位置: 首頁>>代碼示例>>Golang>>正文


Golang response.WriteStatusError函數代碼示例

本文整理匯總了Golang中linkernetworks/com/linker_common_lib/rest/response.WriteStatusError函數的典型用法代碼示例。如果您正苦於以下問題:Golang WriteStatusError函數的具體用法?Golang WriteStatusError怎麽用?Golang WriteStatusError使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了WriteStatusError函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: HostsAddHandler

func (p *Resource) HostsAddHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("HostsAddHandler is called!")
	number := req.QueryParameter("number")
	logrus.Infof("Number is %s \n", number)

	x_auth_token := req.HeaderParameter("X-Auth-Token")
	code, err := services.TokenValidation(x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}

	objectId := req.PathParameter(ParamID)
	logrus.Debugf("cluster id is %d \n", objectId)

	cluster, errorCode, err := services.GetClusterService().AddHosts(objectId, number, x_auth_token)

	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	res := response.QueryStruct{Success: true, Data: cluster}
	resp.WriteEntity(res)
	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:26,代碼來源:clusterHandler.go

示例2: UserLoginHandler

// UserLoginHandler parses the http request and login with an exist user.
// Usage :
//		POST v1/user/login
// If successful,response code will be set to 201.
func (p *Resource) UserLoginHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserLoginHandler is called!")

	doc := bson.M{}
	decoder := json.NewDecoder(req.Request.Body)
	err := decoder.Decode(&doc)
	if err != nil {
		logrus.Errorf("decode user err is %v", err)
		response.WriteStatusError(services.USER_ERROR_LOGIN, err, resp)
		return
	}
	username, password, paraErr := userLoginParamCheck(doc)
	if paraErr != nil {
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, paraErr, resp)
		return
	}

	if len(username) == 0 || len(password) == 0 {
		logrus.Errorf("username and password can not be null!")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("Username or password can not be null"), resp)
		return
	}

	errorCode, loginRes, err := services.GetUserService().UserLogin(username, password)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(loginRes, resp)

	return

}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:38,代碼來源:userHandler.go

示例3: HostsDeleteHandler

//terminate specified hosts of a cluster
// Request
// URL:
// 	PUT /v1/cluster/<CLUSTER_ID>/hosts
// Header:
// 	X-Auth-Token
// Except Body:
//{
//    "host_ids":["568e23655d5c3d173019f1ba","568e2be45d5c3d173019f1bb","568e2bfd5d5c3d173019f1bc","568e2c335d5c3d173019f1bd"]
//}
//
// Response:
//{
//  "success": true
//}
//
func (p *Resource) HostsDeleteHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("HostsDeleteHandler is called!")

	x_auth_token := req.HeaderParameter("X-Auth-Token")
	code, err := services.TokenValidation(x_auth_token)
	if err != nil {
		logrus.Errorln("token validation error is %v", err)
		response.WriteStatusError(code, err, resp)
		return
	}

	clusterId := req.PathParameter(ParamID)
	body := TerminateHostsRequestBody{}
	err = json.NewDecoder(req.Request.Body).Decode(&body)

	errorCode, err := services.GetClusterService().TerminateHosts(clusterId, body.HostIds, x_auth_token)
	if err != nil {
		logrus.Errorln("terminate hosts error is %v", err)
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	// Write success response
	response.WriteSuccess(resp)
	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:42,代碼來源:clusterHandler.go

示例4: ClustersListHandler

func (p *Resource) ClustersListHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("ClustersListHandler is called!")
	x_auth_token := req.HeaderParameter("X-Auth-Token")
	code, err := services.TokenValidation(x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}

	var skip int = queryIntParam(req, "skip", 0)
	var limit int = queryIntParam(req, "limit", 0)

	var name string = req.QueryParameter("name")
	var user_id string = req.QueryParameter("user_id")
	var status string = req.QueryParameter("status")
	var sort string = req.QueryParameter("sort")

	total, clusters, code, err := services.GetClusterService().QueryCluster(name, user_id, status, skip, limit, sort, x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}
	res := response.QueryStruct{Success: true, Data: clusters}
	if c, _ := strconv.ParseBool(req.QueryParameter("count")); c {
		res.Count = total
		resp.AddHeader("X-Object-Count", strconv.Itoa(total))
	}
	resp.WriteEntity(res)
	return

}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:31,代碼來源:clusterHandler.go

示例5: UserUpdateHandler

// UserUpdateHandler parses the http request and updata a exist user.
// Usage :
//		PUT /v1/user/{ParamID}
// Params :
//		ParamID : storage identifier of user
// If successful,response code will be set to 201.
func (p *Resource) UserUpdateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserUpdateHanlder is called!")
	token := req.HeaderParameter("X-Auth-Token")
	id := req.PathParameter(ParamID)
	if len(id) <= 0 {
		logrus.Warnln("user id should not be null for update operation")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("user id should not be null for update operation"), resp)
		return
	}

	newuser := entity.User{}

	// Populate the user data
	err := json.NewDecoder(req.Request.Body).Decode(&newuser)
	if err != nil {
		logrus.Errorf("convert body to user failed, error is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp)
		return
	}

	created, id, errorCode, err := services.GetUserService().UserUpdate(token, newuser, id)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	p.successUpdate(id, created, req, resp)
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:34,代碼來源:userHandler.go

示例6: ClusterCreateHandler

func (p *Resource) ClusterCreateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("ClusterCreateHandler is called!")
	x_auth_token := req.HeaderParameter("X-Auth-Token")
	code, err := services.TokenValidation(x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}
	// Stub an acluster to be populated from the body
	cluster := entity.Cluster{}

	err = json.NewDecoder(req.Request.Body).Decode(&cluster)
	if err != nil {
		logrus.Errorf("convert body to cluster failed, error is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp)
		return
	}

	newCluster, code, err := services.GetClusterService().Create(cluster, x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}

	res := response.QueryStruct{Success: true, Data: newCluster}
	resp.WriteEntity(res)
	return

}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:29,代碼來源:clusterHandler.go

示例7: CreateClusterHandler

func (p *Resource) CreateClusterHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("CreateClusterHandler is called!")

	// Stub an repairpolicy to be populated from the body
	request := entity.Request{}

	// Populate the user data
	err := json.NewDecoder(req.Request.Body).Decode(&request)

	logrus.Infof("Request is %v", request)
	logrus.Infof("ProviderInfo is %v", request.ProviderInfo)
	logrus.Infof("AwsEC2Info is %v", request.ProviderInfo.AwsEC2Info)

	if err != nil {
		logrus.Errorf("convert body to request failed, error is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp)
		return
	}

	servers, code, err := services.GetDeployService().CreateCluster(request)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}

	res := response.Response{Success: true, Data: servers}
	resp.WriteEntity(res)
	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:29,代碼來源:deployHandler.go

示例8: TokenCreateHandler

// TokenCreateHandler parses the http request and create a new user token.
// Usage :
//		POST /v1/token
// If successful,response code will be set to 201.
func (p *Resource) TokenCreateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("TokenCreateHandler is called!")

	doc := bson.M{}
	decoder := json.NewDecoder(req.Request.Body)
	err := decoder.Decode(&doc)
	if err != nil {
		logrus.Errorf("decode credential err is %v", err)
		response.WriteStatusError(services.TOKEN_ERROR_CREATE, err, resp)
		return
	}

	username, passwd, tenant, paraErr := tokenCreateParamCheck(doc)
	if paraErr != nil {
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, paraErr, resp)
		return
	}

	ret, errorCode, err := services.GetTokenService().TokenCreate(username, passwd, tenant)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(ret, resp)

	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:32,代碼來源:tokenHandler.go

示例9: DeleteClusterHandler

func (p *Resource) DeleteClusterHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("DeleteClusterHandler is called!")

	// Stub an repairpolicy to be populated from the body
	request := entity.DeleteRequest{}

	// Populate the user data
	err := json.NewDecoder(req.Request.Body).Decode(&request)

	logrus.Infof("Username is %v", request.UserName)
	logrus.Infof("Cluster is %v", request.ClusterName)
	logrus.Infof("Servers is %v", request.Servers)

	if err != nil {
		logrus.Errorf("convert body to DeleteClusterRequest failed, error is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp)
		return
	}

	code, err := services.GetDeployService().DeleteCluster(request.UserName, request.ClusterName, request.Servers)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}

	res := response.Response{Success: true}
	resp.WriteEntity(res)
	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:29,代碼來源:deployHandler.go

示例10: AddNodesHandler

func (p *Resource) AddNodesHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("AddNodesHandler is called!")

	// Stub an repairpolicy to be populated from the body
	request := entity.AddNodeRequest{}

	// Populate the user data
	err := json.NewDecoder(req.Request.Body).Decode(&request)

	logrus.Infof("Username is %v", request.UserName)
	logrus.Infof("Cluster is %v", request.ClusterName)
	logrus.Infof("CreateNumber is %v", request.CreateNumber)

	if err != nil {
		logrus.Errorf("convert body to AddNodesRequest failed, error is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, err, resp)
		return
	}

	servers, code, err := services.GetDeployService().CreateNode(request)

	var res response.Response
	if err != nil {
		errObj := response.Error{Code: code, ErrorMsg: fmt.Sprintf("%v", err)}
		res = response.Response{Success: true, Error: &errObj, Data: servers}
	} else {
		res = response.Response{Success: true, Data: servers}
	}

	resp.WriteEntity(res)
	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:32,代碼來源:deployHandler.go

示例11: HostsListHandler

func (p *Resource) HostsListHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("HostsListHandler is called!")

	x_auth_token := req.HeaderParameter("X-Auth-Token")
	code, err := services.TokenValidation(x_auth_token)
	if err != nil {
		logrus.Errorln("token validation error is %v", err)
		response.WriteStatusError(code, err, resp)
		return
	}

	clusterId := req.PathParameter(ParamID)

	var skip, limit int64
	if param_skip := req.QueryParameter("skip"); len(param_skip) > 0 {
		skip, err = strconv.ParseInt(param_skip, 10, 0)
		if err != nil {
			response.WriteStatusError("E12002", err, resp)
			return
		}
	}
	if param_limit := req.QueryParameter("limit"); len(param_limit) > 0 {
		limit, err = strconv.ParseInt(req.QueryParameter("limit"), 10, 0)
		if err != nil {
			response.WriteStatusError("E12002", err, resp)
			return
		}
	}

	var status string = req.QueryParameter("status")

	total, hosts, errorCode, err := services.GetHostService().QueryHosts(clusterId, int(skip), int(limit), status, x_auth_token)

	if err != nil {
		logrus.Errorln("list hosts error is %v", err)
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	res := response.QueryStruct{Success: true, Data: hosts}
	if c, _ := strconv.ParseBool(req.QueryParameter("count")); c {
		res.Count = total
		resp.AddHeader("X-Object-Count", strconv.Itoa(total))
	}
	resp.WriteEntity(res)
	return
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:47,代碼來源:clusterHandler.go

示例12: TokenValidateHandler

// TokenValidateHandler parses the http request and return valid user token.
// Usage :
//		GET /v1/token
// If successful,response code will be set to 201.
func (p *Resource) TokenValidateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("TokenValidateHandler is called!")

	var tokenId = req.QueryParameter("token")
	if len(tokenId) <= 0 {
		logrus.Errorln("invalie parameter! tokenId can not be null for token validation")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("invalie parameter! tokenId can not be null for token validation"), resp)
		return
	}

	errorCode, err := services.GetTokenService().TokenValidate(tokenId)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}
	response.WriteSuccess(resp)

}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:22,代碼來源:tokenHandler.go

示例13: UserDeleteHandler

// UserDeleteHandler parses the http request and delete a user.
// Usage :
//		DELETE /v1/user/{ParamID}
// Params :
//		ParamID : storage identifier of user
// If successful,response code will be set to 201.
func (p *Resource) UserDeleteHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserDeleteHandler is called!")
	token := req.HeaderParameter("X-Auth-Token")
	id := req.PathParameter(ParamID)
	if len(id) <= 0 {
		logrus.Warnln("user id should not be null for delete operation")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("user id should not be null for delete operation"), resp)
		return
	}

	errorCode, err := services.GetUserService().UserDelete(token, id)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteSuccess(resp)
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:24,代碼來源:userHandler.go

示例14: UserChangePasswdHandler

// UserChangePasswdHandler parses the http request and change
// password of an exist user.
// Usage :
//		PUT v1/user/changepassword/{ParamID}
// Params :
//		ParamID : storage identifier of user
// If successful,response code will be set to 201.
func (p *Resource) UserChangePasswdHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserChangePasswdHandler is called!")
	token := req.HeaderParameter("X-Auth-Token")
	id := req.PathParameter(ParamID)
	if len(id) <= 0 {
		logrus.Warnln("user id should not be null for change password operation")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("user id should not be null for update operation"), resp)
		return
	}

	document := bson.M{}
	decoder := json.NewDecoder(req.Request.Body)
	err := decoder.Decode(&document)
	if err != nil {
		logrus.Errorf("decode change password object err is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INTERNAL, err, resp)
		return
	}

	document, err = mejson.Unmarshal(document)
	if err != nil {
		logrus.Errorf("unmarshal change password obejct err is %v", err)
		response.WriteStatusError(services.COMMON_ERROR_INTERNAL, err, resp)
		return
	}

	password := document["password"]
	newpwd1 := document["newpassword"]
	newpwd2 := document["confirm_newpassword"]
	if password == nil || newpwd1 == nil || newpwd2 == nil {
		logrus.Errorln("invalid parameter! password and newpassword field should not be null")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("invalid parameter!password, newpassword and confirm_newpassword should not be null!"), resp)
		return
	}

	created, errorCode, err := services.GetUserService().UserChangePassword(token, id, password.(string), newpwd1.(string), newpwd2.(string))
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	p.successUpdate(id, created, req, resp)

}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:51,代碼來源:userHandler.go

示例15: TokenDetailHandler

func (p *Resource) TokenDetailHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("TokenDetailHandler is called!")

	token := req.HeaderParameter("X-Auth-Token")
	id := req.PathParameter(ParamID)
	if len(id) <= 0 {
		logrus.Warnln("token id should not be null for token detail operation")
		response.WriteStatusError(services.TOKEN_ERROR_GET, errors.New("token id should not be null for get user operation"), resp)
		return
	}

	ret, errorCode, err := services.GetTokenService().TokenDetail(token, id)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(ret, resp)
}
開發者ID:popsuper1982,項目名稱:DCOS_Cluster,代碼行數:19,代碼來源:tokenHandler.go


注:本文中的linkernetworks/com/linker_common_lib/rest/response.WriteStatusError函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。