Browse Source

use new node structs

Anish Mukherjee 2 years ago
parent
commit
90ef4fd168
7 changed files with 43 additions and 81 deletions
  1. 3 15
      cli/cmd/acl/allow.go
  2. 2 14
      cli/cmd/acl/deny.go
  3. 1 6
      cli/cmd/acl/list.go
  4. 1 0
      cli/cmd/host/update.go
  5. 0 6
      cli/cmd/node/flags.go
  6. 9 7
      cli/cmd/node/list.go
  7. 27 33
      cli/cmd/node/update.go

+ 3 - 15
cli/cmd/acl/allow.go

@@ -2,8 +2,6 @@ package acl
 
 import (
 	"fmt"
-	"log"
-	"strings"
 
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/logic/acls"
@@ -11,23 +9,13 @@ import (
 )
 
 var aclAllowCmd = &cobra.Command{
-	Use:   "allow [NETWORK NAME] [FROM_NODE_NAME] [TO_NODE_NAME]",
+	Use:   "allow [NETWORK NAME] [NODE_1_ID] [NODE_2_ID]",
 	Args:  cobra.ExactArgs(3),
 	Short: "Allow access from one node to another",
 	Long:  `Allow access from one node to another`,
 	Run: func(cmd *cobra.Command, args []string) {
-		nameIDMap := make(map[string]string)
-		for _, node := range *functions.GetNodes(args[0]) {
-			nameIDMap[strings.ToLower(node.Name)] = node.ID
-		}
-		fromNodeID, ok := nameIDMap[strings.ToLower(args[1])]
-		if !ok {
-			log.Fatalf("Node %s doesn't exist", args[1])
-		}
-		toNodeID, ok := nameIDMap[strings.ToLower(args[2])]
-		if !ok {
-			log.Fatalf("Node %s doesn't exist", args[2])
-		}
+		fromNodeID := args[1]
+		toNodeID := args[2]
 		payload := acls.ACLContainer(map[acls.AclID]acls.ACL{
 			acls.AclID(fromNodeID): map[acls.AclID]byte{
 				acls.AclID(toNodeID): acls.Allowed,

+ 2 - 14
cli/cmd/acl/deny.go

@@ -2,8 +2,6 @@ package acl
 
 import (
 	"fmt"
-	"log"
-	"strings"
 
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/logic/acls"
@@ -16,18 +14,8 @@ var aclDenyCmd = &cobra.Command{
 	Short: "Deny access from one node to another",
 	Long:  `Deny access from one node to another`,
 	Run: func(cmd *cobra.Command, args []string) {
-		nameIDMap := make(map[string]string)
-		for _, node := range *functions.GetNodes(args[0]) {
-			nameIDMap[strings.ToLower(node.Name)] = node.ID
-		}
-		fromNodeID, ok := nameIDMap[strings.ToLower(args[1])]
-		if !ok {
-			log.Fatalf("Node %s doesn't exist", args[1])
-		}
-		toNodeID, ok := nameIDMap[strings.ToLower(args[2])]
-		if !ok {
-			log.Fatalf("Node %s doesn't exist", args[2])
-		}
+		fromNodeID := args[1]
+		toNodeID := args[2]
 		payload := acls.ACLContainer(map[acls.AclID]acls.ACL{
 			acls.AclID(fromNodeID): map[acls.AclID]byte{
 				acls.AclID(toNodeID): acls.NotAllowed,

+ 1 - 6
cli/cmd/acl/list.go

@@ -16,16 +16,11 @@ var aclListCmd = &cobra.Command{
 	Long:  `List all ACLs associated with a network`,
 	Run: func(cmd *cobra.Command, args []string) {
 		aclSource := (map[acls.AclID]acls.ACL)(*functions.GetACL(args[0]))
-		nodes := functions.GetNodes(args[0])
-		idNameMap := make(map[string]string)
-		for _, node := range *nodes {
-			idNameMap[node.ID] = node.Name
-		}
 		table := tablewriter.NewWriter(os.Stdout)
 		table.SetHeader([]string{"From", "To", "Status"})
 		for id, acl := range aclSource {
 			for k, v := range (map[acls.AclID]byte)(acl) {
-				row := []string{idNameMap[string(id)], idNameMap[string(k)]}
+				row := []string{string(id), string(k)}
 				switch v {
 				case acls.NotAllowed:
 					row = append(row, "Not Allowed")

+ 1 - 0
cli/cmd/host/update.go

@@ -6,6 +6,7 @@ import (
 	"os"
 
 	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/gravitl/netmaker/models"
 	"github.com/spf13/cobra"
 )
 

+ 0 - 6
cli/cmd/node/flags.go

@@ -6,23 +6,17 @@ var (
 	failover               bool
 	networkName            string
 	nodeDefinitionFilePath string
-	endpoint               string
-	listenPort             int
 	address                string
 	address6               string
 	localAddress           string
 	name                   string
 	postUp                 string
 	postDown               string
-	allowedIPs             string
 	keepAlive              int
 	relayAddrs             string
 	egressGatewayRanges    string
-	localRange             string
-	mtu                    int
 	expirationDateTime     int
 	defaultACL             bool
 	dnsOn                  bool
 	disconnect             bool
-	networkHub             bool
 )

+ 9 - 7
cli/cmd/node/list.go

@@ -2,6 +2,7 @@ package node
 
 import (
 	"os"
+	"strconv"
 
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/models"
@@ -23,19 +24,20 @@ var nodeListCmd = &cobra.Command{
 			data = *functions.GetNodes()
 		}
 		table := tablewriter.NewWriter(os.Stdout)
-		table.SetHeader([]string{"Name", "Addresses", "Version", "Network", "Egress", "Ingress", "Relay", "ID"})
+		table.SetHeader([]string{"ID", "Addresses", "Network", "Egress", "Ingress", "Relay"})
 		for _, d := range data {
 			addresses := ""
-			if d.Address != "" {
-				addresses += d.Address
+			if d.Address.String() != "" {
+				addresses += d.Address.String()
 			}
-			if d.Address6 != "" {
-				if d.Address != "" {
+			if d.Address6.String() != "" {
+				if d.Address.String() != "" {
 					addresses += ", "
 				}
-				addresses += d.Address6
+				addresses += d.Address6.String()
 			}
-			table.Append([]string{d.Name, addresses, d.Version, d.Network, d.IsEgressGateway, d.IsIngressGateway, d.IsRelay, d.ID})
+			table.Append([]string{d.ID.String(), addresses, d.Network,
+				strconv.FormatBool(d.IsEgressGateway), strconv.FormatBool(d.IsIngressGateway), strconv.FormatBool(d.IsRelay)})
 		}
 		table.Render()
 	},

+ 27 - 33
cli/cmd/node/update.go

@@ -3,8 +3,10 @@ package node
 import (
 	"encoding/json"
 	"log"
+	"net"
 	"os"
 	"strings"
+	"time"
 
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/models"
@@ -31,45 +33,43 @@ var nodeUpdateCmd = &cobra.Command{
 				log.Fatal(err)
 			}
 		} else {
-			if endpoint != "" {
-				node.Endpoint = endpoint
-				node.IsStatic = "no"
+			if address != "" {
+				if _, addr, err := net.ParseCIDR(address); err != nil {
+					log.Fatal(err)
+				} else {
+					node.Address = *addr
+				}
+			}
+			if address6 != "" {
+				if _, addr6, err := net.ParseCIDR(address6); err != nil {
+					log.Fatal(err)
+				} else {
+					node.Address6 = *addr6
+				}
+			}
+			if localAddress != "" {
+				if _, localAddr, err := net.ParseCIDR(localAddress); err != nil {
+					log.Fatal(err)
+				} else {
+					node.LocalAddress = *localAddr
+					node.IsLocal = true
+				}
 			}
-			node.ListenPort = int32(listenPort)
-			node.Address = address
-			node.Address6 = address6
-			node.LocalAddress = localAddress
-			node.Name = name
 			node.PostUp = postUp
 			node.PostDown = postDown
-			if allowedIPs != "" {
-				node.AllowedIPs = strings.Split(allowedIPs, ",")
-			}
-			node.PersistentKeepalive = int32(keepAlive)
+			node.PersistentKeepalive = time.Duration(time.Second * time.Duration(keepAlive))
 			if relayAddrs != "" {
 				node.RelayAddrs = strings.Split(relayAddrs, ",")
 			}
 			if egressGatewayRanges != "" {
 				node.EgressGatewayRanges = strings.Split(egressGatewayRanges, ",")
 			}
-			if localRange != "" {
-				node.LocalRange = localRange
-				node.IsLocal = "yes"
-			}
-			node.MTU = int32(mtu)
-			node.ExpirationDateTime = int64(expirationDateTime)
+			node.ExpirationDateTime = time.Unix(int64(expirationDateTime), 0)
 			if defaultACL {
 				node.DefaultACL = "yes"
 			}
-			if dnsOn {
-				node.DNSOn = "yes"
-			}
-			if disconnect {
-				node.Connected = "no"
-			}
-			if networkHub {
-				node.IsHub = "yes"
-			}
+			node.DNSOn = dnsOn
+			node.Connected = !disconnect
 		}
 		functions.PrettyPrint(functions.UpdateNode(networkName, nodeID, node))
 	},
@@ -77,24 +77,18 @@ var nodeUpdateCmd = &cobra.Command{
 
 func init() {
 	nodeUpdateCmd.Flags().StringVar(&nodeDefinitionFilePath, "file", "", "Filepath of updated node definition in JSON")
-	nodeUpdateCmd.Flags().StringVar(&endpoint, "endpoint", "", "Public endpoint of the node")
-	nodeUpdateCmd.Flags().IntVar(&listenPort, "listen_port", 0, "Default wireguard port for the node")
 	nodeUpdateCmd.Flags().StringVar(&address, "ipv4_addr", "", "IPv4 address of the node")
 	nodeUpdateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 address of the node")
 	nodeUpdateCmd.Flags().StringVar(&localAddress, "local_addr", "", "Locally reachable address of the node")
 	nodeUpdateCmd.Flags().StringVar(&name, "name", "", "Node name")
 	nodeUpdateCmd.Flags().StringVar(&postUp, "post_up", "", "Commands to run after node is up `;` separated")
 	nodeUpdateCmd.Flags().StringVar(&postDown, "post_down", "", "Commands to run after node is down `;` separated")
-	nodeUpdateCmd.Flags().StringVar(&allowedIPs, "allowed_addrs", "", "Additional private addresses given to the node (comma separated)")
 	nodeUpdateCmd.Flags().IntVar(&keepAlive, "keep_alive", 0, "Interval in which packets are sent to keep connections open with peers")
 	nodeUpdateCmd.Flags().StringVar(&relayAddrs, "relay_addrs", "", "Addresses for relaying connections if node acts as a relay")
 	nodeUpdateCmd.Flags().StringVar(&egressGatewayRanges, "egress_addrs", "", "Addresses for egressing traffic if node acts as an egress")
-	nodeUpdateCmd.Flags().StringVar(&localRange, "local_range", "", "Local range in which the node will look for private addresses to use as an endpoint if `LocalNetwork` is enabled")
-	nodeUpdateCmd.Flags().IntVar(&mtu, "mtu", 0, "MTU size")
 	nodeUpdateCmd.Flags().IntVar(&expirationDateTime, "expiry", 0, "UNIX timestamp after which node will lose access to the network")
 	nodeUpdateCmd.Flags().BoolVar(&defaultACL, "acl", false, "Enable default ACL ?")
 	nodeUpdateCmd.Flags().BoolVar(&dnsOn, "dns", false, "Setup DNS entries for peers locally ?")
 	nodeUpdateCmd.Flags().BoolVar(&disconnect, "disconnect", false, "Disconnect from the network ?")
-	nodeUpdateCmd.Flags().BoolVar(&networkHub, "hub", false, "On a point to site network, this node is the only one which all peers connect to ?")
 	rootCmd.AddCommand(nodeUpdateCmd)
 }