2021-09-14 08:25:27 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-07-12 17:38:15 +08:00
|
|
|
"fmt"
|
2021-09-14 08:25:27 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route POST /api/nodes/{network}/{nodeid}/createrelay nodes createRelay
|
|
|
|
//
|
|
|
|
// Create a relay.
|
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: nodeResponse
|
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 {
|
2022-07-12 17:38:15 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
|
2022-09-15 01:26:31 +08:00
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
2021-09-14 08:25:27 +08:00
|
|
|
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 {
|
2022-07-12 17:38:15 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"),
|
|
|
|
fmt.Sprintf("failed to create relay on node [%s] on network [%s]: %v", relay.NodeID, relay.NetID, err))
|
2022-09-15 01:26:31 +08:00
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
|
2021-09-14 08:25:27 +08:00
|
|
|
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-04-20 04:18:03 +08:00
|
|
|
logger.Log(1, "error sending update to relayed node ", relayedNode.Name, "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
|
|
|
}
|
|
|
|
|
2022-09-06 20:20:24 +08:00
|
|
|
// swagger:route DELETE /api/nodes/{network}/{nodeid}/deleterelay nodes deleteRelay
|
|
|
|
//
|
|
|
|
// Remove a relay.
|
|
|
|
//
|
|
|
|
// Schemes: https
|
|
|
|
//
|
|
|
|
// Security:
|
|
|
|
// oauth
|
2022-09-11 12:51:59 +08:00
|
|
|
//
|
|
|
|
// Responses:
|
|
|
|
// 200: nodeResponse
|
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 {
|
2022-07-12 17:38:15 +08:00
|
|
|
logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
|
2022-09-15 01:26:31 +08:00
|
|
|
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
|
2021-10-06 03:02:09 +08:00
|
|
|
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 {
|
2022-04-20 04:18:03 +08:00
|
|
|
logger.Log(1, "error sending update to relayed node ", relayedNode.Name, "on network", netid, ": ", err.Error())
|
2022-02-08 00:30:15 +08:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|