netmaker/models/network.go

117 lines
6 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package models
import (
2021-05-01 11:07:25 +08:00
// "../mongoconn"
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
2021-03-26 00:17:52 +08:00
)
//Network Struct
2021-03-26 00:17:52 +08:00
//At some point, need to replace all instances of Name with something else like Identifier
type Network struct {
2021-05-01 11:07:25 +08:00
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
AddressRange string `json:"addressrange" bson:"addressrange" validate:"required,cidr"`
// bug in validator --- required_with does not work with bools issue#683
// AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"required_with=isdualstack true,cidrv6"`
2021-05-01 19:57:49 +08:00
AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"addressrange6_valid"`
//can't have min=1 with omitempty
DisplayName string `json:"displayname,omitempty" bson:"displayname,omitempty" validate:"omitempty,alphanum,min=2,max=20,displayname_unique"`
NetID string `json:"netid" bson:"netid" validate:"required,alphanum,min=1,max=12,netid_valid"`
NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
DefaultInterface string `json:"defaultinterface" bson:"defaultinterface"`
DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
2021-05-03 19:13:12 +08:00
DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
DefaultSaveConfig *bool `json:"defaultsaveconfig" bson:"defaultsaveconfig"`
AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"`
AllowManualSignUp *bool `json:"allowmanualsignup" bson:"allowmanualsignup"`
IsLocal *bool `json:"islocal" bson:"islocal"`
IsDualStack *bool `json:"isdualstack" bson:"isdualstack"`
2021-05-26 00:48:04 +08:00
IsIPv4 string `json:"isipv4" bson:"isipv4"`
IsIPv6 string `json:"isipv6" bson:"isipv6"`
IsGRPCHub string `json:"isgrpchub" bson:"isgrpchub"`
2021-05-03 19:13:12 +08:00
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
//can't have min=1 with omitempty
DefaultCheckInInterval int32 `json:"checkininterval,omitempty" bson:"checkininterval,omitempty" validate:"omitempty,numeric,min=2,max=100000"`
}
type NetworkUpdate struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
AddressRange string `json:"addressrange" bson:"addressrange" validate:"omitempty,cidr"`
// bug in validator --- required_with does not work with bools issue#683
// AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"required_with=isdualstack true,cidrv6"`
AddressRange6 string `json:"addressrange6" bson:"addressrange6" validate:"omitempty,cidr"`
//can't have min=1 with omitempty
DisplayName string `json:"displayname,omitempty" bson:"displayname,omitempty" validate:"omitempty,alphanum,min=2,max=20"`
2021-05-04 05:51:38 +08:00
NetID string `json:"netid" bson:"netid" validate:"omitempty,alphanum,min=1,max=12"`
2021-05-03 19:13:12 +08:00
NodesLastModified int64 `json:"nodeslastmodified" bson:"nodeslastmodified"`
NetworkLastModified int64 `json:"networklastmodified" bson:"networklastmodified"`
DefaultInterface string `json:"defaultinterface" bson:"defaultinterface"`
DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
DefaultPostUp string `json:"defaultpostup" bson:"defaultpostup"`
2021-05-01 19:57:49 +08:00
DefaultPostDown string `json:"defaultpostdown" bson:"defaultpostdown"`
KeyUpdateTimeStamp int64 `json:"keyupdatetimestamp" bson:"keyupdatetimestamp"`
DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
DefaultSaveConfig *bool `json:"defaultsaveconfig" bson:"defaultsaveconfig"`
AccessKeys []AccessKey `json:"accesskeys" bson:"accesskeys"`
AllowManualSignUp *bool `json:"allowmanualsignup" bson:"allowmanualsignup"`
IsLocal *bool `json:"islocal" bson:"islocal"`
IsDualStack *bool `json:"isdualstack" bson:"isdualstack"`
LocalRange string `json:"localrange" bson:"localrange" validate:"omitempty,cidr"`
//can't have min=1 with omitempty
DefaultCheckInInterval int32 `json:"checkininterval,omitempty" bson:"checkininterval,omitempty" validate:"omitempty,numeric,min=2,max=100000"`
2021-03-26 00:17:52 +08:00
}
//TODO:
//Not sure if we need the below two functions. Got rid of one of the calls. May want to revisit
2021-05-01 11:07:25 +08:00
func (network *Network) SetNodesLastModified() {
network.NodesLastModified = time.Now().Unix()
2021-03-26 00:17:52 +08:00
}
2021-05-01 11:07:25 +08:00
func (network *Network) SetNetworkLastModified() {
network.NetworkLastModified = time.Now().Unix()
2021-03-26 00:17:52 +08:00
}
2021-05-01 11:07:25 +08:00
func (network *Network) SetDefaults() {
if network.DisplayName == "" {
network.DisplayName = network.NetID
}
if network.DefaultInterface == "" {
network.DefaultInterface = "nm-" + network.NetID
}
if network.DefaultListenPort == 0 {
network.DefaultListenPort = 51821
}
if network.DefaultPostDown == "" {
2021-03-26 00:17:52 +08:00
2021-05-01 11:07:25 +08:00
}
if network.DefaultSaveConfig == nil {
defaultsave := true
network.DefaultSaveConfig = &defaultsave
}
if network.DefaultKeepalive == 0 {
network.DefaultKeepalive = 20
}
if network.DefaultPostUp == "" {
}
//Check-In Interval for Nodes, In Seconds
if network.DefaultCheckInInterval == 0 {
network.DefaultCheckInInterval = 30
}
if network.AllowManualSignUp == nil {
signup := false
network.AllowManualSignUp = &signup
}
2021-05-26 00:48:04 +08:00
if (network.IsDualStack != nil) && *network.IsDualStack {
network.IsIPv6 = "yes"
network.IsIPv4 = "yes"
} else if network.IsGRPCHub != "yes" {
network.IsIPv6 = "no"
network.IsIPv4 = "yes"
}
2021-03-26 00:17:52 +08:00
}