relay.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package ee_controllers
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "github.com/gorilla/mux"
  8. controller "github.com/gravitl/netmaker/controllers"
  9. "github.com/gravitl/netmaker/logger"
  10. "github.com/gravitl/netmaker/logic"
  11. "github.com/gravitl/netmaker/models"
  12. "github.com/gravitl/netmaker/mq"
  13. )
  14. // RelayHandlers - handle EE Relays
  15. func RelayHandlers(r *mux.Router) {
  16. r.HandleFunc("/api/nodes/{network}/{nodeid}/createrelay", controller.Authorize(false, true, "user", http.HandlerFunc(createRelay))).Methods(http.MethodPost)
  17. r.HandleFunc("/api/nodes/{network}/{nodeid}/deleterelay", controller.Authorize(false, true, "user", http.HandlerFunc(deleteRelay))).Methods(http.MethodDelete)
  18. }
  19. // swagger:route POST /api/nodes/{network}/{nodeid}/createrelay nodes createRelay
  20. //
  21. // Create a relay.
  22. //
  23. // Schemes: https
  24. //
  25. // Security:
  26. // oauth
  27. //
  28. // Responses:
  29. // 200: nodeResponse
  30. func createRelay(w http.ResponseWriter, r *http.Request) {
  31. var relayRequest models.RelayRequest
  32. var params = mux.Vars(r)
  33. w.Header().Set("Content-Type", "application/json")
  34. err := json.NewDecoder(r.Body).Decode(&relayRequest)
  35. if err != nil {
  36. logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
  37. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  38. return
  39. }
  40. relayRequest.NetID = params["network"]
  41. relayRequest.NodeID = params["nodeid"]
  42. _, relayNode, err := logic.CreateRelay(relayRequest)
  43. if err != nil {
  44. logger.Log(0, r.Header.Get("user"),
  45. fmt.Sprintf("failed to create relay on node [%s] on network [%s]: %v", relayRequest.NodeID, relayRequest.NetID, err))
  46. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
  47. return
  48. }
  49. go mq.PublishPeerUpdate()
  50. logger.Log(1, r.Header.Get("user"), "created relay on node", relayRequest.NodeID, "on network", relayRequest.NetID)
  51. apiNode := relayNode.ConvertToAPINode()
  52. w.WriteHeader(http.StatusOK)
  53. json.NewEncoder(w).Encode(apiNode)
  54. }
  55. // swagger:route DELETE /api/nodes/{network}/{nodeid}/deleterelay nodes deleteRelay
  56. //
  57. // Remove a relay.
  58. //
  59. // Schemes: https
  60. //
  61. // Security:
  62. // oauth
  63. //
  64. // Responses:
  65. // 200: nodeResponse
  66. func deleteRelay(w http.ResponseWriter, r *http.Request) {
  67. w.Header().Set("Content-Type", "application/json")
  68. var params = mux.Vars(r)
  69. nodeid := params["nodeid"]
  70. netid := params["network"]
  71. updateNodes, node, err := logic.DeleteRelay(netid, nodeid)
  72. if err != nil {
  73. logger.Log(0, r.Header.Get("user"), "error decoding request body: ", err.Error())
  74. logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
  75. return
  76. }
  77. logger.Log(1, r.Header.Get("user"), "deleted relay server", nodeid, "on network", netid)
  78. go func() {
  79. for _, relayedNode := range updateNodes {
  80. err = mq.NodeUpdate(&relayedNode)
  81. if err != nil {
  82. logger.Log(1, "relayed node update ", relayedNode.ID.String(), "on network", relayedNode.Network, ": ", err.Error())
  83. }
  84. h, err := logic.GetHost(relayedNode.HostID.String())
  85. if err == nil {
  86. if h.OS == models.OS_Types.IoT {
  87. node.IsRelay = true // for iot update to recognise that it has to delete relay peer
  88. if err = mq.PublishSingleHostPeerUpdate(context.Background(), h, &node, nil); err != nil {
  89. logger.Log(1, "failed to publish peer update to host", h.ID.String(), ": ", err.Error())
  90. }
  91. }
  92. }
  93. }
  94. mq.PublishPeerUpdate()
  95. }()
  96. logger.Log(1, r.Header.Get("user"), "deleted relay on node", node.ID.String(), "on network", node.Network)
  97. apiNode := node.ConvertToAPINode()
  98. w.WriteHeader(http.StatusOK)
  99. json.NewEncoder(w).Encode(apiNode)
  100. }