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
}
Example #2
0
// 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

}
//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
}
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

}
Example #5
0
// 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)
}
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

}
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
}
// 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
}
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
}
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
}
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
}
Example #12
0
// 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)

}
Example #13
0
// 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)
}
Example #14
0
// 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)

}
Example #15
0
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)
}
func (p *Resource) UserClusterDeleteHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("UserClusterDeleteHandler 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
	}

	userId := req.QueryParameter("user_id")

	code, err = services.GetClusterService().DeleteByUserId(userId, x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}
	// Write success response
	response.WriteSuccess(resp)
	return
}
//Send cluster owner an email of endpoint
func (p *Resource) EmailSendHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("EmailSendHandler 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
	}

	clusterId := req.PathParameter(ParamID)

	errorCode, err := services.GetEmailService().SendClusterDeployedEmail(clusterId, x_auth_token)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}
	// Write success response
	response.WriteSuccess(resp)
	return
}
Example #18
0
func (p *Resource) UserValidateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserValidateHandler is called!")
	token := req.HeaderParameter("X-Auth-Token")
	username := req.QueryParameter("username")
	if len(username) == 0 {
		logrus.Warnln("username should not be null")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("username should not be null"), resp)
		return
	}
	logrus.Infof("username is %v", username)
	logrus.Infof("start to test username")

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

	response.WriteSuccess(resp)

}
//check username and clustername
func (p *Resource) ClusterNameCheckHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("ClusterNameCheckHandler 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
	}

	userId := req.QueryParameter("userid")
	clusterName := req.QueryParameter("clustername")

	errorCode, err := services.GetClusterService().CheckClusterName(userId, clusterName, x_auth_token)

	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}
	// Write success response
	response.WriteSuccess(resp)
	return
}
Example #20
0
// RoleListHandler parses the http request and return the exist roles.
// Usage :
//		GET /v1/role
// If successful,response code will be set to 201.
func (p *Resource) RoleListHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("RoleListHandler is called!")
	token := req.HeaderParameter("X-Auth-Token")

	ret, count, errorCode, err := services.GetRoleService().RoleList(token)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	p.successList(ret, 0, count, req, resp)

}
func (p *Resource) ClusterGetHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("ClusterGetHandler 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
	}

	objectId := req.PathParameter(ParamID)
	cluster, code, err := services.GetClusterService().QueryById(objectId, x_auth_token)
	if err != nil {
		response.WriteStatusError(code, err, resp)
		return
	}
	logrus.Debugf("cluster is %v", cluster)

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

}
Example #22
0
// CheckAndGenerateToken parses the http request and registry a new user.
// Usage :
//		POST /v1/user/registry
// If successful,response code will be set to 201.
func (p *Resource) UserCreateUserHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserCreateUserHandler is called!")

	token := req.HeaderParameter("X-Auth-Token")
	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.COMMON_ERROR_INVALIDATE, err, resp)
		return
	}

	username, email, password, company, paraErr := userRegistryParamCheck(doc)
	if paraErr != nil {
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, paraErr, resp)
		return
	}

	if len(email) == 0 || len(password) == 0 || len(username) == 0 {
		logrus.Errorln("parameter can not be null!")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("Invalid parameter"), resp)
		return
	}

	userParam := services.UserParam{
		UserName: username,
		Email:    email,
		Password: password,
		Company:  company}
	errorCode, userId, err := services.GetUserService().Create(userParam, token)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	p.successUpdate(userId, true, req, resp)

}
Example #23
0
// TokenReGenerateHandler parses the http request and generate
// another user's token from current token.
// Usage :
//		POST /v1/token/regenerate
// If successful,response code will be set to 201.
func (p *Resource) TokenReGenerateHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("TokenReGenerateHandler is called!")
	token := req.HeaderParameter("X-Auth-Token")

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

	document, err = mejson.Unmarshal(document)
	if err != nil {
		logrus.Errorf("unmarshal token generate err is %v", err)
		response.WriteStatusError(services.TOKEN_ERROR_CREATE, err, resp)
		return
	}

	userid := document["user_id"]
	tenantid := document["tenant_id"]
	if userid == nil || tenantid == nil {
		logrus.Errorln("invalid parameter! user and tenant should not be null!")
		response.WriteStatusError(services.COMMON_ERROR_INVALIDATE, errors.New("invalid parameter! user and tenant should not be null!"), resp)
		return
	}

	tokenId, errorCode, err := services.GetTokenService().TokenReGenerate(token, userid.(string), tenantid.(string))
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(tokenId, resp)

	return
}
func (p *Resource) ClustersDeleteHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("UserClusterDeleteHandler 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
	}

	body := DeleteClustersRequestBody{}
	err = json.NewDecoder(req.Request.Body).Decode(&body)

	errorCode, err := services.GetClusterService().DeleteClusters(body.ClusterIds, x_auth_token)
	if err != nil {
		logrus.Errorln("delete clusters error , [%v]", err)
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	// Write success response
	response.WriteSuccess(resp)
	return
}
func (p *Resource) TenantIdHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infoln("TenantIdHandler is called!")

	token := req.HeaderParameter("X-Auth-Token")
	userId := req.QueryParameter("userId")

	ret, errorCode, err := services.GetTenantService().GetTenantId(token, userId)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	response.WriteResponse(ret, resp)
}
func (p *Resource) HostGetHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("HostGetHandler 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
	}

	hostId := req.PathParameter(ParamHostID)

	host, code, err := services.GetHostService().QueryById(hostId, x_auth_token)
	if err != nil {
		logrus.Errorln("get host error is %v", err)
		response.WriteStatusError(code, err, resp)
		return
	}

	res := response.QueryStruct{Success: true, Data: host}
	resp.WriteEntity(res)
	return
}
Example #27
0
// UserListHandler parses the http request and return the user items.
// Usage :
//		GET /v1/user
//		GET /v1/user/{ParamID}
// Params :
//		ParamID : storage identifier of user
// If successful,response code will be set to 201.
func (p *Resource) UserListHandler(req *restful.Request, resp *restful.Response) {
	logrus.Infof("UserListHandler is called!")

	token := req.HeaderParameter("X-Auth-Token")
	limitnum := queryIntParam(req, "limit", 10)
	skipnum := queryIntParam(req, "skip", 0)
	sort := req.QueryParameter("sort")

	ret, count, errorCode, err := services.GetUserService().UserList(token, limitnum, skipnum, sort)
	if err != nil {
		response.WriteStatusError(errorCode, err, resp)
		return
	}

	p.successList(ret, limitnum, count, req, resp)
}