From 9188dd2cdfa2aa7b8e2c1830794d4687663d5d1a Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Mon, 19 Dec 2022 18:04:55 -0500 Subject: [PATCH] initial commit --- controllers/hosts.go | 41 ++++++++++++++++++++++++++++++++++++++++- logic/hosts.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index cd8f6c00..493580d6 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -10,11 +10,15 @@ import ( "github.com/gravitl/netmaker/models" ) +type hostNetworksUpdatePayload struct { + Networks []string `json:"networks"` +} + func hostHandlers(r *mux.Router) { r.HandleFunc("/api/hosts", logic.SecurityCheck(false, http.HandlerFunc(getHosts))).Methods("GET") r.HandleFunc("/api/hosts", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods("PUT") r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods("DELETE") - // r.HandleFunc("/api/hosts/{hostid}/{network}", logic.SecurityCheck(false, http.HandlerFunc(getHosts))).Methods("PUT") + r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHostNetworks))).Methods("PUT") } // swagger:route GET /api/hosts hosts getHosts @@ -112,3 +116,38 @@ func deleteHost(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(currHost) } + +// swagger:route PUT /api/hosts hosts updateHostNetworks +// +// Given a list of networks, a host is updated accordingly. +// +// Schemes: https +// +// Security: +// oauth +// +// Responses: +// 200: updateHostNetworks +func updateHostNetworks(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) + hostid := params["hostid"] + currHost, err := logic.GetHost(hostid) + if err != nil { + logger.Log(0, r.Header.Get("user"), "failed to find host:", err.Error()) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) + return + } + + logger.Log(2, r.Header.Get("user"), "updated host", currHost.Name) + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(payload) +} diff --git a/logic/hosts.go b/logic/hosts.go index 1627f3e6..56433606 100644 --- a/logic/hosts.go +++ b/logic/hosts.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/gravitl/netmaker/database" + "github.com/gravitl/netmaker/logger" "github.com/gravitl/netmaker/models" ) @@ -137,3 +138,40 @@ func RemoveHost(h *models.Host) error { } return database.DeleteRecord(database.HOSTS_TABLE_NAME, h.ID.String()) } + +// UpdateHostNetworks - updates a given host's networks +func UpdateHostNetworks(h *models.Host, nets []string) 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 = DeleteNodeByID(&n); err != nil { + return err + } + } + } + } else { + h.Nodes = []string{} + } + + for i := range nets { + // create a node for each non zero network remaining + if len(nets[i]) > 0 { + // TODO create a node with given hostid + logger.Log(0, "I will create a node here") + } + } + + return nil +}