Browse Source

GRA-985: host deletion logic in mq handler

Abhishek Kondur 2 years ago
parent
commit
12f4656c62
1 changed files with 27 additions and 0 deletions
  1. 27 0
      logic/hosts.go

+ 27 - 0
logic/hosts.go

@@ -151,6 +151,11 @@ func RemoveHost(h *models.Host) error {
 	return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String())
 }
 
+// RemoveHostByID - removes a given host by id from server
+func RemoveHostByID(hostID string) error {
+	return database.DeleteRecord(database.HOSTS_TABLE_NAME, hostID)
+}
+
 // UpdateHostNetworks - updates a given host's networks
 func UpdateHostNetworks(h *models.Host, server string, nets []string) error {
 	if len(h.Nodes) > 0 {
@@ -241,6 +246,28 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error {
 	return UpsertHost(h)
 }
 
+// DisassociateAllNodesFromHost - deletes all nodes of the host
+func DisassociateAllNodesFromHost(hostID string) error {
+	host, err := GetHost(hostID)
+	if err != nil {
+		return err
+	}
+	for _, nodeID := range host.Nodes {
+		node, err := GetNodeByID(nodeID)
+		if err != nil {
+			logger.Log(0, "failed to get host node", err.Error())
+			continue
+		}
+		if err := DeleteNode(&node, true); err != nil {
+			logger.Log(0, "failed to delete node", node.ID.String(), err.Error())
+			continue
+		}
+		logger.Log(3, "deleted node", node.ID.String(), "of host", host.ID.String())
+	}
+	host.Nodes = []string{}
+	return UpsertHost(host)
+}
+
 // GetDefaultHosts - retrieve all hosts marked as default from DB
 func GetDefaultHosts() []models.Host {
 	defaultHostList := []models.Host{}