add user create and list

This commit is contained in:
Anish Mukherjee 2022-11-27 11:47:30 +05:30
parent 7004c8dd3e
commit b58040b9b8
8 changed files with 182 additions and 0 deletions

View file

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

View file

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

View file

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

45
cli/cmd/user/create.go Normal file
View file

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

20
cli/cmd/user/list.go Normal file
View file

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

37
cli/cmd/user/root.go Normal file
View file

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

View file

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

19
cli/functions/user.go Normal file
View file

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