Merge pull request #1870 from gravitl/story/GRA-793

Add default hosts to network upon creation
This commit is contained in:
dcarns 2022-12-23 17:09:03 -05:00 committed by GitHub
commit afc1812ef3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View file

@ -447,6 +447,19 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
event.Commands, err.Error()))
}
// add default hosts to network
defaultHosts := logic.GetDefaultHosts()
for i := range defaultHosts {
newNode := models.Node{}
newNode.Network = network.NetID
newNode.Server = servercfg.GetServer()
if err = logic.AssociateNodeToHost(&newNode, &defaultHosts[i]); err != nil {
logger.Log(0, "error occurred when adding network", network.NetID, "to host", defaultHosts[i].Name)
}
}
// TODO: Send message notifying host of new peers/network conf
logger.Log(1, r.Header.Get("user"), "created network", network.NetID)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(network)

View file

@ -246,3 +246,18 @@ func DissasociateNodeFromHost(n *models.Node, h *models.Host) error {
h.Nodes = RemoveStringSlice(h.Nodes, index)
return UpsertHost(h)
}
// GetDefaultHosts - retrieve all hosts marked as default from DB
func GetDefaultHosts() []models.Host {
defaultHostList := []models.Host{}
hosts, err := GetAllHosts()
if err != nil {
return defaultHostList
}
for i := range hosts {
if hosts[i].IsDefault {
defaultHostList = append(defaultHostList, hosts[i])
}
}
return defaultHostList[:]
}