diff --git a/cli/cmd/ext_client/update.go b/cli/cmd/ext_client/update.go new file mode 100644 index 00000000..a410f7ec --- /dev/null +++ b/cli/cmd/ext_client/update.go @@ -0,0 +1,33 @@ +package ext_client + +import ( + "encoding/json" + "log" + "os" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/gravitl/netmaker/models" + "github.com/spf13/cobra" +) + +var extClientUpdateCmd = &cobra.Command{ + Use: "update [NETWORK NAME] [NODE ID] [/path/to/ext_client_definition.json]", + Args: cobra.ExactArgs(3), + Short: "Update an External Client", + Long: `Update an External Client`, + Run: func(cmd *cobra.Command, args []string) { + extClient := &models.ExtClient{} + content, err := os.ReadFile(args[2]) + if err != nil { + log.Fatal("Error when opening file: ", err) + } + if err := json.Unmarshal(content, extClient); err != nil { + log.Fatal(err) + } + functions.PrettyPrint(functions.UpdateExtClient(args[0], args[1], extClient)) + }, +} + +func init() { + rootCmd.AddCommand(extClientUpdateCmd) +} diff --git a/cli/cmd/root.go b/cli/cmd/root.go index ec4ba953..6760713a 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -11,6 +11,7 @@ import ( "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/spf13/cobra" ) @@ -57,4 +58,5 @@ func init() { rootCmd.AddCommand(dns.GetRoot()) rootCmd.AddCommand(server.GetRoot()) rootCmd.AddCommand(ext_client.GetRoot()) + rootCmd.AddCommand(user.GetRoot()) } diff --git a/cli/cmd/server/has_admin.go b/cli/cmd/server/has_admin.go new file mode 100644 index 00000000..f9a27250 --- /dev/null +++ b/cli/cmd/server/has_admin.go @@ -0,0 +1,22 @@ +package server + +import ( + "fmt" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var serverHasAdminCmd = &cobra.Command{ + Use: "has_admin", + Args: cobra.NoArgs, + Short: "Check if server has an admin", + Long: `Check if server has an admin`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(*functions.HasAdmin()) + }, +} + +func init() { + rootCmd.AddCommand(serverHasAdminCmd) +} diff --git a/cli/cmd/user/create.go b/cli/cmd/user/create.go new file mode 100644 index 00000000..48cf6ba9 --- /dev/null +++ b/cli/cmd/user/create.go @@ -0,0 +1,45 @@ +package user + +import ( + "strings" + + "github.com/gravitl/netmaker/cli/functions" + "github.com/gravitl/netmaker/models" + "github.com/spf13/cobra" +) + +var ( + username string + password string + admin bool + networks string + groups string +) + +var userCreateCmd = &cobra.Command{ + Use: "create", + Args: cobra.NoArgs, + Short: "Create a new user", + Long: `Create a new user`, + Run: func(cmd *cobra.Command, args []string) { + user := &models.User{UserName: username, Password: password, IsAdmin: admin} + if networks != "" { + user.Networks = strings.Split(networks, ",") + } + if groups != "" { + user.Groups = strings.Split(groups, ",") + } + functions.PrettyPrint(functions.CreateUser(user)) + }, +} + +func init() { + userCreateCmd.Flags().StringVar(&username, "name", "", "Name of the user") + userCreateCmd.Flags().StringVar(&password, "password", "", "Password of the user") + userCreateCmd.MarkFlagRequired("name") + userCreateCmd.MarkFlagRequired("password") + userCreateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ?") + userCreateCmd.Flags().StringVar(&networks, "networks", "", "List of networks the user will access to (comma separated)") + userCreateCmd.Flags().StringVar(&groups, "groups", "", "List of user groups the user will be part of (comma separated)") + rootCmd.AddCommand(userCreateCmd) +} diff --git a/cli/cmd/user/list.go b/cli/cmd/user/list.go new file mode 100644 index 00000000..da46bcea --- /dev/null +++ b/cli/cmd/user/list.go @@ -0,0 +1,20 @@ +package user + +import ( + "github.com/gravitl/netmaker/cli/functions" + "github.com/spf13/cobra" +) + +var userListCmd = &cobra.Command{ + Use: "list", + Args: cobra.NoArgs, + Short: "List all users", + Long: `List all users`, + Run: func(cmd *cobra.Command, args []string) { + functions.PrettyPrint(functions.ListUsers()) + }, +} + +func init() { + rootCmd.AddCommand(userListCmd) +} diff --git a/cli/cmd/user/root.go b/cli/cmd/user/root.go new file mode 100644 index 00000000..1e86ed85 --- /dev/null +++ b/cli/cmd/user/root.go @@ -0,0 +1,37 @@ +package user + +import ( + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "user", + Short: "Manage users and permissions", + Long: `Manage users and permissions`, + // 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/functions/ext_client.go b/cli/functions/ext_client.go index 9540bdd5..c7b30a4d 100644 --- a/cli/functions/ext_client.go +++ b/cli/functions/ext_client.go @@ -36,3 +36,7 @@ func CreateExtClient(networkName, nodeID, extClientID string) { func DeleteExtClient(networkName, clientID string) *models.SuccessResponse { return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), nil) } + +func UpdateExtClient(networkName, clientID string, payload *models.ExtClient) *models.ExtClient { + return request[models.ExtClient](http.MethodPut, fmt.Sprintf("/api/extclients/%s/%s", networkName, clientID), payload) +} diff --git a/cli/functions/user.go b/cli/functions/user.go new file mode 100644 index 00000000..44f0e8de --- /dev/null +++ b/cli/functions/user.go @@ -0,0 +1,19 @@ +package functions + +import ( + "net/http" + + "github.com/gravitl/netmaker/models" +) + +func HasAdmin() *bool { + return request[bool](http.MethodGet, "/api/users/adm/hasadmin", nil) +} + +func CreateUser(payload *models.User) *models.User { + return request[models.User](http.MethodPost, "/api/users/"+payload.UserName, payload) +} + +func ListUsers() *[]models.ReturnUser { + return request[[]models.ReturnUser](http.MethodGet, "/api/users", nil) +}