roles.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package user
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "github.com/gravitl/netmaker/cli/cmd/commons"
  7. "github.com/gravitl/netmaker/cli/functions"
  8. "github.com/guumaster/tablewriter"
  9. "github.com/spf13/cobra"
  10. )
  11. var userRoleCmd = &cobra.Command{
  12. Use: "role",
  13. Args: cobra.NoArgs,
  14. Short: "Manage User Roles",
  15. Long: `Manage User Roles`,
  16. }
  17. // List Roles
  18. var (
  19. platformRoles bool
  20. )
  21. var userRoleListCmd = &cobra.Command{
  22. Use: "list",
  23. Args: cobra.NoArgs,
  24. Short: "List all user roles",
  25. Long: `List all user roles`,
  26. Run: func(cmd *cobra.Command, args []string) {
  27. data := functions.ListUserRoles()
  28. switch commons.OutputFormat {
  29. case commons.JsonOutput:
  30. functions.PrettyPrint(data)
  31. default:
  32. table := tablewriter.NewWriter(os.Stdout)
  33. h := []string{"ID", "Default", "Dashboard Access", "Full Access"}
  34. if !platformRoles {
  35. h = append(h, "Network")
  36. }
  37. table.SetHeader(h)
  38. for _, d := range data {
  39. e := []string{d.ID.String(), strconv.FormatBool(d.Default), strconv.FormatBool(d.DenyDashboardAccess), strconv.FormatBool(d.FullAccess)}
  40. if !platformRoles {
  41. e = append(e, d.NetworkID.String())
  42. }
  43. table.Append(e)
  44. }
  45. table.Render()
  46. }
  47. },
  48. }
  49. var userRoleCreateCmd = &cobra.Command{
  50. Use: "create",
  51. Args: cobra.NoArgs,
  52. Short: "create user role",
  53. Long: `create user role`,
  54. Run: func(cmd *cobra.Command, args []string) {
  55. fmt.Println("CLI doesn't support creation of roles currently. Visit the dashboard to create one or refer to our api documentation https://docs.v2.netmaker.io/reference")
  56. },
  57. }
  58. var userRoleDeleteCmd = &cobra.Command{
  59. Use: "delete [roleID]",
  60. Args: cobra.ExactArgs(1),
  61. Short: "delete user role",
  62. Long: `delete user role`,
  63. Run: func(cmd *cobra.Command, args []string) {
  64. resp := functions.DeleteUserRole(args[0])
  65. if resp != nil {
  66. fmt.Println(resp.Message)
  67. }
  68. },
  69. }
  70. var userRoleGetCmd = &cobra.Command{
  71. Use: "get [roleID]",
  72. Args: cobra.ExactArgs(1),
  73. Short: "get user role",
  74. Long: `get user role`,
  75. Run: func(cmd *cobra.Command, args []string) {
  76. d := functions.GetUserRole(args[0])
  77. switch commons.OutputFormat {
  78. case commons.JsonOutput:
  79. functions.PrettyPrint(d)
  80. default:
  81. table := tablewriter.NewWriter(os.Stdout)
  82. h := []string{"ID", "Default Role", "Dashboard Access", "Full Access"}
  83. if d.NetworkID != "" {
  84. h = append(h, "Network")
  85. }
  86. table.SetHeader(h)
  87. e := []string{d.ID.String(), strconv.FormatBool(d.Default), strconv.FormatBool(!d.DenyDashboardAccess), strconv.FormatBool(d.FullAccess)}
  88. if !platformRoles {
  89. e = append(e, d.NetworkID.String())
  90. }
  91. table.Append(e)
  92. table.Render()
  93. }
  94. },
  95. }
  96. func init() {
  97. rootCmd.AddCommand(userRoleCmd)
  98. // list roles cmd
  99. userRoleListCmd.Flags().BoolVar(&platformRoles, "platform-roles", true,
  100. "set to false to list network roles. By default it will only list platform roles")
  101. userRoleCmd.AddCommand(userRoleListCmd)
  102. // create roles cmd
  103. userRoleCmd.AddCommand(userRoleCreateCmd)
  104. // delete role cmd
  105. userRoleCmd.AddCommand(userRoleDeleteCmd)
  106. // Get Role
  107. userRoleCmd.AddCommand(userRoleGetCmd)
  108. }