mirror of
https://github.com/gravitl/netmaker.git
synced 2024-09-21 07:46:04 +08:00
Merge pull request #1870 from gravitl/story/GRA-793
Add default hosts to network upon creation
This commit is contained in:
commit
afc1812ef3
|
@ -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)
|
||||
|
|
|
@ -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[:]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue