Browse Source

publish dns update on host name change

Matthew R Kasun 2 years ago
parent
commit
5cac655f04
3 changed files with 44 additions and 4 deletions
  1. 13 0
      controllers/hosts.go
  2. 4 2
      models/dnsEntry.go
  3. 27 2
      mq/publishers.go

+ 13 - 0
controllers/hosts.go

@@ -109,6 +109,19 @@ func updateHost(w http.ResponseWriter, r *http.Request) {
 		if err := mq.PublishPeerUpdate(); err != nil {
 			logger.Log(0, "fail to publish peer update: ", err.Error())
 		}
+		if newHost.Name != currHost.Name {
+			networks := logic.GetHostNetworks(currHost.ID.String())
+			if err := mq.PublishHostDNSUpdate(currHost, newHost, networks); err != nil {
+				var dnsError *mq.DNSError
+				if errors.Is(err, dnsError) {
+					for _, message := range err.(mq.DNSError).ErrorStrings {
+						logger.Log(0, message)
+					}
+				} else {
+					logger.Log(0, err.Error())
+				}
+			}
+		}
 	}()
 
 	apiHostData := newHost.ConvertNMHostToAPI()

+ 4 - 2
models/dnsEntry.go

@@ -4,15 +4,17 @@ package models
 type DNSUpdateAction int
 
 const (
-	DNSDelete = iota
+	DNSDeleteByIP = iota
 	DNSDeleteByName
+	DNSReplaceName
+	DNSReplaceByIP
 	DNSInsert
-	DNSReplace
 )
 
 type DNSUpdate struct {
 	Action  DNSUpdateAction
 	Name    string
+	NewName string
 	Address string
 }
 

+ 27 - 2
mq/publishers.go

@@ -240,7 +240,7 @@ func PublishAllDNS(newnode *models.Node) error {
 
 func PublishDNSDelete(node *models.Node, host *models.Host) error {
 	dns := models.DNSUpdate{
-		Action: models.DNSDelete,
+		Action: models.DNSDeleteByIP,
 		Name:   host.Name + "." + node.Network,
 	}
 	if node.Address.IP != nil {
@@ -260,7 +260,7 @@ func PublishDNSDelete(node *models.Node, host *models.Host) error {
 
 func PublishReplaceDNS(oldNode, newNode *models.Node, host *models.Host) error {
 	dns := models.DNSUpdate{
-		Action: models.DNSReplace,
+		Action: models.DNSReplaceByIP,
 		Name:   host.Name + "." + oldNode.Network,
 	}
 	if !oldNode.Address.IP.Equal(newNode.Address.IP) {
@@ -329,6 +329,31 @@ func PublishCustomDNS(entry *models.DNSEntry) error {
 	return nil
 }
 
+type DNSError struct {
+	ErrorStrings []string
+}
+
+func (e DNSError) Error() string {
+	return "error publishing dns update"
+}
+func PublishHostDNSUpdate(old, new *models.Host, networks []string) error {
+	errors := DNSError{}
+	for _, network := range networks {
+		dns := models.DNSUpdate{
+			Action:  models.DNSReplaceName,
+			Name:    old.Name + "." + network,
+			NewName: new.Name + "." + network,
+		}
+		if err := PublishDNSUpdate(network, dns); err != nil {
+			errors.ErrorStrings = append(errors.ErrorStrings, err.Error())
+		}
+	}
+	if len(errors.ErrorStrings) > 0 {
+		return errors
+	}
+	return nil
+}
+
 // function to collect and store metrics for server nodes
 //func collectServerMetrics(networks []models.Network) {
 //	if !servercfg.Is_EE {