fetch related hosts on relay creation to enable proxy

This commit is contained in:
Abhishek Kondur 2023-01-10 18:54:52 +05:30
parent beb0b1adf2
commit 01d28e6483
3 changed files with 34 additions and 24 deletions

View file

@ -124,23 +124,17 @@ func createHostRelay(w http.ResponseWriter, r *http.Request) {
}
logger.Log(1, r.Header.Get("user"), "created relay on host", relay.HostID)
// for _, relayedHost := range relayedHosts {
// err = mq.PublishSingleHostUpdate(&relayedHost)
// if err != nil {
// logger.Log(1, "error sending update to relayed host ", relayedHost.ID.String(), ": ", err.Error())
// }
// }
// // publish host update for relayhost
// err = mq.PublishSingleHostUpdate(relayHost)
// if err != nil {
// logger.Log(1, "error sending update to relay host ", relayHost.ID.String(), ": ", err.Error())
// }
go func() {
go func(relayHostID string) {
relatedhosts := logic.GetRelatedHosts(relayHostID)
for _, relatedHost := range relatedhosts {
relatedHost.ProxyEnabled = true
logic.UpsertHost(&relatedHost)
}
if err := mq.PublishPeerUpdate(); err != nil {
logger.Log(0, "fail to publish peer update: ", err.Error())
}
}()
}(relay.HostID)
apiHostData := relayHost.ConvertNMHostToAPI()
w.WriteHeader(http.StatusOK)
@ -169,16 +163,6 @@ func deleteHostRelay(w http.ResponseWriter, r *http.Request) {
return
}
logger.Log(1, r.Header.Get("user"), "deleted relay host", hostid)
// for _, relayedHost := range relayedHosts {
// err = mq.PublishSingleHostUpdate(&relayedHost)
// if err != nil {
// logger.Log(1, "error sending update to relayed host ", relayedHost.ID.String(), ": ", err.Error())
// }
// }
// err = mq.PublishSingleHostUpdate(relayHost)
// if err != nil {
// logger.Log(1, "error sending update to relayed host ", relayHost.ID.String(), ": ", err.Error())
// }
go func() {
if err := mq.PublishPeerUpdate(); err != nil {
logger.Log(0, "fail to publish peer update: ", err.Error())

View file

@ -295,3 +295,26 @@ func GetHostNetworks(hostID string) []string {
}
return nets
}
// GetRelatedHosts - fetches related hosts of a given host
func GetRelatedHosts(hostID string) []models.Host {
relatedHosts := []models.Host{}
networks := GetHostNetworks(hostID)
networkMap := make(map[string]struct{})
for _, network := range networks {
networkMap[network] = struct{}{}
}
hosts, err := GetAllHosts()
if err == nil {
for _, host := range hosts {
networks := GetHostNetworks(host.ID.String())
for _, network := range networks {
if _, ok := networkMap[network]; ok {
relatedHosts = append(relatedHosts, host)
break
}
}
}
}
return relatedHosts
}

View file

@ -48,6 +48,7 @@ func CreateRelay(relay models.RelayRequest) ([]models.Node, models.Node, error)
return returnnodes, node, nil
}
// CreateHostRelay - creates a host relay
func CreateHostRelay(relay models.HostRelayRequest) (relayHost *models.Host, relayedHosts []models.Host, err error) {
relayHost, err = GetHost(relay.HostID)
@ -69,6 +70,7 @@ func CreateHostRelay(relay models.HostRelayRequest) (relayHost *models.Host, rel
return
}
// SetRelayedHosts - updates the relayed hosts status
func SetRelayedHosts(setRelayed bool, relayHostID string, relayedHostIDs []string) []models.Host {
var relayedHosts []models.Host
for _, relayedHostID := range relayedHostIDs {
@ -133,6 +135,7 @@ func GetRelayedNodes(relayNode *models.Node) ([]models.Node, error) {
return returnnodes, nil
}
// GetRelayedHosts - gets the relayed hosts of a relay host
func GetRelayedHosts(relayHost *models.Host) []models.Host {
relayedHosts := []models.Host{}