mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-13 08:34:44 +08:00
GRA-988: add host to network
This commit is contained in:
parent
be2a730a24
commit
99a5cc3307
2 changed files with 40 additions and 62 deletions
|
@ -2,6 +2,8 @@ package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
@ -10,7 +12,6 @@ import (
|
||||||
"github.com/gravitl/netmaker/logic"
|
"github.com/gravitl/netmaker/logic"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
"github.com/gravitl/netmaker/mq"
|
"github.com/gravitl/netmaker/mq"
|
||||||
"github.com/gravitl/netmaker/servercfg"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type hostNetworksUpdatePayload struct {
|
type hostNetworksUpdatePayload struct {
|
||||||
|
@ -21,7 +22,7 @@ func hostHandlers(r *mux.Router) {
|
||||||
r.HandleFunc("/api/hosts", logic.SecurityCheck(true, http.HandlerFunc(getHosts))).Methods(http.MethodGet)
|
r.HandleFunc("/api/hosts", logic.SecurityCheck(true, http.HandlerFunc(getHosts))).Methods(http.MethodGet)
|
||||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods(http.MethodPut)
|
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods(http.MethodPut)
|
||||||
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods(http.MethodDelete)
|
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods(http.MethodDelete)
|
||||||
r.HandleFunc("/api/hosts/{hostid}/networks", logic.SecurityCheck(true, http.HandlerFunc(updateHostNetworks))).Methods(http.MethodPut)
|
r.HandleFunc("/api/hosts/{hostid}/networks/{network}", logic.SecurityCheck(true, http.HandlerFunc(addHostToNetwork))).Methods(http.MethodPost)
|
||||||
r.HandleFunc("/api/hosts/{hostid}/relay", logic.SecurityCheck(false, http.HandlerFunc(createHostRelay))).Methods(http.MethodPost)
|
r.HandleFunc("/api/hosts/{hostid}/relay", logic.SecurityCheck(false, http.HandlerFunc(createHostRelay))).Methods(http.MethodPost)
|
||||||
r.HandleFunc("/api/hosts/{hostid}/relay", logic.SecurityCheck(false, http.HandlerFunc(deleteHostRelay))).Methods(http.MethodDelete)
|
r.HandleFunc("/api/hosts/{hostid}/relay", logic.SecurityCheck(false, http.HandlerFunc(deleteHostRelay))).Methods(http.MethodDelete)
|
||||||
}
|
}
|
||||||
|
@ -171,9 +172,9 @@ func deleteHost(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(apiHostData)
|
json.NewEncoder(w).Encode(apiHostData)
|
||||||
}
|
}
|
||||||
|
|
||||||
// swagger:route PUT /api/hosts hosts updateHostNetworks
|
// swagger:route POST /api/hosts/{hostid}/networks/{network} hosts addHostToNetwork
|
||||||
//
|
//
|
||||||
// Given a list of networks, a host is updated accordingly.
|
// Given a network, a host is added to the network.
|
||||||
//
|
//
|
||||||
// Schemes: https
|
// Schemes: https
|
||||||
//
|
//
|
||||||
|
@ -181,41 +182,43 @@ func deleteHost(w http.ResponseWriter, r *http.Request) {
|
||||||
// oauth
|
// oauth
|
||||||
//
|
//
|
||||||
// Responses:
|
// Responses:
|
||||||
// 200: updateHostNetworks
|
// 200: addHostToNetwork
|
||||||
func updateHostNetworks(w http.ResponseWriter, r *http.Request) {
|
func addHostToNetwork(w http.ResponseWriter, r *http.Request) {
|
||||||
var payload hostNetworksUpdatePayload
|
|
||||||
err := json.NewDecoder(r.Body).Decode(&payload)
|
|
||||||
if err != nil {
|
|
||||||
logger.Log(0, r.Header.Get("user"), "failed to update host networks:", err.Error())
|
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// confirm host exists
|
|
||||||
var params = mux.Vars(r)
|
var params = mux.Vars(r)
|
||||||
hostid := params["hostid"]
|
hostid := params["hostid"]
|
||||||
|
network := params["network"]
|
||||||
|
if hostid == "" || network == "" {
|
||||||
|
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("hostid or network cannot be empty"), "badrequest"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// confirm host exists
|
||||||
currHost, err := logic.GetHost(hostid)
|
currHost, err := logic.GetHost(hostid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Log(0, r.Header.Get("user"), "failed to find host:", err.Error())
|
logger.Log(0, r.Header.Get("user"), "failed to find host:", hostid, err.Error())
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = logic.UpdateHostNetworks(currHost, servercfg.GetServer(), payload.Networks[:]); err != nil {
|
newNode, err := logic.UpdateHostNetwork(currHost, network, true)
|
||||||
logger.Log(0, r.Header.Get("user"), "failed to update host networks:", err.Error())
|
if err != nil {
|
||||||
|
logger.Log(0, r.Header.Get("user"), "failed to add host to network:", hostid, network, err.Error())
|
||||||
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
logger.Log(1, "added new node", newNode.ID.String(), "to host", currHost.Name)
|
||||||
|
|
||||||
|
networks := logic.GetHostNetworks(currHost.ID.String())
|
||||||
|
if len(networks) > 0 {
|
||||||
if err = mq.ModifyClient(&mq.MqClient{
|
if err = mq.ModifyClient(&mq.MqClient{
|
||||||
ID: currHost.ID.String(),
|
ID: currHost.ID.String(),
|
||||||
Text: currHost.Name,
|
Text: currHost.Name,
|
||||||
Networks: payload.Networks,
|
Networks: networks,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
logger.Log(0, r.Header.Get("user"), "failed to update host networks roles in DynSec:", err.Error())
|
logger.Log(0, r.Header.Get("user"), "failed to update host networks roles in DynSec:", hostid, err.Error())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Log(2, r.Header.Get("user"), "updated host networks", currHost.Name)
|
logger.Log(2, r.Header.Get("user"), fmt.Sprintf("added host %s to network %s", currHost.Name, network))
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
json.NewEncoder(w).Encode(payload)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/gravitl/netmaker/database"
|
"github.com/gravitl/netmaker/database"
|
||||||
"github.com/gravitl/netmaker/logger"
|
"github.com/gravitl/netmaker/logger"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
|
"github.com/gravitl/netmaker/servercfg"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -156,46 +157,20 @@ func RemoveHostByID(hostID string) error {
|
||||||
return database.DeleteRecord(database.HOSTS_TABLE_NAME, hostID)
|
return database.DeleteRecord(database.HOSTS_TABLE_NAME, hostID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateHostNetworks - updates a given host's networks
|
// UpdateHostNetwork - adds/deletes host from a network
|
||||||
func UpdateHostNetworks(h *models.Host, server string, nets []string) error {
|
func UpdateHostNetwork(h *models.Host, network string, add bool) (*models.Node, error) {
|
||||||
if len(h.Nodes) > 0 {
|
|
||||||
for i := range h.Nodes {
|
|
||||||
n, err := GetNodeByID(h.Nodes[i])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// loop through networks and remove need for updating existing networks
|
|
||||||
found := false
|
|
||||||
for j := range nets {
|
|
||||||
if len(nets[j]) > 0 && nets[j] == n.Network {
|
|
||||||
nets[j] = "" // mark as ignore
|
|
||||||
found = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found { // remove the node/host from that network
|
|
||||||
if err = DissasociateNodeFromHost(&n, h); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
h.Nodes = []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range nets {
|
if add {
|
||||||
// create a node for each non zero network remaining
|
|
||||||
if len(nets[i]) > 0 {
|
|
||||||
newNode := models.Node{}
|
newNode := models.Node{}
|
||||||
newNode.Server = server
|
newNode.Server = servercfg.GetServer()
|
||||||
newNode.Network = nets[i]
|
newNode.Network = network
|
||||||
if err := AssociateNodeToHost(&newNode, h); err != nil {
|
if err := AssociateNodeToHost(&newNode, h); err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
|
||||||
logger.Log(1, "added new node", newNode.ID.String(), "to host", h.Name)
|
|
||||||
}
|
}
|
||||||
|
return &newNode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil, errors.New("failed to update host networks")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssociateNodeToHost - associates and creates a node with a given host
|
// AssociateNodeToHost - associates and creates a node with a given host
|
||||||
|
|
Loading…
Add table
Reference in a new issue