Refactor Route IDs (#1891)

This commit is contained in:
Viktor Liu
2024-05-06 14:47:49 +02:00
committed by GitHub
parent 6a4935139d
commit 4e7c17756c
25 changed files with 320 additions and 292 deletions

22
route/hauniqueid.go Normal file
View File

@@ -0,0 +1,22 @@
package route
import "strings"
type HAUniqueID string
// GetHAUniqueID returns a highly available route ID by combining Network ID and Network range address
func GetHAUniqueID(input *Route) HAUniqueID {
return HAUniqueID(string(input.NetID) + "-" + input.Network.String())
}
func (id HAUniqueID) String() string {
return string(id)
}
// NetID returns the Network ID from the HAUniqueID
func (id HAUniqueID) NetID() NetID {
if i := strings.LastIndex(string(id), "-"); i != -1 {
return NetID(id[:i])
}
return NetID(id)
}

View File

@@ -36,6 +36,12 @@ const (
IPv6Network
)
type ID string
type NetID string
type HAMap map[HAUniqueID][]*Route
// NetworkType route network type
type NetworkType int
@@ -65,11 +71,11 @@ func ToPrefixType(prefix string) NetworkType {
// Route represents a route
type Route struct {
ID string `gorm:"primaryKey"`
ID ID `gorm:"primaryKey"`
// AccountID is a reference to Account that this object belongs
AccountID string `gorm:"index"`
Network netip.Prefix `gorm:"serializer:json"`
NetID string
NetID NetID
Description string
Peer string
PeerGroups []string `gorm:"serializer:json"`
@@ -165,8 +171,3 @@ func compareList(list, other []string) bool {
return true
}
// GetHAUniqueID returns a highly available route ID by combining Network ID and Network range address
func GetHAUniqueID(input *Route) string {
return input.NetID + "-" + input.Network.String()
}