add metrics and usergroup subcommands

This commit is contained in:
Anish Mukherjee 2022-11-28 19:27:17 +05:30
parent d999f7b86e
commit 683fe7abb1
13 changed files with 307 additions and 0 deletions

20
cli/cmd/metrics/all.go Normal file
View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

20
cli/cmd/metrics/node.go Normal file
View file

@ -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)
}

38
cli/cmd/metrics/root.go Normal file
View file

@ -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")
}

View file

@ -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())
}

View file

@ -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)
}

View file

@ -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)
}

20
cli/cmd/usergroup/get.go Normal file
View file

@ -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)
}

38
cli/cmd/usergroup/root.go Normal file
View file

@ -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")
}

25
cli/functions/metrics.go Normal file
View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}