Skip to content

Commit

Permalink
Add handler that sends MQ msg to upgrade a host (#2582)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielseibel1 authored Oct 2, 2023
1 parent bfc61fa commit 23ad85b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
17 changes: 17 additions & 0 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
24 changes: 13 additions & 11 deletions models/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23ad85b

Please sign in to comment.