コード例 #1
0
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
}
コード例 #2
0
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
}