Sfoglia il codice sorgente

add name field to network model

abhishek9686 10 mesi fa
parent
commit
e9c13f7fc3
4 ha cambiato i file con 32 aggiunte e 33 eliminazioni
  1. 1 1
      controllers/network.go
  2. 10 9
      logic/networks.go
  3. 2 2
      migrate/migrate.go
  4. 19 21
      models/network.go

+ 1 - 1
controllers/network.go

@@ -488,7 +488,7 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	if len(network.NetID) > 32 {
+	if len(network.Name) > 32 {
 		err := errors.New("network name shouldn't exceed 32 characters")
 		logger.Log(0, r.Header.Get("user"), "failed to create network: ",
 			err.Error())

+ 10 - 9
logic/networks.go

@@ -122,22 +122,22 @@ func getNetworksFromCache() (networks []models.Network) {
 	return
 }
 
-func deleteNetworkFromCache(key uuid.UUID) {
+func deleteNetworkFromCache(key string) {
 	networkCacheMutex.Lock()
-	delete(networkCacheMap, key.String())
+	delete(networkCacheMap, key)
 	networkCacheMutex.Unlock()
 }
 
-func getNetworkFromCache(key uuid.UUID) (network models.Network, ok bool) {
+func getNetworkFromCache(key string) (network models.Network, ok bool) {
 	networkCacheMutex.RLock()
-	network, ok = networkCacheMap[key.String()]
+	network, ok = networkCacheMap[key]
 	networkCacheMutex.RUnlock()
 	return
 }
 
-func storeNetworkInCache(key uuid.UUID, network models.Network) {
+func storeNetworkInCache(key string, network models.Network) {
 	networkCacheMutex.Lock()
-	networkCacheMap[key.String()] = network
+	networkCacheMap[key] = network
 	networkCacheMutex.Unlock()
 }
 
@@ -163,7 +163,7 @@ func GetNetworks() ([]models.Network, error) {
 		// add network our array
 		networks = append(networks, network)
 		if servercfg.CacheEnabled() {
-			storeNetworkInCache(network.ID, network)
+			storeNetworkInCache(network.NetID, network)
 		}
 	}
 
@@ -205,7 +205,7 @@ func DeleteNetwork(network string) error {
 
 // CreateNetwork - creates a network in database
 func CreateNetwork(network models.Network) (models.Network, error) {
-	network.ID = uuid.New()
+	network.NetID = uuid.New().String()
 	if network.AddressRange != "" {
 		normalizedRange, err := NormalizeCIDR(network.AddressRange)
 		if err != nil {
@@ -515,9 +515,10 @@ func UpsertNetwork(net *models.Network) error {
 	err = database.Insert(net.NetID, string(data), database.NETWORKS_TABLE_NAME)
 	if err == nil {
 		if servercfg.CacheEnabled() {
-			storeNetworkInCache(net.ID.String(), *net)
+			storeNetworkInCache(net.NetID, *net)
 		}
 	}
+	return nil
 }
 
 // GetNetwork - gets a network from database

+ 2 - 2
migrate/migrate.go

@@ -162,8 +162,8 @@ func updateNetworks() {
 	}
 	for _, netI := range nets {
 		netI := netI
-		if netI.ID == uuid.Nil {
-			netI.ID = uuid.New()
+		if netI.Name == "" {
+			netI.Name = netI.NetID
 			logic.UpsertNetwork(&netI)
 		}
 	}

+ 19 - 21
models/network.go

@@ -3,32 +3,30 @@ package models
 import (
 	"net"
 	"time"
-
-	"github.com/google/uuid"
 )
 
 // Network Struct - contains info for a given unique network
 // At  some point, need to replace all instances of Name with something else like  Identifier
 type Network struct {
-	ID                  uuid.UUID `json:"id"`
-	AddressRange        string    `json:"addressrange" bson:"addressrange" validate:"omitempty,cidrv4"`
-	AddressRange6       string    `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidrv6"`
-	NetID               string    `json:"netid" bson:"netid" validate:"required,min=1,max=32,netid_valid"`
-	NodesLastModified   int64     `json:"nodeslastmodified" bson:"nodeslastmodified"`
-	NetworkLastModified int64     `json:"networklastmodified" bson:"networklastmodified"`
-	DefaultInterface    string    `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=35"`
-	DefaultListenPort   int32     `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
-	NodeLimit           int32     `json:"nodelimit" bson:"nodelimit"`
-	DefaultPostDown     string    `json:"defaultpostdown" bson:"defaultpostdown"`
-	DefaultKeepalive    int32     `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
-	AllowManualSignUp   string    `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
-	IsIPv4              string    `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
-	IsIPv6              string    `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
-	DefaultUDPHolePunch string    `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
-	DefaultMTU          int32     `json:"defaultmtu" bson:"defaultmtu"`
-	DefaultACL          string    `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
-	CreatedBy           string    `json:"created_by"`
-	CreatedAt           string    `json:"created_at"`
+	Name                string `json:"name"`
+	AddressRange        string `json:"addressrange" bson:"addressrange" validate:"omitempty,cidrv4"`
+	AddressRange6       string `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidrv6"`
+	NetID               string `json:"netid" bson:"netid" validate:"required,min=1,max=32,netid_valid"`
+	NodesLastModified   int64  `json:"nodeslastmodified" bson:"nodeslastmodified"`
+	NetworkLastModified int64  `json:"networklastmodified" bson:"networklastmodified"`
+	DefaultInterface    string `json:"defaultinterface" bson:"defaultinterface" validate:"min=1,max=35"`
+	DefaultListenPort   int32  `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
+	NodeLimit           int32  `json:"nodelimit" bson:"nodelimit"`
+	DefaultPostDown     string `json:"defaultpostdown" bson:"defaultpostdown"`
+	DefaultKeepalive    int32  `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
+	AllowManualSignUp   string `json:"allowmanualsignup" bson:"allowmanualsignup" validate:"checkyesorno"`
+	IsIPv4              string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
+	IsIPv6              string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
+	DefaultUDPHolePunch string `json:"defaultudpholepunch" bson:"defaultudpholepunch" validate:"checkyesorno"`
+	DefaultMTU          int32  `json:"defaultmtu" bson:"defaultmtu"`
+	DefaultACL          string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
+	CreatedBy           string `json:"created_by"`
+	CreatedAt           string `json:"created_at"`
 }
 
 // SaveData - sensitive fields of a network that should be kept the same