|
@@ -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)
|
|
|
+}
|