Browse Source

add flags for network creation

Anish Mukherjee 2 years ago
parent
commit
b7202b371a
2 changed files with 52 additions and 10 deletions
  1. 50 8
      cli/cmd/network/create.go
  2. 2 2
      cli/cmd/network/update.go

+ 50 - 8
cli/cmd/network/create.go

@@ -10,25 +10,67 @@ import (
 	"github.com/spf13/cobra"
 	"github.com/spf13/cobra"
 )
 )
 
 
+var (
+	networkDefinitionFilePath string
+	netID                     string
+	ipv4Address               string
+	ipv6Address               string
+	udpHolePunch              bool
+	localNetwork              bool
+	defaultACL                bool
+	pointToSite               bool
+)
+
 // networkCreateCmd represents the networkCreate command
 // networkCreateCmd represents the networkCreate command
 var networkCreateCmd = &cobra.Command{
 var networkCreateCmd = &cobra.Command{
-	Use:   "create [/path/to/network_definition.json]",
+	Use:   "create [--flags]",
 	Short: "Create a Network",
 	Short: "Create a Network",
 	Long:  `Create a Network`,
 	Long:  `Create a Network`,
-	Args:  cobra.ExactArgs(1),
+	Args:  cobra.NoArgs,
 	Run: func(cmd *cobra.Command, args []string) {
 	Run: func(cmd *cobra.Command, args []string) {
-		content, err := os.ReadFile(args[0])
-		if err != nil {
-			log.Fatal("Error when opening file: ", err)
-		}
 		network := &models.Network{}
 		network := &models.Network{}
-		if err := json.Unmarshal(content, network); err != nil {
-			log.Fatal(err)
+		if networkDefinitionFilePath != "" {
+			content, err := os.ReadFile(networkDefinitionFilePath)
+			if err != nil {
+				log.Fatal("Error when opening file: ", err)
+			}
+			if err := json.Unmarshal(content, network); err != nil {
+				log.Fatal(err)
+			}
+		} else {
+			network.NetID = netID
+			network.AddressRange = ipv4Address
+			if ipv6Address != "" {
+				network.AddressRange6 = ipv6Address
+				network.IsIPv6 = "yes"
+			}
+			if udpHolePunch {
+				network.DefaultUDPHolePunch = "yes"
+			}
+			if localNetwork {
+				network.IsLocal = "yes"
+			}
+			if defaultACL {
+				network.DefaultACL = "yes"
+			}
+			if pointToSite {
+				network.IsPointToSite = "yes"
+			}
 		}
 		}
 		functions.PrettyPrint(functions.CreateNetwork(network))
 		functions.PrettyPrint(functions.CreateNetwork(network))
 	},
 	},
 }
 }
 
 
 func init() {
 func init() {
+	networkCreateCmd.Flags().StringVar(&networkDefinitionFilePath, "file", "", "Path to network_definition.json")
+	networkCreateCmd.Flags().StringVar(&netID, "name", "", "Name of the network")
+	networkCreateCmd.MarkFlagsMutuallyExclusive("file", "name")
+
+	networkCreateCmd.Flags().StringVar(&ipv4Address, "ipv4_addr", "", "IPv4 address of the network")
+	networkCreateCmd.Flags().StringVar(&ipv6Address, "ipv6_addr", "", "IPv6 address of the network")
+	networkCreateCmd.Flags().BoolVar(&udpHolePunch, "udp_hole_punch", false, "Enable UDP Hole Punching ?")
+	networkCreateCmd.Flags().BoolVar(&localNetwork, "local", false, "Is the network local (LAN) ?")
+	networkCreateCmd.Flags().BoolVar(&defaultACL, "default_acl", true, "Enable default Access Control List ?")
+	networkCreateCmd.Flags().BoolVar(&pointToSite, "point_to_site", false, "Enforce all clients to have only 1 central peer ?")
 	rootCmd.AddCommand(networkCreateCmd)
 	rootCmd.AddCommand(networkCreateCmd)
 }
 }

+ 2 - 2
cli/cmd/network/update.go

@@ -2,8 +2,8 @@ package network
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
-	"io/ioutil"
 	"log"
 	"log"
+	"os"
 
 
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models"
@@ -16,7 +16,7 @@ var networkUpdateCmd = &cobra.Command{
 	Long:  `Update a Network`,
 	Long:  `Update a Network`,
 	Args:  cobra.ExactArgs(2),
 	Args:  cobra.ExactArgs(2),
 	Run: func(cmd *cobra.Command, args []string) {
 	Run: func(cmd *cobra.Command, args []string) {
-		content, err := ioutil.ReadFile(args[1])
+		content, err := os.ReadFile(args[1])
 		if err != nil {
 		if err != nil {
 			log.Fatal("Error when opening file: ", err)
 			log.Fatal("Error when opening file: ", err)
 		}
 		}