Skip to content

Commit

Permalink
remove unused fields from network model
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkasun committed Aug 16, 2023
1 parent f6dded0 commit 780c8a8
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 118 deletions.
10 changes: 0 additions & 10 deletions cli/cmd/network/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,12 @@ var networkCreateCmd = &cobra.Command{
network.AddressRange6 = address6
network.IsIPv6 = "yes"
}
if udpHolePunch {
network.DefaultUDPHolePunch = "yes"
}
if defaultACL {
network.DefaultACL = "yes"
}
network.DefaultInterface = defaultInterface
network.DefaultListenPort = int32(defaultListenPort)
network.NodeLimit = int32(nodeLimit)
network.DefaultKeepalive = int32(defaultKeepalive)
if allowManualSignUp {
network.AllowManualSignUp = "yes"
}
network.DefaultMTU = int32(defaultMTU)
}
functions.PrettyPrint(functions.CreateNetwork(network))
Expand All @@ -57,13 +50,10 @@ func init() {
networkCreateCmd.MarkFlagsMutuallyExclusive("file", "name")
networkCreateCmd.Flags().StringVar(&address, "ipv4_addr", "", "IPv4 address of the network")
networkCreateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 address of the network")
networkCreateCmd.Flags().BoolVar(&udpHolePunch, "udp_hole_punch", false, "Enable UDP Hole Punching ?")
networkCreateCmd.Flags().BoolVar(&defaultACL, "default_acl", false, "Enable default Access Control List ?")
networkCreateCmd.Flags().StringVar(&defaultInterface, "interface", "", "Name of the network interface")
networkCreateCmd.Flags().IntVar(&defaultListenPort, "listen_port", 51821, "Default wireguard port each node will attempt to use")
networkCreateCmd.Flags().IntVar(&nodeLimit, "node_limit", 999999999, "Maximum number of nodes that can be associated with this network")
networkCreateCmd.Flags().IntVar(&defaultKeepalive, "keep_alive", 20, "Keep Alive in seconds")
networkCreateCmd.Flags().IntVar(&defaultMTU, "mtu", 1280, "MTU size")
networkCreateCmd.Flags().BoolVar(&allowManualSignUp, "manual_signup", false, "Allow manual signup ?")
rootCmd.AddCommand(networkCreateCmd)
}
3 changes: 0 additions & 3 deletions cli/cmd/network/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ var (
netID string
address string
address6 string
udpHolePunch bool
defaultACL bool
defaultInterface string
defaultListenPort int
nodeLimit int
defaultKeepalive int
allowManualSignUp bool
defaultMTU int
)
5 changes: 2 additions & 3 deletions cli/cmd/network/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ var networkListCmd = &cobra.Command{
functions.PrettyPrint(networks)
default:
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"NetId", "Address Range (IPv4)", "Address Range (IPv6)", "Network Last Modified", "Nodes Last Modified"})
table.SetHeader([]string{"NetId", "Address Range (IPv4)", "Address Range (IPv6)", "Nodes Last Modified"})
for _, n := range *networks {
networkLastModified := time.Unix(n.NetworkLastModified, 0).Format(time.RFC3339)
nodesLastModified := time.Unix(n.NodesLastModified, 0).Format(time.RFC3339)
table.Append([]string{n.NetID, n.AddressRange, n.AddressRange6, networkLastModified, nodesLastModified})
table.Append([]string{n.NetID, n.AddressRange, n.AddressRange6, nodesLastModified})
}
table.Render()
}
Expand Down
48 changes: 0 additions & 48 deletions controllers/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

"github.com/gorilla/mux"
"golang.org/x/exp/slog"

