Browse Source

NET-383: Set Additional Host Fields From Client Side (#2566)

* update static,mtu from client side host update

* update host fields if host exists already on registration
Abhishek K 1 year ago
parent
commit
3f1211795c
2 changed files with 17 additions and 3 deletions
  1. 14 0
      controllers/enrollmentkeys.go
  2. 3 3
      logic/hosts.go

+ 14 - 0
controllers/enrollmentkeys.go

@@ -13,6 +13,7 @@ import (
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/mq"
 	"github.com/gravitl/netmaker/servercfg"
+	"golang.org/x/exp/slog"
 )
 
 func enrollmentKeyHandlers(r *mux.Router) {
@@ -223,6 +224,19 @@ func handleHostRegister(w http.ResponseWriter, r *http.Request) {
 			}
 		}
 		enrollmentKey.Networks = networksToAdd
+		currHost, err := logic.GetHost(newHost.ID.String())
+		if err != nil {
+			slog.Error("failed registration", "hostID", newHost.ID.String(), "hostName", newHost.Name, "error", err.Error())
+			logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
+			return
+		}
+		logic.UpdateHostFromClient(&newHost, currHost)
+		err = logic.UpsertHost(currHost)
+		if err != nil {
+			slog.Error("failed to update host", "id", currHost.ID, "error", err)
+			logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
+			return
+		}
 	}
 	// ready the response
 	server := servercfg.GetServerInfo()

+ 3 - 3
logic/hosts.go

@@ -242,9 +242,9 @@ func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool)
 	currHost.Debug = newHost.Debug
 	currHost.Verbosity = newHost.Verbosity
 	currHost.Version = newHost.Version
-	if newHost.Name != "" {
-		currHost.Name = newHost.Name
-	}
+	currHost.IsStatic = newHost.IsStatic
+	currHost.MTU = newHost.MTU
+	currHost.Name = newHost.Name
 	if len(newHost.NatType) > 0 && newHost.NatType != currHost.NatType {
 		currHost.NatType = newHost.NatType
 		sendPeerUpdate = true