Exemple #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))
}
Exemple #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))
}
Exemple #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))
}
Exemple #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))
}
Exemple #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))
}
Exemple #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))
}
Exemple #7
0
func heartbeat(ctx *macaron.Context) {
	ctx.JSON(200, rbody.OkResp("heartbeat", nil))
}