diff --git a/modules/agent/api/host.go b/modules/agent/api/host.go index 3d1b15a1..f9aeffeb 100644 --- a/modules/agent/api/host.go +++ b/modules/agent/api/host.go @@ -34,7 +34,7 @@ func (h *APIHandler) enrollHost(w http.ResponseWriter, req *http.Request, ps htt return } errors := util.MapStr{} - for _, hi := range reqBody { + for i, hi := range reqBody { var ( hostInfo *host.HostInfo ) @@ -74,7 +74,13 @@ func (h *APIHandler) enrollHost(w http.ResponseWriter, req *http.Request, ps htt continue } hostInfo.Timestamp = time.Now() - err = orm.Create(nil, hostInfo) + var ctx *orm.Context + if i == len(reqBody) - 1 { + ctx = &orm.Context{ + Refresh: "wait_for", + } + } + err = orm.Create(ctx, hostInfo) if err != nil { errors[hi.IP] = util.MapStr{ "error": err.Error(), @@ -94,6 +100,26 @@ func (h *APIHandler) enrollHost(w http.ResponseWriter, req *http.Request, ps htt h.WriteJSON(w, resBody, http.StatusOK) } +func (h *APIHandler) deleteHost(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + hostID := ps.MustGetParameter("host_id") + hostInfo, err := getHost(hostID) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + ctx := orm.Context{ + Refresh: "wait_for", + } + err = orm.Delete(&ctx, hostInfo) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + h.WriteDeletedOKJSON(w, hostID) +} + func (h *APIHandler) GetHostAgentInfo(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { hostID := ps.MustGetParameter("host_id") hostInfo, err := getHost(hostID) @@ -123,7 +149,7 @@ func (h *APIHandler) GetHostAgentInfo(w http.ResponseWriter, req *http.Request, "host_id": hostID, "agent_id": ag.ID, "version": ag.Version, - "status": ag.Status, + "status": hostInfo.AgentStatus, "endpoint": ag.GetEndpoint(), }, http.StatusOK) } diff --git a/modules/agent/api/init.go b/modules/agent/api/init.go index 63f6d27e..2937f97a 100644 --- a/modules/agent/api/init.go +++ b/modules/agent/api/init.go @@ -29,6 +29,7 @@ func Init() { api.HandleAPIMethod(api.POST, "/host/_enroll", handler.enrollHost) api.HandleAPIMethod(api.GET, "/host/:host_id/agent/info",handler.GetHostAgentInfo) api.HandleAPIMethod(api.GET, "/host/:host_id/processes",handler.GetHostElasticProcess) + api.HandleAPIMethod(api.DELETE, "/host/:host_id",handler.deleteHost) api.HandleAPIMethod(api.POST, "/agent/install_command", handler.RequireLogin(handler.generateInstallCommand))