From 24063bb2ae30649ac82a9c2a4b758032c4d8052e Mon Sep 17 00:00:00 2001 From: liugq Date: Tue, 27 Jun 2023 14:43:58 +0800 Subject: [PATCH] remove old agent when endpoint exists --- modules/agent/api/instance.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/modules/agent/api/instance.go b/modules/agent/api/instance.go index 50aba75f..9941bfc4 100644 --- a/modules/agent/api/instance.go +++ b/modules/agent/api/instance.go @@ -59,8 +59,7 @@ func (h *APIHandler) createInstance(w http.ResponseWriter, req *http.Request, ps port = "8080" } obj.Endpoint = fmt.Sprintf("https://%s:%s", remoteIP, port) - obj.Tags = append(obj.Tags, "mtls") - obj.Tags = append(obj.Tags, "auto") + obj.Tags = append(obj.Tags, "mtls", "auto") } //fetch more information of agent instance @@ -101,6 +100,25 @@ func (h *APIHandler) createInstance(w http.ResponseWriter, req *http.Request, ps log.Error(errMsg) return } + if token != "" { + err, result := orm.GetBy("endpoint", obj.Endpoint, oldInst) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + if len(result.Result) > 0 { + if m, ok := result.Result[0].(map[string]interface{}); ok { + if id, ok := m["id"].(string); ok { + oldInst.ID = id + err = orm.Delete(nil, oldInst) + if err != nil { + log.Error(err) + } + } + } + } + } obj.Status = model.StatusOnline err = orm.Create(nil, obj)