diff --git a/cli/cmd/metrics/all.go b/cli/cmd/metrics/all.go new file mode 100644 index 00000000..a270559b --- /dev/null +++ b/cli/cmd/metrics/all.go @@ -0,0 +1,20 @@ +package metrics + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var metricsAllCmd = &cobra.Command{ + Use: "all", + Args: cobra.NoArgs, + Short: "Retrieve all metrics", + Long: `Retrieve all metrics`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetAllMetrics()) + }, +} + +func init() { + rootCmd.AddCommand(metricsAllCmd) +} diff --git a/cli/cmd/metrics/network.go b/cli/cmd/metrics/network.go new file mode 100644 index 00000000..0c211776 --- /dev/null +++ b/cli/cmd/metrics/network.go @@ -0,0 +1,20 @@ +package metrics + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var metricsNetworkCmd = &cobra.Command{ + Use: "network [NETWORK NAME]", + Args: cobra.ExactArgs(1), + Short: "Retrieve network metrics", + Long: `Retrieve network metrics`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetNetworkNodeMetrics(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(metricsNetworkCmd) +} diff --git a/cli/cmd/metrics/network_ext.go b/cli/cmd/metrics/network_ext.go new file mode 100644 index 00000000..92876a7e --- /dev/null +++ b/cli/cmd/metrics/network_ext.go @@ -0,0 +1,20 @@ +package metrics + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var metricsNetworkExtCmd = &cobra.Command{ + Use: "network_ext [NETWORK NAME]", + Args: cobra.ExactArgs(1), + Short: "Retrieve metrics of external clients on a given network", + Long: `Retrieve metrics of external clients on a given network`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetNetworkExtMetrics(args[0])) + }, +} + +func init() { + rootCmd.AddCommand(metricsNetworkExtCmd) +} diff --git a/cli/cmd/metrics/node.go b/cli/cmd/metrics/node.go new file mode 100644 index 00000000..9ebcfd52 --- /dev/null +++ b/cli/cmd/metrics/node.go @@ -0,0 +1,20 @@ +package metrics + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var metricsNodeCmd = &cobra.Command{ + Use: "node [NETWORK NAME] [NODE ID]", + Args: cobra.ExactArgs(2), + Short: "Retrieve node metrics", + Long: `Retrieve node metrics`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetNodeMetrics(args[0], args[1])) + }, +} + +func init() { + rootCmd.AddCommand(metricsNodeCmd) +} diff --git a/cli/cmd/metrics/root.go b/cli/cmd/metrics/root.go new file mode 100644 index 00000000..529ebe65 --- /dev/null +++ b/cli/cmd/metrics/root.go @@ -0,0 +1,38 @@ +package metrics + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "metrics", + Short: "Fetch metrics of nodes/networks", + Long: `Fetch metrics of nodes/networks`, + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// GetRoot returns the root subcommand +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 6760713a..8492f8c8 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -8,10 +8,12 @@ import ( "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/metrics" "github.com/gravitl/netmaker/cli/cmd/network" "github.com/gravitl/netmaker/cli/cmd/node" "github.com/gravitl/netmaker/cli/cmd/server" "github.com/gravitl/netmaker/cli/cmd/user" + "github.com/gravitl/netmaker/cli/cmd/usergroup" "github.com/spf13/cobra" ) @@ -59,4 +61,6 @@ func init() { rootCmd.AddCommand(server.GetRoot()) rootCmd.AddCommand(ext_client.GetRoot()) rootCmd.AddCommand(user.GetRoot()) + rootCmd.AddCommand(usergroup.GetRoot()) + rootCmd.AddCommand(metrics.GetRoot()) } diff --git a/cli/cmd/usergroup/create.go b/cli/cmd/usergroup/create.go new file mode 100644 index 00000000..f3139d7c --- /dev/null +++ b/cli/cmd/usergroup/create.go @@ -0,0 +1,23 @@ +package usergroup + +import ( + "fmt" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var usergroupCreateCmd = &cobra.Command{ + Use: "create [GROUP NAME]", + Args: cobra.ExactArgs(1), + Short: "Create a usergroup", + Long: `Create a usergroup`, + Run: func(cmd *cobra.Command, args []string) { + functions.CreateUsergroup(args[0]) + fmt.Println("Success") + }, +} + +func init() { + rootCmd.AddCommand(usergroupCreateCmd) +} diff --git a/cli/cmd/usergroup/delete.go b/cli/cmd/usergroup/delete.go new file mode 100644 index 00000000..b4172b74 --- /dev/null +++ b/cli/cmd/usergroup/delete.go @@ -0,0 +1,23 @@ +package usergroup + +import ( + "fmt" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var usergroupDeleteCmd = &cobra.Command{ + Use: "delete [GROUP NAME]", + Args: cobra.ExactArgs(1), + Short: "Delete a usergroup", + Long: `Delete a usergroup`, + Run: func(cmd *cobra.Command, args []string) { + functions.DeleteUsergroup(args[0]) + fmt.Println("Success") + }, +} + +func init() { + rootCmd.AddCommand(usergroupDeleteCmd) +} diff --git a/cli/cmd/usergroup/get.go b/cli/cmd/usergroup/get.go new file mode 100644 index 00000000..e2f1a0bf --- /dev/null +++ b/cli/cmd/usergroup/get.go @@ -0,0 +1,20 @@ +package usergroup + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var usergroupGetCmd = &cobra.Command{ + Use: "get", + Args: cobra.NoArgs, + Short: "Fetch all usergroups", + Long: `Fetch all usergroups`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.GetUsergroups()) + }, +} + +func init() { + rootCmd.AddCommand(usergroupGetCmd) +} diff --git a/cli/cmd/usergroup/root.go b/cli/cmd/usergroup/root.go new file mode 100644 index 00000000..e941ebb3 --- /dev/null +++ b/cli/cmd/usergroup/root.go @@ -0,0 +1,38 @@ +package usergroup + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "usergroup", + Short: "Manage User Groups", + Long: `Manage User Groups`, + // Run: func(cmd *cobra.Command, args []string) { }, +} + +// GetRoot returns the root subcommand +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/functions/metrics.go b/cli/functions/metrics.go new file mode 100644 index 00000000..b4a42f1d --- /dev/null +++ b/cli/functions/metrics.go @@ -0,0 +1,25 @@ +package functions + +import ( + "fmt" + "net/http" + + "github.com/gravitl/netmaker/models" +) + +// GetNodeMetrics - fetch a single node's metrics +func GetNodeMetrics(networkName, nodeID string) *models.Metrics { + return request[models.Metrics](http.MethodGet, fmt.Sprintf("/api/metrics/%s/%s", networkName, nodeID), nil) +} + +func GetNetworkNodeMetrics(networkName string) *models.NetworkMetrics { + return request[models.NetworkMetrics](http.MethodGet, "/api/metrics/"+networkName, nil) +} + +func GetAllMetrics() *models.NetworkMetrics { + return request[models.NetworkMetrics](http.MethodGet, "/api/metrics", nil) +} + +func GetNetworkExtMetrics(networkName string) *map[string]models.Metric { + return request[map[string]models.Metric](http.MethodGet, "/api/metrics-ext/"+networkName, nil) +} diff --git a/cli/functions/network_user.go b/cli/functions/network_user.go new file mode 100644 index 00000000..b8bd2dae --- /dev/null +++ b/cli/functions/network_user.go @@ -0,0 +1,37 @@ +package functions + +import ( + "fmt" + "net/http" + + "github.com/gravitl/netmaker/ee/ee_controllers" + "github.com/gravitl/netmaker/models/promodels" +) + +func GetAllNetworkUsers() *map[string][]promodels.NetworkUser { + return request[map[string][]promodels.NetworkUser](http.MethodGet, "/api/networkusers", nil) +} + +func GetNetworkUsers(networkName string) *promodels.NetworkUserMap { + return request[promodels.NetworkUserMap](http.MethodGet, "/api/networkusers/"+networkName, nil) +} + +func GetNetworkUser(networkName, networkUserName string) *promodels.NetworkUser { + return request[promodels.NetworkUser](http.MethodGet, fmt.Sprintf("/api/networkusers/%s/%s", networkName, networkUserName), nil) +} + +func CreateNetworkUser(networkName string, payload *promodels.NetworkUser) { + request[any](http.MethodPost, "/api/networkusers/"+networkName, payload) +} + +func UpdateNetworkUser(networkName string, payload *promodels.NetworkUser) { + request[any](http.MethodPut, "/api/networkusers/"+networkName, payload) +} + +func GetNetworkUserData(networkUserName string) *ee_controllers.NetworkUserDataMap { + return request[ee_controllers.NetworkUserDataMap](http.MethodGet, fmt.Sprintf("/api/networkusers/data/%s/me", networkUserName), nil) +} + +func DeleteNetworkUser(networkName, networkUserName string) { + request[any](http.MethodDelete, fmt.Sprintf("/api/networkusers/%s/%s", networkName, networkUserName), nil) +} diff --git a/cli/functions/usergroups.go b/cli/functions/usergroups.go new file mode 100644 index 00000000..5095a0b7 --- /dev/null +++ b/cli/functions/usergroups.go @@ -0,0 +1,19 @@ +package functions + +import ( + "net/http" + + "github.com/gravitl/netmaker/models/promodels" +) + +func GetUsergroups() *promodels.UserGroups { + return request[promodels.UserGroups](http.MethodGet, "/api/usergroups", nil) +} + +func CreateUsergroup(usergroupName string) { + request[any](http.MethodPost, "/api/usergroups/"+usergroupName, nil) +} + +func DeleteUsergroup(usergroupName string) { + request[any](http.MethodDelete, "/api/usergroups/"+usergroupName, nil) +}