fix role and groups command

This commit is contained in:
abhishek9686
2024-08-24 18:12:19 +05:30
parent a5c66f1b87
commit ddb8f6fa06
6 changed files with 64 additions and 24 deletions

View File

@@ -46,7 +46,7 @@ func init() {
"Platform Role of the user; run `nmctl roles list` to see available user roles")
userCreateCmd.MarkFlagRequired("name")
userCreateCmd.MarkFlagRequired("password")
userCreateCmd.PersistentFlags().StringToStringVarP(&networkRoles, "network-roles", "n", make(map[string]string),
userCreateCmd.PersistentFlags().StringToStringVarP(&networkRoles, "network-roles", "n", nil,
"Mapping of networkID and list of roles user will be part of (comma separated)")
userCreateCmd.Flags().BoolVar(&admin, "admin", false, "Make the user an admin ? (deprecated v0.25.0 onwards)")
userCreateCmd.Flags().StringArrayVarP(&groups, "groups", "g", nil, "List of user groups the user will be part of (comma separated)")

View File

@@ -7,7 +7,6 @@ import (
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
"github.com/spf13/cobra"
)
@@ -30,7 +29,6 @@ var userGroupListCmd = &cobra.Command{
Long: `List all user groups`,
Run: func(cmd *cobra.Command, args []string) {
data := functions.ListUserGrps()
userGrps := data.Response.([]models.UserGroup)
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
@@ -38,7 +36,7 @@ var userGroupListCmd = &cobra.Command{
table := tablewriter.NewWriter(os.Stdout)
h := []string{"ID", "MetaData", "Network Roles"}
table.SetHeader(h)
for _, d := range userGrps {
for _, d := range data {
roleInfoStr := ""
for netID, netRoleMap := range d.NetworkRoles {
@@ -85,9 +83,25 @@ var userGroupGetCmd = &cobra.Command{
Short: "get user group",
Long: `get user group`,
Run: func(cmd *cobra.Command, args []string) {
resp := functions.GetUserGrp(groupID)
if resp != nil {
fmt.Println(resp.Message)
data := functions.GetUserGrp(groupID)
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
default:
table := tablewriter.NewWriter(os.Stdout)
h := []string{"ID", "MetaData", "Network Roles"}
table.SetHeader(h)
roleInfoStr := ""
for netID, netRoleMap := range data.NetworkRoles {
roleList := []string{}
for roleID := range netRoleMap {
roleList = append(roleList, roleID.String())
}
roleInfoStr += fmt.Sprintf("[%s]: %s", netID, strings.Join(roleList, ","))
}
e := []string{data.ID.String(), data.MetaData, roleInfoStr}
table.Append(e)
table.Render()
}
},
}

View File

@@ -2,7 +2,6 @@ package user
import (
"os"
"strconv"
"strings"
"github.com/gravitl/netmaker/cli/cmd/commons"
@@ -29,7 +28,7 @@ var userListCmd = &cobra.Command{
for gID := range d.UserGroups {
g = append(g, gID.String())
}
table.Append([]string{d.UserName, d.PlatformRoleID.String(), strconv.FormatBool(d.IsAdmin), strings.Join(g, ",")})
table.Append([]string{d.UserName, d.PlatformRoleID.String(), strings.Join(g, ",")})
}
table.Render()
}

View File

@@ -7,7 +7,6 @@ import (
"github.com/gravitl/netmaker/cli/cmd/commons"
"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/models"
"github.com/guumaster/tablewriter"
"github.com/spf13/cobra"
)
@@ -31,7 +30,6 @@ var userRoleListCmd = &cobra.Command{
Long: `List all user roles`,
Run: func(cmd *cobra.Command, args []string) {
data := functions.ListUserRoles()
userRoles := data.Response.([]models.UserRolePermissionTemplate)
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(data)
@@ -43,7 +41,7 @@ var userRoleListCmd = &cobra.Command{
h = append(h, "Network")
}
table.SetHeader(h)
for _, d := range userRoles {
for _, d := range data {
e := []string{d.ID.String(), strconv.FormatBool(d.Default), strconv.FormatBool(d.DenyDashboardAccess), strconv.FormatBool(d.FullAccess)}
if !platformRoles {
e = append(e, d.NetworkID.String())
@@ -84,9 +82,24 @@ var userRoleGetCmd = &cobra.Command{
Short: "get user role",
Long: `get user role`,
Run: func(cmd *cobra.Command, args []string) {
resp := functions.GetUserRole(roleID)
if resp != nil {
fmt.Println(resp.Message)
d := functions.GetUserRole(roleID)
switch commons.OutputFormat {
case commons.JsonOutput:
functions.PrettyPrint(d)
default:
table := tablewriter.NewWriter(os.Stdout)
h := []string{"ID", "Default Role", "Dashboard Access", "Full Access"}
if d.NetworkID != "" {
h = append(h, "Network")
}
table.SetHeader(h)
e := []string{d.ID.String(), strconv.FormatBool(d.Default), strconv.FormatBool(!d.DenyDashboardAccess), strconv.FormatBool(d.FullAccess)}
if !platformRoles {
e = append(e, d.NetworkID.String())
}
table.Append(e)
table.Render()
}
},
}

View File

@@ -86,7 +86,7 @@ func GetCurrentContext() (name string, ctx Context) {
return
}
}
log.Fatalf("No current context set, do so via `netmaker context use <name>`")
log.Fatalf("No current context set, do so via `nmctl context use <name>`")
return
}

View File

@@ -1,6 +1,7 @@
package functions
import (
"encoding/json"
"fmt"
"net/http"
@@ -37,24 +38,37 @@ func ListUsers() *[]models.ReturnUser {
return request[[]models.ReturnUser](http.MethodGet, "/api/users", nil)
}
func ListUserRoles() *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodGet, "/api/v1/users/roles", nil)
func ListUserRoles() (roles []models.UserRolePermissionTemplate) {
resp := request[models.SuccessResponse](http.MethodGet, "/api/v1/users/roles", nil)
d, _ := json.Marshal(resp.Response)
json.Unmarshal(d, &roles)
return
}
func DeleteUserRole(roleID string) *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/v1/users/role?role_id=%s", roleID), nil)
}
func GetUserRole(roleID string) *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodGet, fmt.Sprintf("/api/v1/users/role?role_id=%s", roleID), nil)
func GetUserRole(roleID string) (role models.UserRolePermissionTemplate) {
resp := request[models.SuccessResponse](http.MethodGet, fmt.Sprintf("/api/v1/users/role?role_id=%s", roleID), nil)
d, _ := json.Marshal(resp.Response)
json.Unmarshal(d, &role)
return
}
func ListUserGrps() *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodGet, "/api/v1/users/groups", nil)
func ListUserGrps() (groups []models.UserGroup) {
resp := request[models.SuccessResponse](http.MethodGet, "/api/v1/users/groups", nil)
d, _ := json.Marshal(resp.Response)
json.Unmarshal(d, &groups)
return
}
func DeleteUserGrp(grpID string) *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodDelete, fmt.Sprintf("/api/v1/users/group?group_id=%s", grpID), nil)
}
func GetUserGrp(grpID string) *models.SuccessResponse {
return request[models.SuccessResponse](http.MethodGet, fmt.Sprintf("/api/v1/users/group?group_id=%s", grpID), nil)
func GetUserGrp(grpID string) (group models.UserGroup) {
resp := request[models.SuccessResponse](http.MethodGet, fmt.Sprintf("/api/v1/users/group?group_id=%s", grpID), nil)
d, _ := json.Marshal(resp.Response)
json.Unmarshal(d, &group)
return
}