Browse Source

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

Aceix 2 years ago
parent
commit
68b8d7f600
4 changed files with 16 additions and 5 deletions
  1. 3 1
      controllers/hosts.go
  2. 1 1
      logic/host_test.go
  3. 11 2
      logic/hosts.go
  4. 1 1
      logic/zombie.go

+ 3 - 1
controllers/hosts.go

@@ -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

+ 1 - 1
logic/host_test.go

@@ -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)

+ 11 - 2
logic/hosts.go

@@ -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
 }

+ 1 - 1
logic/zombie.go

@@ -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())
 						}
 					}