GRA-985: host deletion logic in mq handler

This commit is contained in:
Abhishek Kondur
2023-01-17 15:26:54 +05:30
parent a9d1df6022
commit 12f4656c62

View File

@@ -151,6 +151,11 @@ func RemoveHost(h *models.Host) error {
return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String()) 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 // UpdateHostNetworks - updates a given host's networks
func UpdateHostNetworks(h *models.Host, server string, nets []string) error { func UpdateHostNetworks(h *models.Host, server string, nets []string) error {
if len(h.Nodes) > 0 { if len(h.Nodes) > 0 {
@@ -241,6 +246,28 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error {
return UpsertHost(h) 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 // GetDefaultHosts - retrieve all hosts marked as default from DB
func GetDefaultHosts() []models.Host { func GetDefaultHosts() []models.Host {
defaultHostList := []models.Host{} defaultHostList := []models.Host{}