Browse Source

update host name on join

Abhishek Kondur 2 years ago
parent
commit
18ddb3d5ca
3 changed files with 47 additions and 34 deletions
  1. 9 2
      controllers/node.go
  2. 37 0
      logic/hosts.go
  3. 1 32
      mq/handlers.go

+ 9 - 2
controllers/node.go

@@ -592,8 +592,15 @@ func createNode(w http.ResponseWriter, r *http.Request) {
 				logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 				return
 			}
-			logic.UpdateHost(&data.Host, host) // update the in memory struct values
-
+			logic.UpdateHostFromClient(&data.Host, host) // update the in memory struct values
+			logger.Log(0, fmt.Sprintf("HOST ----------> %+v\n", data.Host))
+			err = logic.UpsertHost(host)
+			if err != nil {
+				logger.Log(0, r.Header.Get("user"),
+					fmt.Sprintf("failed to update host [ %s ]: %v", host.ID.String(), err))
+				logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
+				return
+			}
 		} else {
 			logger.Log(0, "error creating host", err.Error())
 			logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))

+ 37 - 0
logic/hosts.go

@@ -133,6 +133,43 @@ func UpdateHost(newHost, currentHost *models.Host) {
 	if newHost.ProxyListenPort == 0 {
 		newHost.ProxyListenPort = currentHost.ProxyListenPort
 	}
+	if newHost.PublicListenPort == 0 {
+		newHost.PublicListenPort = currentHost.PublicListenPort
+	}
+}
+
+// UpdateHostFromClient - used for updating host on server with update recieved from client
+func UpdateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
+
+	if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
+		currHost.ListenPort = newHost.ListenPort
+		sendPeerUpdate = true
+	}
+	if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
+		currHost.ProxyListenPort = newHost.ProxyListenPort
+		sendPeerUpdate = true
+	}
+	if newHost.PublicListenPort != 0 && currHost.PublicListenPort != newHost.PublicListenPort {
+		currHost.PublicListenPort = newHost.PublicListenPort
+		sendPeerUpdate = true
+	}
+	if currHost.ProxyEnabled != newHost.ProxyEnabled {
+		currHost.ProxyEnabled = newHost.ProxyEnabled
+		sendPeerUpdate = true
+	}
+	if currHost.EndpointIP.String() != newHost.EndpointIP.String() {
+		currHost.EndpointIP = newHost.EndpointIP
+		sendPeerUpdate = true
+	}
+	currHost.DaemonInstalled = newHost.DaemonInstalled
+	currHost.Debug = newHost.Debug
+	currHost.Verbosity = newHost.Verbosity
+	currHost.Version = newHost.Version
+	if newHost.Name != "" {
+		currHost.Name = newHost.Name
+	}
+
+	return
 }
 
 // UpsertHost - upserts into DB a given host model, does not check for existence*

+ 1 - 32
mq/handlers.go

@@ -145,7 +145,7 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 		var sendPeerUpdate bool
 		switch hostUpdate.Action {
 		case models.UpdateHost:
-			sendPeerUpdate = updateHostFromClient(&hostUpdate.Host, currentHost)
+			sendPeerUpdate = logic.UpdateHostFromClient(&hostUpdate.Host, currentHost)
 			err := logic.UpsertHost(currentHost)
 			if err != nil {
 				logger.Log(0, "failed to update host: ", currentHost.ID.String(), err.Error())
@@ -183,37 +183,6 @@ func UpdateHost(client mqtt.Client, msg mqtt.Message) {
 	}(msg)
 }
 
-// used for updating host on server with update recieved from client
-func updateHostFromClient(newHost, currHost *models.Host) (sendPeerUpdate bool) {
-
-	if newHost.ListenPort != 0 && currHost.ListenPort != newHost.ListenPort {
-		currHost.ListenPort = newHost.ListenPort
-		sendPeerUpdate = true
-	}
-	if newHost.ProxyListenPort != 0 && currHost.ProxyListenPort != newHost.ProxyListenPort {
-		currHost.ProxyListenPort = newHost.ProxyListenPort
-		sendPeerUpdate = true
-	}
-	if newHost.PublicListenPort != 0 && currHost.PublicListenPort != newHost.PublicListenPort {
-		currHost.PublicListenPort = newHost.PublicListenPort
-		sendPeerUpdate = true
-	}
-	if currHost.ProxyEnabled != newHost.ProxyEnabled {
-		currHost.ProxyEnabled = newHost.ProxyEnabled
-		sendPeerUpdate = true
-	}
-	if currHost.EndpointIP.String() != newHost.EndpointIP.String() {
-		currHost.EndpointIP = newHost.EndpointIP
-		sendPeerUpdate = true
-	}
-	currHost.DaemonInstalled = newHost.DaemonInstalled
-	currHost.Debug = newHost.Debug
-	currHost.Verbosity = newHost.Verbosity
-	currHost.Version = newHost.Version
-	currHost.Name = newHost.Name
-	return
-}
-
 // UpdateMetrics  message Handler -- handles updates from client nodes for metrics
 func UpdateMetrics(client mqtt.Client, msg mqtt.Message) {
 	if servercfg.Is_EE {