"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
Expand All @@ -24,7 +23,6 @@ func networkHandlers(r *mux.Router) {
r.HandleFunc("/api/networks", logic.SecurityCheck(true, checkFreeTierLimits(limitChoiceNetworks, http.HandlerFunc(createNetwork)))).Methods(http.MethodPost)
r.HandleFunc("/api/networks/{networkname}", logic.SecurityCheck(false, http.HandlerFunc(getNetwork))).Methods(http.MethodGet)
r.HandleFunc("/api/networks/{networkname}", logic.SecurityCheck(true, http.HandlerFunc(deleteNetwork))).Methods(http.MethodDelete)
r.HandleFunc("/api/networks/{networkname}", logic.SecurityCheck(true, http.HandlerFunc(updateNetwork))).Methods(http.MethodPut)
// ACLs
r.HandleFunc("/api/networks/{networkname}/acls", logic.SecurityCheck(true, http.HandlerFunc(updateNetworkACL))).Methods(http.MethodPut)
r.HandleFunc("/api/networks/{networkname}/acls", logic.SecurityCheck(true, http.HandlerFunc(getNetworkACL))).Methods(http.MethodGet)
Expand Down Expand Up @@ -292,49 +290,3 @@ func createNetwork(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(network)
}

// swagger:route PUT /api/networks networks updateNetwork
//
// Update pro settings for a network.
//
// Schemes: https
//
// Security:
// oauth
//
// Responses:
// 200: networkBodyResponse
func updateNetwork(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")

var payload models.Network

// we decode our body request params
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
slog.Info("error decoding request body", "user", r.Header.Get("user"), "err", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}

netOld1, err := logic.GetNetwork(payload.NetID)
if err != nil {
slog.Info("error fetching network", "user", r.Header.Get("user"), "err", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
// partial update
netOld2 := netOld1
netOld2.ProSettings = payload.ProSettings
_, _, _, _, _, err = logic.UpdateNetwork(&netOld1, &netOld2)
if err != nil {
slog.Info("failed to update network", "user", r.Header.Get("user"), "err", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}

slog.Info("updated network", "network", payload.NetID, "user", r.Header.Get("user"))
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(payload)
}
19 changes: 5 additions & 14 deletions logic/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func CreateNetwork(network models.Network) (models.Network, error) {

network.SetDefaults()
network.SetNodesLastModified()
network.SetNetworkLastModified()

pro.AddProNetDefaults(&network)

Expand Down Expand Up @@ -302,28 +301,20 @@ func IsNetworkNameUnique(network *models.Network) (bool, error) {
}

// UpdateNetwork - updates a network with another network's fields
func UpdateNetwork(currentNetwork *models.Network, newNetwork *models.Network) (bool, bool, bool, []string, []string, error) {
func UpdateNetwork(currentNetwork *models.Network, newNetwork *models.Network) error {
if err := ValidateNetwork(newNetwork, true); err != nil {
return false, false, false, nil, nil, err
return err
}
if newNetwork.NetID == currentNetwork.NetID {
hasrangeupdate4 := newNetwork.AddressRange != currentNetwork.AddressRange
hasrangeupdate6 := newNetwork.AddressRange6 != currentNetwork.AddressRange6
hasholepunchupdate := newNetwork.DefaultUDPHolePunch != currentNetwork.DefaultUDPHolePunch
groupDelta := append(StringDifference(newNetwork.ProSettings.AllowedGroups, currentNetwork.ProSettings.AllowedGroups),
StringDifference(currentNetwork.ProSettings.AllowedGroups, newNetwork.ProSettings.AllowedGroups)...)
userDelta := append(StringDifference(newNetwork.ProSettings.AllowedUsers, currentNetwork.ProSettings.AllowedUsers),
StringDifference(currentNetwork.ProSettings.AllowedUsers, newNetwork.ProSettings.AllowedUsers)...)
data, err := json.Marshal(newNetwork)
if err != nil {
return false, false, false, nil, nil, err
return err
}
newNetwork.SetNetworkLastModified()
err = database.Insert(newNetwork.NetID, string(data), database.NETWORKS_TABLE_NAME)
return hasrangeupdate4, hasrangeupdate6, hasholepunchupdate, groupDelta, userDelta, err
return err
}
// copy values
return false, false, false, nil, nil, errors.New("failed to update network " + newNetwork.NetID + ", cannot change netid.")
return errors.New("failed to update network " + newNetwork.NetID + ", cannot change netid.")
}

// GetNetwork - gets a network from database
Expand Down
48 changes: 12 additions & 36 deletions models/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,18 @@ import (
// 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 {
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"`
ProSettings *promodels.ProNetwork `json:"prosettings,omitempty" bson:"prosettings,omitempty" yaml:"prosettings,omitempty"`
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"`
DefaultListenPort int32 `json:"defaultlistenport,omitempty" bson:"defaultlistenport,omitempty" validate:"omitempty,min=1024,max=65535"`
NodeLimit int32 `json:"nodelimit" bson:"nodelimit"`
DefaultKeepalive int32 `json:"defaultkeepalive" bson:"defaultkeepalive" validate:"omitempty,max=1000"`
IsIPv4 string `json:"isipv4" bson:"isipv4" validate:"checkyesorno"`
IsIPv6 string `json:"isipv6" bson:"isipv6" validate:"checkyesorno"`
DefaultMTU int32 `json:"defaultmtu" bson:"defaultmtu"`
DefaultACL string `json:"defaultacl" bson:"defaultacl" yaml:"defaultacl" validate:"checkyesorno"`
ProSettings *promodels.ProNetwork `json:"prosettings,omitempty" bson:"prosettings,omitempty" yaml:"prosettings,omitempty"`
}

// SaveData - sensitive fields of a network that should be kept the same
Expand All @@ -38,23 +33,8 @@ func (network *Network) SetNodesLastModified() {
network.NodesLastModified = time.Now().Unix()
}

// Network.SetNetworkLastModified - sets network last modified time
func (network *Network) SetNetworkLastModified() {
network.NetworkLastModified = time.Now().Unix()
}

// Network.SetDefaults - sets default values for a network struct
func (network *Network) SetDefaults() {
if network.DefaultUDPHolePunch == "" {
network.DefaultUDPHolePunch = "no"
}
if network.DefaultInterface == "" {
if len(network.NetID) < 33 {
network.DefaultInterface = "nm-" + network.NetID
} else {
network.DefaultInterface = network.NetID
}
}
if network.DefaultListenPort == 0 {
network.DefaultListenPort = 51821
}
Expand All @@ -64,10 +44,6 @@ func (network *Network) SetDefaults() {
if network.DefaultKeepalive == 0 {
network.DefaultKeepalive = 20
}
if network.AllowManualSignUp == "" {
network.AllowManualSignUp = "no"
}

if network.IsIPv4 == "" {
network.IsIPv4 = "yes"
}
Expand Down
5 changes: 1 addition & 4 deletions serverctl/serverctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ func setNetworkDefaults() error {
newNet := network
if strings.Contains(network.NetID, ".") {
newNet.NetID = strings.ReplaceAll(network.NetID, ".", "")
newNet.DefaultInterface = strings.ReplaceAll(network.DefaultInterface, ".", "")
update = true
}
if strings.ContainsAny(network.NetID, "ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
newNet.NetID = strings.ToLower(network.NetID)
newNet.DefaultInterface = strings.ToLower(network.DefaultInterface)
update = true
}
if update {
Expand All @@ -85,8 +83,7 @@ func setNetworkDefaults() error {
}
} else {
network.SetDefaults()
_, _, _, _, _, err = logic.UpdateNetwork(&network, &network)
if err != nil {
if err := logic.UpdateNetwork(&network, &network); err != nil {
logger.Log(0, "could not set defaults on network", network.NetID)
}
}
Expand Down

0 comments on commit 780c8a8

Please sign in to comment.