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)