예제 #1
0
func GetAgents(ctx *Context, query model.GetAgentsQuery) {
	query.OrgId = ctx.OrgId
	agents, err := sqlstore.GetAgents(&query)
	if err != nil {
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}
	ctx.JSON(200, rbody.OkResp("agents", agents))
}
예제 #2
0
func AddAgent(ctx *Context, agent model.AgentDTO) {
	if !agent.ValidName() {
		ctx.JSON(400, "invalde agent Name. must match /^[0-9a-Z_-]+$/")
		return
	}
	agent.Id = 0
	//need to add suport for middelware context with AUTH/
	agent.OrgId = ctx.OrgId
	err := sqlstore.AddAgent(&agent)
	if err != nil {
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}
	ctx.JSON(200, rbody.OkResp("agent", agent))
}
예제 #3
0
func GetAgentById(ctx *Context) {
	id := ctx.ParamsInt64(":id")
	owner := ctx.OrgId
	agent, err := sqlstore.GetAgentById(id, owner)
	if err == model.AgentNotFound {
		ctx.JSON(200, rbody.ErrResp(404, fmt.Errorf("agent not found")))
		return
	}
	if err != nil {
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}

	ctx.JSON(200, rbody.OkResp("agent", agent))
}
예제 #4
0
func DeleteAgent(ctx *Context) {
	id := ctx.ParamsInt64(":id")
	owner := ctx.OrgId
	err := sqlstore.DeleteAgent(id, owner)
	if err != nil {
		if err == model.AgentNotFound {
			ctx.JSON(200, rbody.ErrResp(404, fmt.Errorf("agent not found")))
			return
		}
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}

	ActiveSockets.CloseSocketByAgentId(id)

	ctx.JSON(200, rbody.OkResp("agent", nil))
}
예제 #5
0
func UpdateAgent(ctx *Context, agent model.AgentDTO) {
	if !agent.ValidName() {
		ctx.JSON(200, rbody.ErrResp(400, fmt.Errorf("invalid agent Name. must match /^[0-9a-Z_-]+$/")))
		return
	}
	if agent.Id == 0 {
		ctx.JSON(200, rbody.ErrResp(400, fmt.Errorf("agent ID not set.")))
		return
	}
	//need to add suport for middelware context with AUTH/
	agent.OrgId = ctx.OrgId
	err := sqlstore.UpdateAgent(&agent)
	if err != nil {
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}
	ctx.JSON(200, rbody.OkResp("agent", agent))
}
예제 #6
0
func GetAgentMetrics(ctx *Context) {
	id := ctx.ParamsInt64(":id")
	owner := ctx.OrgId
	agent, err := sqlstore.GetAgentById(id, owner)
	if err != nil {
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}
	if agent == nil {
		ctx.JSON(200, rbody.ErrResp(404, fmt.Errorf("agent not found")))
		return
	}
	metrics, err := sqlstore.GetAgentMetrics(agent)
	if err != nil {
		log.Error(3, err.Error())
		ctx.JSON(200, rbody.ErrResp(500, err))
		return
	}

	ctx.JSON(200, rbody.OkResp("metrics", metrics))
}
예제 #7
0
func heartbeat(ctx *macaron.Context) {
	ctx.JSON(200, rbody.OkResp("heartbeat", nil))
}