fix(NET-117): force delete hosts and assoc nodes (#2432)

This commit is contained in:
Aceix 2023-07-10 10:03:59 +00:00 committed by GitHub
parent b212ae32d1
commit 68b8d7f600
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 5 deletions

View file

@ -199,6 +199,8 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
func deleteHost(w http.ResponseWriter, r *http.Request) {
var params = mux.Vars(r)
hostid := params["hostid"]
forceDelete := r.URL.Query().Get("force") == "true"
// confirm host exists
currHost, err := logic.GetHost(hostid)
if err != nil {
@ -206,7 +208,7 @@ func deleteHost(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
if err = logic.RemoveHost(currHost); err != nil {
if err = logic.RemoveHost(currHost, forceDelete); err != nil {
logger.Log(0, r.Header.Get("user"), "failed to delete a host:", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return

View file

@ -44,7 +44,7 @@ func TestCheckPorts(t *testing.T) {
//not sure why this initialization is required but without it
// RemoveHost returns database is closed
database.InitializeDatabase()
RemoveHost(&h)
RemoveHost(&h, true)
CreateHost(&h)
t.Run("no change", func(t *testing.T) {
is := is.New(t)

View file

@ -296,17 +296,26 @@ func UpsertHost(h *models.Host) error {
}
// RemoveHost - removes a given host from server
func RemoveHost(h *models.Host) error {
if len(h.Nodes) > 0 {
func RemoveHost(h *models.Host, forceDelete bool) error {
if !forceDelete && len(h.Nodes) > 0 {
return fmt.Errorf("host still has associated nodes")
}
if servercfg.IsUsingTurn() {
DeRegisterHostWithTurn(h.ID.String())
}
if len(h.Nodes) > 0 {
if err := DisassociateAllNodesFromHost(h.ID.String()); err != nil {
return err
}
}
err := database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
if err != nil {
return err
}
deleteHostFromCache(h.ID.String())
return nil
}

View file

@ -120,7 +120,7 @@ func ManageZombies(ctx context.Context, peerUpdate chan *models.Node) {
continue
}
if len(host.Nodes) == 0 {
if err := RemoveHost(host); err != nil {
if err := RemoveHost(host, true); err != nil {
logger.Log(0, "error deleting zombie host", host.ID.String(), err.Error())
}
}