From 7004c8dd3ec6a23584ce617930a8ff2ef22cd31b Mon Sep 17 00:00:00 2001 From: Anish Mukherjee Date: Wed, 23 Nov 2022 23:29:27 +0530 Subject: [PATCH] add ext_client create, get, delete and list commands --- cli/cmd/ext_client/create.go | 26 ++++++++++++++++++++++++ cli/cmd/ext_client/delete.go | 20 +++++++++++++++++++ cli/cmd/ext_client/get.go | 20 +++++++++++++++++++ cli/cmd/ext_client/list.go | 27 +++++++++++++++++++++++++ cli/cmd/ext_client/root.go | 37 +++++++++++++++++++++++++++++++++++ cli/cmd/root.go | 2 ++ cli/functions/ext_client.go | 38 ++++++++++++++++++++++++++++++++++++ 7 files changed, 170 insertions(+) create mode 100644 cli/cmd/ext_client/create.go create mode 100644 cli/cmd/ext_client/delete.go create mode 100644 cli/cmd/ext_client/get.go create mode 100644 cli/cmd/ext_client/list.go create mode 100644 cli/cmd/ext_client/root.go create mode 100644 cli/functions/ext_client.go diff --git a/cli/cmd/ext_client/create.go b/cli/cmd/ext_client/create.go new file mode 100644 index 00000000..aad25925 --- /dev/null +++ b/cli/cmd/ext_client/create.go @@ -0,0 +1,26 @@ +package ext_client + +import ( + "fmt" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var extClientID string + +var extClientCreateCmd = &cobra.Command{ + Use: "create [NETWORK NAME] [NODE ID]", + Args: cobra.ExactArgs(2), + Short: "Create an External Client", + Long: `Create an External Client`, + Run: func(cmd *cobra.Command, args []string) { + functions.CreateExtClient(args[0], args[1], extClientID) + fmt.Println("Success") + }, +} + +func init() { + extClientCreateCmd.Flags().StringVar(&extClientID, "id", "", "ID of the external client") + rootCmd.AddCommand(extClientCreateCmd) +} diff --git a/cli/cmd/ext_client/delete.go b/cli/cmd/ext_client/delete.go new file mode 100644 index 00000000..52c12529 --- /dev/null +++ b/cli/cmd/ext_client/delete.go @@ -0,0 +1,20 @@ +package ext_client + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var extClientDeleteCmd = &cobra.Command{ + Use: "delete [NETWORK NAME] [EXTERNAL CLIENT ID]", + Args: cobra.ExactArgs(2), + Short: "Delete an External Client", + Long: `Delete an External Client`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.DeleteExtClient(args[0], args[1])) + }, +} + +func init() { + rootCmd.AddCommand(extClientDeleteCmd) +} diff --git a/cli/cmd/ext_client/get.go b/cli/cmd/ext_client/get.go new file mode 100644 index 00000000..4bdb918e --- /dev/null +++ b/cli/cmd/ext_client/get.go @@ -0,0 +1,20 @@ +package ext_client + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var extClientGetCmd = &cobra.Command{ + Use: "get [NETWORK NAME] [EXTERNAL CLIENT ID]", + Args: cobra.ExactArgs(2), + Short: "Get an External Client", + Long: `Get an External Client`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetExtClient(args[0], args[1])) + }, +} + +func init() { + rootCmd.AddCommand(extClientGetCmd) +} diff --git a/cli/cmd/ext_client/list.go b/cli/cmd/ext_client/list.go new file mode 100644 index 00000000..9822d805 --- /dev/null +++ b/cli/cmd/ext_client/list.go @@ -0,0 +1,27 @@ +package ext_client + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var networkName string + +var extClientListCmd = &cobra.Command{ + Use: "list", + Args: cobra.NoArgs, + Short: "List External Clients", + Long: `List External Clients`, + Run: func(cmd *cobra.Command, args []string) { + if networkName != "" { + functions.PrettyPrint(functions.GetNetworkExtClients(networkName)) + } else { + functions.PrettyPrint(functions.GetAllExtClients()) + } + }, +} + +func init() { + extClientListCmd.Flags().StringVar(&networkName, "network", "", "Network name") + rootCmd.AddCommand(extClientListCmd) +} diff --git a/cli/cmd/ext_client/root.go b/cli/cmd/ext_client/root.go new file mode 100644 index 00000000..d55f2de5 --- /dev/null +++ b/cli/cmd/ext_client/root.go @@ -0,0 +1,37 @@ +package ext_client + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "ext_client", + Short: "Manage External Clients", + Long: `Manage External Clients`, + // Run: func(cmd *cobra.Command, args []string) { }, +} + +func GetRoot() *cobra.Command { + return rootCmd +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + // Cobra also supports local flags, which will only run + // when this action is called directly. + rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index e7a6debc..ec4ba953 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -6,6 +6,7 @@ import ( "github.com/gravitl/netmaker/cli/cmd/acl" "github.com/gravitl/netmaker/cli/cmd/context" "github.com/gravitl/netmaker/cli/cmd/dns" + "github.com/gravitl/netmaker/cli/cmd/ext_client" "github.com/gravitl/netmaker/cli/cmd/keys" "github.com/gravitl/netmaker/cli/cmd/network" "github.com/gravitl/netmaker/cli/cmd/node" @@ -55,4 +56,5 @@ func init() { rootCmd.AddCommand(node.GetRoot()) rootCmd.AddCommand(dns.GetRoot()) rootCmd.AddCommand(server.GetRoot()) + rootCmd.AddCommand(ext_client.GetRoot()) } diff --git a/cli/functions/ext_client.go b/cli/functions/ext_client.go new file mode 100644 index 00000000..9540bdd5 --- /dev/null +++ b/cli/functions/ext_client.go @@ -0,0 +1,38 @@ +package functions + +import ( + "fmt" + "net/http" + + "github.com/gravitl/netmaker/models" +) + +func GetAllExtClients() *[]models.ExtClient { + return request[[]models.ExtClient](http.MethodGet, "/api/extclients", nil) +} + +func GetNetworkExtClients(networkName string) *[]models.ExtClient { + return request[[]models.ExtClient](http.MethodGet, "/api/extclients/"+networkName, nil) +} + +func GetExtClient(networkName, clientID string) *models.ExtClient { + return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil) +} + +func GetExtClientConfig(networkName, clientID, configType string) *models.ExtClient { + return request[models.ExtClient](http.MethodGet, fmt.Sprintf("/api/extclients/%s/%s/%s", networkName, clientID, configType), nil) +} + +func CreateExtClient(networkName, nodeID, extClientID string) { + if extClientID != "" { + request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), &models.CustomExtClient{ + ClientID: extClientID, + }) + } else { + request[any](http.MethodPost, fmt.Sprintf("/api/extclients/%s/%s", networkName, nodeID), nil) + } +} + +func DeleteExtClient(networkName, clientID string) *models.SuccessResponse { + return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil) +}