Skip to content

Commit

Permalink
feat(NET-449): add sync feature to request a host pull from server
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceix committed Aug 2, 2023
1 parent 0c70c4d commit e3610f8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
42 changes: 42 additions & 0 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"github.com/gravitl/netmaker/mq"
"github.com/gravitl/netmaker/servercfg"
"golang.org/x/crypto/bcrypt"
"golang.org/x/exp/slog"
)

func hostHandlers(r *mux.Router) {
r.HandleFunc("/api/hosts", logic.SecurityCheck(false, http.HandlerFunc(getHosts))).Methods(http.MethodGet)
r.HandleFunc("/api/hosts/keys", logic.SecurityCheck(true, http.HandlerFunc(updateAllKeys))).Methods(http.MethodPut)
r.HandleFunc("/api/hosts/{hostid}/keys", logic.SecurityCheck(true, http.HandlerFunc(updateKeys))).Methods(http.MethodPut)
r.HandleFunc("/api/hosts/{hostid}/sync", logic.SecurityCheck(true, http.HandlerFunc(syncHost))).Methods(http.MethodPost)
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(updateHost))).Methods(http.MethodPut)
r.HandleFunc("/api/hosts/{hostid}", logic.SecurityCheck(true, http.HandlerFunc(deleteHost))).Methods(http.MethodDelete)
r.HandleFunc("/api/hosts/{hostid}/networks/{network}", logic.SecurityCheck(true, http.HandlerFunc(addHostToNetwork))).Methods(http.MethodPost)
Expand Down Expand Up @@ -583,3 +585,43 @@ func updateKeys(w http.ResponseWriter, r *http.Request) {
logger.Log(2, r.Header.Get("user"), "updated key on host", host.Name)
w.WriteHeader(http.StatusOK)
}

// swagger:route POST /api/hosts/{hostId}/sync host syncHost
//
// Requests a host to pull.
//
// Schemes: https
//
// Security:
// oauth
//
// Responses:
// 200: networkBodyResponse
func syncHost(w http.ResponseWriter, r *http.Request) {
hostId := mux.Vars(r)["hostid"]

var errorResponse = models.ErrorResponse{}
w.Header().Set("Content-Type", "application/json")

host, err := logic.GetHost(hostId)
if err != nil {
slog.Error("failed to retrieve host", "user", r.Header.Get("user"), "error", err)
errorResponse.Code = http.StatusBadRequest
errorResponse.Message = err.Error()
logic.ReturnErrorResponse(w, r, errorResponse)
return
}

go func() {
hostUpdate := models.HostUpdate{
Action: models.RequestPull,
Host: *host,
}
if err = mq.HostUpdate(&hostUpdate); err != nil {
slog.Error("failed to send host pull request", "host", host.ID.String(), "error", err)
}
}()

slog.Info("requested host pull", "user", r.Header.Get("user"), "host", host.ID)
w.WriteHeader(http.StatusOK)
}
2 changes: 2 additions & 0 deletions models/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const (
RegisterWithTurn = "REGISTER_WITH_TURN"
// UpdateKeys - update wireguard private/public keys
UpdateKeys = "UPDATE_KEYS"
// RequestPull - request a pull from a host
RequestPull = "REQ_PULL"
)

// SignalAction - turn peer signal action
Expand Down

0 comments on commit e3610f8

Please sign in to comment.