add user subcommand

This commit is contained in:
Anish Mukherjee 2022-11-28 16:42:36 +05:30
parent db014c93bd
commit da2d143e39
6 changed files with 96 additions and 8 deletions

View file

@ -8,14 +8,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
username string
password string
admin bool
networks string
groups string
)
var userCreateCmd = &cobra.Command{ var userCreateCmd = &cobra.Command{
Use: "create", Use: "create",
Args: cobra.NoArgs, Args: cobra.NoArgs,

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

@ -0,0 +1,20 @@
package user
import (
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var userDeleteCmd = &cobra.Command{
Use: "delete [USER NAME]",
Args: cobra.ExactArgs(1),
Short: "Delete a user",
Long: `Delete a user`,
Run: func(cmd *cobra.Command, args []string) {
functions.PrettyPrint(*functions.DeleteUser(args[0]))
},
}
func init() {
rootCmd.AddCommand(userDeleteCmd)
}

9
cli/cmd/user/flags.go Normal file
View file

@ -0,0 +1,9 @@
package user
var (
username string
password string
admin bool
networks string
groups string
)

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

@ -0,0 +1,20 @@
package user
import (
"github.com/gravitl/netmaker/cli/functions"
"github.com/spf13/cobra"
)
var userGetCmd = &cobra.Command{
Use: "get [USER NAME]",
Args: cobra.ExactArgs(1),
Short: "Get a user",
Long: `Get a user`,
Run: func(cmd *cobra.Command, args []string) {
functions.PrettyPrint(functions.GetUser(args[0]))
},
}
func init() {
rootCmd.AddCommand(userGetCmd)
}

35
cli/cmd/user/update.go Normal file
View file

@ -0,0 +1,35 @@
package user
import (
"strings"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/spf13/cobra"
)
var userUpdateCmd = &cobra.Command{
Use: "update [USER NAME]",
Args: cobra.ExactArgs(1),
Short: "Update a user",
Long: `Update a user`,
Run: func(cmd *cobra.Command, args []string) {
user := &models.User{UserName: args[0], IsAdmin: admin}
if networks != "" {
user.Networks = strings.Split(networks, ",")
}
if groups != "" {
user.Groups = strings.Split(groups, ",")
} else {
user.Groups = []string{"*"}
}
functions.PrettyPrint(functions.UpdateUser(user))
},
}
func init() {
userUpdateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ?")
userUpdateCmd.Flags().StringVar(&networks, "networks", "", "List of networks the user will access to (comma separated)")
userUpdateCmd.Flags().StringVar(&groups, "groups", "", "List of user groups the user will be part of (comma separated)")
rootCmd.AddCommand(userUpdateCmd)
}

View file

@ -14,6 +14,18 @@ func CreateUser(payload *models.User) *models.User {
return request[models.User](http.MethodPost, "/api/users/"+payload.UserName, payload) return request[models.User](http.MethodPost, "/api/users/"+payload.UserName, payload)
} }
func UpdateUser(payload *models.User) *models.User {
return request[models.User](http.MethodPut, "/api/users/networks/"+payload.UserName, payload)
}
func DeleteUser(username string) *string {
return request[string](http.MethodDelete, "/api/users/"+username, nil)
}
func GetUser(username string) *models.User {
return request[models.User](http.MethodGet, "/api/users/"+username, nil)
}
func ListUsers() *[]models.ReturnUser { func ListUsers() *[]models.ReturnUser {
return request[[]models.ReturnUser](http.MethodGet, "/api/users", nil) return request[[]models.ReturnUser](http.MethodGet, "/api/users", nil)
} }