netmaker/controllers/relay.go

90 lines
2.6 KiB
Go
Raw Normal View History

package controller
import (
"encoding/json"
"fmt"
"net/http"
2021-09-18 22:33:14 +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"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mq"
)
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
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 {
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"))
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)
if err != nil {
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"))
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)
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())
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
2022-02-17 09:42:57 +08:00
runUpdates(&node, true)
}
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
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"]
netid := params["network"]
2022-02-08 00:30:15 +08:00
updatenodes, node, err := logic.DeleteRelay(netid, nodeid)
if err != nil {
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"))
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
}
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(node)
2022-02-17 09:42:57 +08:00
runUpdates(&node, true)
}