diff --git a/controllers/hosts.go b/controllers/hosts.go index 764fe5c21..f712c25d1 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -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) +} diff --git a/models/host.go b/models/host.go index 70947ba29..5b99b9b74 100644 --- a/models/host.go +++ b/models/host.go @@ -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