mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-13 08:34:44 +08:00
fix(NET-117): force delete hosts and assoc nodes (#2432)
This commit is contained in:
parent
b212ae32d1
commit
68b8d7f600
4 changed files with 16 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue