2022-12-20 03:55:24 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gravitl/netmaker/logger"
|
|
|
|
"github.com/gravitl/netmaker/logic"
|
|
|
|
"github.com/gravitl/netmaker/models"
|
|
|
|
)
|
|
|
|
|
2022-12-20 07:04:55 +08:00
|
|
|
type hostNetworksUpdatePayload struct {
|
|
|
|
Networks []string `json:"networks"`
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:55:24 +08:00
|
|
|
func hostHandlers(r *mux.Router) {
|
|
|
|
r.HandleFunc("/api/hosts", logic.SecurityCheck(false, http.HandlerFunc(getHosts))).Methods("GET")
|
2022-12-23 18:12:17 +08:00
|
|
|
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods("PUT")
|
2022-12-20 03:55:24 +08:00
|
|
|
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods("DELETE")
|
2022-12-23 18:12:17 +08:00
|
|
|
r.HandleFunc("/api/hosts/{hostid}/networks", logic.SecurityCheck(true, http.HandlerFunc(updateHostNetworks))).Methods("PUT")
|
2022-12-20 03:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:route GET /api/hosts hosts getHosts
|
|
|
|
//
|
|
|
|
// Lists all hosts.
|
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: getHostsSliceResponse
|
|
|
|
func getHosts(w http.ResponseWriter, r *http.Request) {
|
|
|
|
currentHosts, err := logic.GetAllHosts()
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to fetch hosts: ", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
2022-12-23 04:44:42 +08:00
|
|
|
// return JSON/API formatted hosts
|
|
|
|
apiHosts := logic.GetAllHostsAPI(currentHosts[:])
|
2022-12-20 03:55:24 +08:00
|
|
|
logger.Log(2, r.Header.Get("user"), "fetched all hosts")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-12-23 04:44:42 +08:00
|
|
|
json.NewEncoder(w).Encode(apiHosts)
|
2022-12-20 03:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:route PUT /api/hosts/{hostid} hosts updateHost
|
|
|
|
//
|
|
|
|
// Updates a Netclient host on Netmaker server.
|
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: updateHostResponse
|
|
|
|
func updateHost(w http.ResponseWriter, r *http.Request) {
|
2022-12-23 04:44:42 +08:00
|
|
|
var newHostData models.ApiHost
|
2022-12-20 03:55:24 +08:00
|
|
|
err := json.NewDecoder(r.Body).Decode(&newHostData)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to update a host:", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// confirm host exists
|
2022-12-23 04:44:42 +08:00
|
|
|
currHost, err := logic.GetHost(newHostData.ID)
|
2022-12-20 03:55:24 +08:00
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to update a host:", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 04:44:42 +08:00
|
|
|
newHost := newHostData.ConvertAPIHostToNMHost(currHost)
|
|
|
|
|
|
|
|
logic.UpdateHost(newHost, currHost) // update the in memory struct values
|
|
|
|
if err = logic.UpsertHost(newHost); err != nil {
|
2022-12-20 03:55:24 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to update a host:", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 04:44:42 +08:00
|
|
|
apiHostData := newHost.ConvertNMHostToAPI()
|
|
|
|
logger.Log(2, r.Header.Get("user"), "updated host", newHost.ID.String())
|
2022-12-20 03:55:24 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-12-23 04:44:42 +08:00
|
|
|
json.NewEncoder(w).Encode(apiHostData)
|
2022-12-20 03:55:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// swagger:route DELETE /api/hosts/{hostid} hosts deleteHost
|
|
|
|
//
|
|
|
|
// Deletes a Netclient host from Netmaker server.
|
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: deleteHostResponse
|
|
|
|
func deleteHost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var params = mux.Vars(r)
|
|
|
|
hostid := params["hostid"]
|
|
|
|
// confirm host exists
|
|
|
|
currHost, err := logic.GetHost(hostid)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to delete a host:", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = logic.RemoveHost(currHost); err != nil {
|
|
|
|
logger.Log(0, r.Header.Get("user"), "failed to delete a host:", err.Error())
|
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-23 04:44:42 +08:00
|
|
|
apiHostData := currHost.ConvertNMHostToAPI()
|
2022-12-20 03:55:24 +08:00
|
|
|
logger.Log(2, r.Header.Get("user"), "removed host", currHost.Name)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2022-12-23 04:44:42 +08:00
|
|
|
json.NewEncoder(w).Encode(apiHostData)
|
2022-12-20 03:55:24 +08:00
|
|
|
}
|
2022-12-20 07:04:55 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-12-23 04:44:42 +08:00
|
|
|
// TODO: add and remove hosts to networks (nodes)
|
|
|
|
|
|
|
|
logger.Log(2, r.Header.Get("user"), "updated host networks", currHost.Name)
|
2022-12-20 07:04:55 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(payload)
|
|
|
|
}
|