mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 20:05:46 +08:00
add network users subcommand
This commit is contained in:
parent
683fe7abb1
commit
a98a116bf7
11 changed files with 227 additions and 0 deletions
43
cli/cmd/network_user/create.go
Normal file
43
cli/cmd/network_user/create.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/cli/functions"
|
||||||
|
"github.com/gravitl/netmaker/models/promodels"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var networkuserCreateCmd = &cobra.Command{
|
||||||
|
Use: "create [NETWORK NAME]",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Short: "Create a network user",
|
||||||
|
Long: `Create a network user`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
user := &promodels.NetworkUser{
|
||||||
|
AccessLevel: accessLevel,
|
||||||
|
ClientLimit: clientLimit,
|
||||||
|
NodeLimit: nodeLimit, ID: promodels.NetworkUserID(id),
|
||||||
|
}
|
||||||
|
if clients != "" {
|
||||||
|
user.Clients = strings.Split(clients, ",")
|
||||||
|
}
|
||||||
|
if nodes != "" {
|
||||||
|
user.Nodes = strings.Split(nodes, ",")
|
||||||
|
}
|
||||||
|
functions.CreateNetworkUser(args[0], user)
|
||||||
|
fmt.Println("Success")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
networkuserCreateCmd.Flags().IntVar(&accessLevel, "access_level", 0, "Custom access level")
|
||||||
|
networkuserCreateCmd.Flags().IntVar(&clientLimit, "client_limit", 0, "Maximum number of external clients that can be created")
|
||||||
|
networkuserCreateCmd.Flags().IntVar(&nodeLimit, "node_limit", 999999999, "Maximum number of nodes that can be attached to a network")
|
||||||
|
networkuserCreateCmd.Flags().StringVar(&clients, "clients", "", "Access to list of external clients (comma separated)")
|
||||||
|
networkuserCreateCmd.Flags().StringVar(&nodes, "nodes", "", "Access to list of nodes (comma separated)")
|
||||||
|
networkuserCreateCmd.Flags().StringVar(&id, "id", "", "ID of the network user")
|
||||||
|
networkuserCreateCmd.MarkFlagRequired("id")
|
||||||
|
rootCmd.AddCommand(networkuserCreateCmd)
|
||||||
|
}
|
23
cli/cmd/network_user/delete.go
Normal file
23
cli/cmd/network_user/delete.go
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/cli/functions"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var networkuserDeleteCmd = &cobra.Command{
|
||||||
|
Use: "delete [NETWORK NAME] [NETWORK USER NAME]",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
Short: "Delete a network user",
|
||||||
|
Long: `Delete a network user`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
functions.DeleteNetworkUser(args[0], args[1])
|
||||||
|
fmt.Println("Success")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(networkuserDeleteCmd)
|
||||||
|
}
|
10
cli/cmd/network_user/flags.go
Normal file
10
cli/cmd/network_user/flags.go
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
var (
|
||||||
|
accessLevel int
|
||||||
|
clientLimit int
|
||||||
|
nodeLimit int
|
||||||
|
clients string
|
||||||
|
nodes string
|
||||||
|
id string
|
||||||
|
)
|
27
cli/cmd/network_user/get.go
Normal file
27
cli/cmd/network_user/get.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gravitl/netmaker/cli/functions"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var data bool
|
||||||
|
|
||||||
|
var networkuserGetCmd = &cobra.Command{
|
||||||
|
Use: "get [NETWORK NAME] [NETWORK USER NAME]",
|
||||||
|
Args: cobra.ExactArgs(2),
|
||||||
|
Short: "Fetch a network user",
|
||||||
|
Long: `Fetch a network user`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if data {
|
||||||
|
functions.PrettyPrint(functions.GetNetworkUserData(args[1]))
|
||||||
|
} else {
|
||||||
|
functions.PrettyPrint(functions.GetNetworkUser(args[0], args[1]))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
networkuserGetCmd.Flags().BoolVar(&data, "data", false, "Fetch entire data of a network user")
|
||||||
|
rootCmd.AddCommand(networkuserGetCmd)
|
||||||
|
}
|
27
cli/cmd/network_user/list.go
Normal file
27
cli/cmd/network_user/list.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gravitl/netmaker/cli/functions"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var networkName string
|
||||||
|
|
||||||
|
var networkuserListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
Short: "List network users",
|
||||||
|
Long: `List network users`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if networkName != "" {
|
||||||
|
functions.PrettyPrint(functions.GetNetworkUsers(networkName))
|
||||||
|
} else {
|
||||||
|
functions.PrettyPrint(functions.GetAllNetworkUsers())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
networkuserListCmd.Flags().StringVar(&networkName, "network", "", "Name of the network")
|
||||||
|
rootCmd.AddCommand(networkuserListCmd)
|
||||||
|
}
|
38
cli/cmd/network_user/root.go
Normal file
38
cli/cmd/network_user/root.go
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// rootCmd represents the base command when called without any subcommands
|
||||||
|
var rootCmd = &cobra.Command{
|
||||||
|
Use: "network_user",
|
||||||
|
Short: "Manage Network Users",
|
||||||
|
Long: `Manage Network Users`,
|
||||||
|
// 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")
|
||||||
|
}
|
43
cli/cmd/network_user/update.go
Normal file
43
cli/cmd/network_user/update.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package network_user
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/gravitl/netmaker/cli/functions"
|
||||||
|
"github.com/gravitl/netmaker/models/promodels"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var networkuserUpdateCmd = &cobra.Command{
|
||||||
|
Use: "update [NETWORK NAME]",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Short: "Update a network user",
|
||||||
|
Long: `Update a network user`,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
user := &promodels.NetworkUser{
|
||||||
|
AccessLevel: accessLevel,
|
||||||
|
ClientLimit: clientLimit,
|
||||||
|
NodeLimit: nodeLimit, ID: promodels.NetworkUserID(id),
|
||||||
|
}
|
||||||
|
if clients != "" {
|
||||||
|
user.Clients = strings.Split(clients, ",")
|
||||||
|
}
|
||||||
|
if nodes != "" {
|
||||||
|
user.Nodes = strings.Split(nodes, ",")
|
||||||
|
}
|
||||||
|
functions.UpdateNetworkUser(args[0], user)
|
||||||
|
fmt.Println("Success")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
networkuserUpdateCmd.Flags().IntVar(&accessLevel, "access_level", 0, "Custom access level")
|
||||||
|
networkuserUpdateCmd.Flags().IntVar(&clientLimit, "client_limit", 0, "Maximum number of external clients that can be created")
|
||||||
|
networkuserUpdateCmd.Flags().IntVar(&nodeLimit, "node_limit", 999999999, "Maximum number of nodes that can be attached to a network")
|
||||||
|
networkuserUpdateCmd.Flags().StringVar(&clients, "clients", "", "Access to list of external clients (comma separated)")
|
||||||
|
networkuserUpdateCmd.Flags().StringVar(&nodes, "nodes", "", "Access to list of nodes (comma separated)")
|
||||||
|
networkuserUpdateCmd.Flags().StringVar(&id, "id", "", "ID of the network user")
|
||||||
|
networkuserUpdateCmd.MarkFlagRequired("id")
|
||||||
|
rootCmd.AddCommand(networkuserUpdateCmd)
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/gravitl/netmaker/cli/cmd/keys"
|
"github.com/gravitl/netmaker/cli/cmd/keys"
|
||||||
"github.com/gravitl/netmaker/cli/cmd/metrics"
|
"github.com/gravitl/netmaker/cli/cmd/metrics"
|
||||||
"github.com/gravitl/netmaker/cli/cmd/network"
|
"github.com/gravitl/netmaker/cli/cmd/network"
|
||||||
|
"github.com/gravitl/netmaker/cli/cmd/network_user"
|
||||||
"github.com/gravitl/netmaker/cli/cmd/node"
|
"github.com/gravitl/netmaker/cli/cmd/node"
|
||||||
"github.com/gravitl/netmaker/cli/cmd/server"
|
"github.com/gravitl/netmaker/cli/cmd/server"
|
||||||
"github.com/gravitl/netmaker/cli/cmd/user"
|
"github.com/gravitl/netmaker/cli/cmd/user"
|
||||||
|
@ -27,6 +28,7 @@ var rootCmd = &cobra.Command{
|
||||||
// Run: func(cmd *cobra.Command, args []string) { },
|
// Run: func(cmd *cobra.Command, args []string) { },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetRoot returns the root of all subcommands
|
||||||
func GetRoot() *cobra.Command {
|
func GetRoot() *cobra.Command {
|
||||||
return rootCmd
|
return rootCmd
|
||||||
}
|
}
|
||||||
|
@ -63,4 +65,5 @@ func init() {
|
||||||
rootCmd.AddCommand(user.GetRoot())
|
rootCmd.AddCommand(user.GetRoot())
|
||||||
rootCmd.AddCommand(usergroup.GetRoot())
|
rootCmd.AddCommand(usergroup.GetRoot())
|
||||||
rootCmd.AddCommand(metrics.GetRoot())
|
rootCmd.AddCommand(metrics.GetRoot())
|
||||||
|
rootCmd.AddCommand(network_user.GetRoot())
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,14 +12,17 @@ func GetNodeMetrics(networkName, nodeID string) *models.Metrics {
|
||||||
return request[models.Metrics](http.MethodGet, fmt.Sprintf("/api/metrics/%s/%s", networkName, nodeID), nil)
|
return request[models.Metrics](http.MethodGet, fmt.Sprintf("/api/metrics/%s/%s", networkName, nodeID), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNetworkNodeMetrics - fetch an entire network's metrics
|
||||||
func GetNetworkNodeMetrics(networkName string) *models.NetworkMetrics {
|
func GetNetworkNodeMetrics(networkName string) *models.NetworkMetrics {
|
||||||
return request[models.NetworkMetrics](http.MethodGet, "/api/metrics/"+networkName, nil)
|
return request[models.NetworkMetrics](http.MethodGet, "/api/metrics/"+networkName, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAllMetrics - fetch all metrics
|
||||||
func GetAllMetrics() *models.NetworkMetrics {
|
func GetAllMetrics() *models.NetworkMetrics {
|
||||||
return request[models.NetworkMetrics](http.MethodGet, "/api/metrics", nil)
|
return request[models.NetworkMetrics](http.MethodGet, "/api/metrics", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNetworkExtMetrics - fetch external client metrics belonging to a network
|
||||||
func GetNetworkExtMetrics(networkName string) *map[string]models.Metric {
|
func GetNetworkExtMetrics(networkName string) *map[string]models.Metric {
|
||||||
return request[map[string]models.Metric](http.MethodGet, "/api/metrics-ext/"+networkName, nil)
|
return request[map[string]models.Metric](http.MethodGet, "/api/metrics-ext/"+networkName, nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,30 +8,37 @@ import (
|
||||||
"github.com/gravitl/netmaker/models/promodels"
|
"github.com/gravitl/netmaker/models/promodels"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetAllNetworkUsers - fetch all network users
|
||||||
func GetAllNetworkUsers() *map[string][]promodels.NetworkUser {
|
func GetAllNetworkUsers() *map[string][]promodels.NetworkUser {
|
||||||
return request[map[string][]promodels.NetworkUser](http.MethodGet, "/api/networkusers", nil)
|
return request[map[string][]promodels.NetworkUser](http.MethodGet, "/api/networkusers", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNetworkUsers - fetch network users belonging to a particular network
|
||||||
func GetNetworkUsers(networkName string) *promodels.NetworkUserMap {
|
func GetNetworkUsers(networkName string) *promodels.NetworkUserMap {
|
||||||
return request[promodels.NetworkUserMap](http.MethodGet, "/api/networkusers/"+networkName, nil)
|
return request[promodels.NetworkUserMap](http.MethodGet, "/api/networkusers/"+networkName, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNetworkUser - fetch a single network user
|
||||||
func GetNetworkUser(networkName, networkUserName string) *promodels.NetworkUser {
|
func GetNetworkUser(networkName, networkUserName string) *promodels.NetworkUser {
|
||||||
return request[promodels.NetworkUser](http.MethodGet, fmt.Sprintf("/api/networkusers/%s/%s", networkName, networkUserName), nil)
|
return request[promodels.NetworkUser](http.MethodGet, fmt.Sprintf("/api/networkusers/%s/%s", networkName, networkUserName), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateNetworkUser - create a network user
|
||||||
func CreateNetworkUser(networkName string, payload *promodels.NetworkUser) {
|
func CreateNetworkUser(networkName string, payload *promodels.NetworkUser) {
|
||||||
request[any](http.MethodPost, "/api/networkusers/"+networkName, payload)
|
request[any](http.MethodPost, "/api/networkusers/"+networkName, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateNetworkUser - update a network user
|
||||||
func UpdateNetworkUser(networkName string, payload *promodels.NetworkUser) {
|
func UpdateNetworkUser(networkName string, payload *promodels.NetworkUser) {
|
||||||
request[any](http.MethodPut, "/api/networkusers/"+networkName, payload)
|
request[any](http.MethodPut, "/api/networkusers/"+networkName, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNetworkUserData - fetch a network user's complete data
|
||||||
func GetNetworkUserData(networkUserName string) *ee_controllers.NetworkUserDataMap {
|
func GetNetworkUserData(networkUserName string) *ee_controllers.NetworkUserDataMap {
|
||||||
return request[ee_controllers.NetworkUserDataMap](http.MethodGet, fmt.Sprintf("/api/networkusers/data/%s/me", networkUserName), nil)
|
return request[ee_controllers.NetworkUserDataMap](http.MethodGet, fmt.Sprintf("/api/networkusers/data/%s/me", networkUserName), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteNetworkUser - delete a network user
|
||||||
func DeleteNetworkUser(networkName, networkUserName string) {
|
func DeleteNetworkUser(networkName, networkUserName string) {
|
||||||
request[any](http.MethodDelete, fmt.Sprintf("/api/networkusers/%s/%s", networkName, networkUserName), nil)
|
request[any](http.MethodDelete, fmt.Sprintf("/api/networkusers/%s/%s", networkName, networkUserName), nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,17 @@ import (
|
||||||
"github.com/gravitl/netmaker/models/promodels"
|
"github.com/gravitl/netmaker/models/promodels"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// GetUsergroups - fetch all usergroups
|
||||||
func GetUsergroups() *promodels.UserGroups {
|
func GetUsergroups() *promodels.UserGroups {
|
||||||
return request[promodels.UserGroups](http.MethodGet, "/api/usergroups", nil)
|
return request[promodels.UserGroups](http.MethodGet, "/api/usergroups", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreateUsergroup - create a usergroup
|
||||||
func CreateUsergroup(usergroupName string) {
|
func CreateUsergroup(usergroupName string) {
|
||||||
request[any](http.MethodPost, "/api/usergroups/"+usergroupName, nil)
|
request[any](http.MethodPost, "/api/usergroups/"+usergroupName, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteUsergroup - delete a usergroup
|
||||||
func DeleteUsergroup(usergroupName string) {
|
func DeleteUsergroup(usergroupName string) {
|
||||||
request[any](http.MethodDelete, "/api/usergroups/"+usergroupName, nil)
|
request[any](http.MethodDelete, "/api/usergroups/"+usergroupName, nil)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue