2021-09-14 08:25:27 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2021-09-18 22:33:14 +08:00
|
|
|
|
2021-09-14 08:25:27 +08:00
|
|
|
"github.com/gorilla/mux"
|
2021-12-07 04:31:08 +08:00
|
|
|
"github.com/gravitl/netmaker/logger"
|
2021-10-27 00:27:29 +08:00
|
|
|
"github.com/gravitl/netmaker/logic"
|
2021-09-14 08:25:27 +08:00
|
|
|
"github.com/gravitl/netmaker/models"
|
2022-02-05 00:54:50 +08:00
|
|
|
"github.com/gravitl/netmaker/mq"
|
2021-09-14 08:25:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func createRelay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var relay models.RelayRequest
|
|
|
|
var params = mux.Vars(r)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&relay)
|
|
|
|
if err != nil {
|
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
relay.NetID = params["network"]
|
2022-01-11 06:52:21 +08:00
|
|
|
relay.NodeID = params["nodeid"]
|
2022-02-08 00:30:15 +08:00
|
|
|
updatenodes, node, err := logic.CreateRelay(relay)
|
2021-09-14 08:25:27 +08:00
|
|
|
if err != nil {
|
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
2021-12-07 04:31:08 +08:00
|
|
|
logger.Log(1, r.Header.Get("user"), "created relay on node", relay.NodeID, "on network", relay.NetID)
|
2022-02-08 00:30:15 +08:00
|
|
|
for _, relayedNode := range updatenodes {
|
|
|
|
err = mq.NodeUpdate(&relayedNode)
|
2022-02-05 00:54:50 +08:00
|
|
|
if err != nil {
|
2022-02-08 00:30:15 +08:00
|
|
|
logger.Log(1, "error sending update to relayed node ", relayedNode.Address, "on network", relay.NetID, ": ", err.Error())
|
2022-02-05 00:54:50 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-14 08:25:27 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(node)
|
2022-02-17 09:42:57 +08:00
|
|
|
runUpdates(&node, true)
|
2021-09-14 08:25:27 +08:00
|
|
|
}
|
|
|
|
|
2021-10-06 03:02:09 +08:00
|
|
|
func deleteRelay(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
var params = mux.Vars(r)
|
2022-01-11 06:52:21 +08:00
|
|
|
nodeid := params["nodeid"]
|
2021-10-06 03:02:09 +08:00
|
|
|
netid := params["network"]
|
2022-02-08 00:30:15 +08:00
|
|
|
updatenodes, node, err := logic.DeleteRelay(netid, nodeid)
|
2021-10-06 03:02:09 +08:00
|
|
|
if err != nil {
|
|
|
|
returnErrorResponse(w, r, formatError(err, "internal"))
|
|
|
|
return
|
|
|
|
}
|
2022-01-25 04:16:29 +08:00
|
|
|
logger.Log(1, r.Header.Get("user"), "deleted relay server", nodeid, "on network", netid)
|
2022-02-08 00:30:15 +08:00
|
|
|
for _, relayedNode := range updatenodes {
|
|
|
|
err = mq.NodeUpdate(&relayedNode)
|
|
|
|
if err != nil {
|
|
|
|
logger.Log(1, "error sending update to relayed node ", relayedNode.Address, "on network", netid, ": ", err.Error())
|
|
|
|
}
|
|
|
|
}
|
2021-10-06 03:02:09 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
json.NewEncoder(w).Encode(node)
|
2022-02-17 09:42:57 +08:00
|
|
|
runUpdates(&node, true)
|
2021-10-06 03:02:09 +08:00
|
|
|
}
|