feat(NET-449): add sync feature to request a host pull from server (#2491)

This commit is contained in:
Aceix 2023-08-04 07:52:47 +00:00 committed by GitHub
parent 922e7dbf2c
commit 596cc7a945
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View file

@ -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)
@ -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)
}

View file

@ -107,6 +107,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