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

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

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