From b624155467c2cd8bc88d9016f6accb234ec038c5 Mon Sep 17 00:00:00 2001 From: gabrielseibel1 Date: Fri, 8 Sep 2023 13:59:04 -0300 Subject: [PATCH] Add handler that sends MQ msg to upgrade a host --- controllers/hosts.go | 17 +++++++++++++++++ models/host.go | 24 +++++++++++++----------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/controllers/hosts.go b/controllers/hosts.go index 0c6e9fd65..8b434a501 100644 --- a/controllers/hosts.go +++ b/controllers/hosts.go @@ -23,6 +23,7 @@ func hostHandlers(r *mux.Router) { 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}", Authorize(true, false, "all", http.HandlerFunc(deleteHost))).Methods(http.MethodDelete) + r.HandleFunc("/api/hosts/{hostid}/upgrade", logic.SecurityCheck(true, http.HandlerFunc(upgradeHost))).Methods(http.MethodPut) r.HandleFunc("/api/hosts/{hostid}/networks/{network}", logic.SecurityCheck(true, http.HandlerFunc(addHostToNetwork))).Methods(http.MethodPost) r.HandleFunc("/api/hosts/{hostid}/networks/{network}", logic.SecurityCheck(true, http.HandlerFunc(deleteHostFromNetwork))).Methods(http.MethodDelete) r.HandleFunc("/api/hosts/adm/authenticate", authenticateHost).Methods(http.MethodPost) @@ -31,6 +32,22 @@ func hostHandlers(r *mux.Router) { r.HandleFunc("/api/v1/auth-register/host", socketHandler) } +// upgrade host is a handler to send upgrade message to a host +func upgradeHost(w http.ResponseWriter, r *http.Request) { + host, err := logic.GetHost(mux.Vars(r)["hostid"]) + if err != nil { + slog.Error("failed to find host", "error", err) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "notfound")) + return + } + if err := mq.HostUpdate(&models.HostUpdate{Action: models.Upgrade, Host: *host}); err != nil { + slog.Error("failed to upgrade host", "error", err) + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) + return + } + logic.ReturnSuccessResponse(w, r, "passed message to upgrade host") +} + // swagger:route GET /api/hosts hosts getHosts // // Lists all hosts. diff --git a/models/host.go b/models/host.go index 2ab90e16d..bfab10027 100644 --- a/models/host.go +++ b/models/host.go @@ -89,26 +89,28 @@ func ParseBool(s string) bool { type HostMqAction string const ( + // Upgrade - const to request host to update it's client + Upgrade HostMqAction = "UPGRADE" // SignalHost - const for host signal action - SignalHost = "SIGNAL_HOST" + SignalHost HostMqAction = "SIGNAL_HOST" // UpdateHost - constant for host update action - UpdateHost = "UPDATE_HOST" + UpdateHost HostMqAction = "UPDATE_HOST" // DeleteHost - constant for host delete action - DeleteHost = "DELETE_HOST" + DeleteHost HostMqAction = "DELETE_HOST" // JoinHostToNetwork - constant for host network join action - JoinHostToNetwork = "JOIN_HOST_TO_NETWORK" + JoinHostToNetwork HostMqAction = "JOIN_HOST_TO_NETWORK" // Acknowledgement - ACK response for hosts - Acknowledgement = "ACK" + Acknowledgement HostMqAction = "ACK" // RequestAck - request an ACK - RequestAck = "REQ_ACK" + RequestAck HostMqAction = "REQ_ACK" // CheckIn - update last check in times and public address and interfaces - CheckIn = "CHECK_IN" - // REGISTER_WITH_TURN - registers host with turn server if configured - RegisterWithTurn = "REGISTER_WITH_TURN" + CheckIn HostMqAction = "CHECK_IN" + // RegisterWithTurn - registers host with turn server if configured + RegisterWithTurn HostMqAction = "REGISTER_WITH_TURN" // UpdateKeys - update wireguard private/public keys - UpdateKeys = "UPDATE_KEYS" + UpdateKeys HostMqAction = "UPDATE_KEYS" // RequestPull - request a pull from a host - RequestPull = "REQ_PULL" + RequestPull HostMqAction = "REQ_PULL" ) // SignalAction - turn peer signal action