github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/cli/backup/team.go (about)

     1  package backup
     2  
     3  import (
     4  	"context"
     5  	"github.com/bep/simplecobra"
     6  	"github.com/esnet/gdg/cli/support"
     7  	"github.com/esnet/gdg/internal/config"
     8  	api "github.com/esnet/gdg/internal/service"
     9  	"github.com/grafana/grafana-openapi-client-go/models"
    10  	"github.com/jedib0t/go-pretty/v6/table"
    11  	"github.com/spf13/cobra"
    12  	"log/slog"
    13  )
    14  
    15  func parseTeamGlobalFlags(command *cobra.Command) []string {
    16  	teamName, _ := command.Flags().GetString("team")
    17  	return []string{teamName}
    18  }
    19  
    20  func getTeamPermission(permissionType models.PermissionType) string {
    21  	permission := "Member"
    22  	if permissionType == models.PermissionType(api.AdminUserPermission) {
    23  		permission = "Admin"
    24  	}
    25  	return permission
    26  }
    27  
    28  func newTeamsCommand() simplecobra.Commander {
    29  	description := "Manage teams"
    30  	return &support.SimpleCommand{
    31  		NameP: "teams",
    32  		Short: description,
    33  		Long:  description,
    34  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    35  			cmd.Aliases = []string{"team", "t"}
    36  			cmd.PersistentFlags().StringP("team", "t", "", "team ID")
    37  		},
    38  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    39  			return cd.CobraCommand.Help()
    40  		},
    41  		CommandsList: []simplecobra.Commander{
    42  			newTeamsListCmd(),
    43  			newTeamsDownloadCmd(),
    44  			newTeamsUploadCmd(),
    45  			newTeamsClearCmd(),
    46  		},
    47  	}
    48  
    49  }
    50  
    51  func newTeamsListCmd() simplecobra.Commander {
    52  	description := "list teams from grafana"
    53  	return &support.SimpleCommand{
    54  		NameP: "list",
    55  		Short: description,
    56  		Long:  description,
    57  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    58  			cmd.Aliases = []string{"l"}
    59  		},
    60  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    61  			slog.Info("Listing teams for context", "context", config.Config().GetGDGConfig().GetContext())
    62  			rootCmd.TableObj.AppendHeader(table.Row{"id", "name", "email", "orgID", "memberCount", "memberID", "member Permission"})
    63  			filter := api.NewTeamFilter(parseTeamGlobalFlags(cd.CobraCommand)...)
    64  			teams := rootCmd.GrafanaSvc().ListTeams(filter)
    65  			if len(teams) == 0 {
    66  				slog.Info("No teams found")
    67  			} else {
    68  				for team, members := range teams {
    69  					rootCmd.TableObj.AppendRow(table.Row{team.ID, team.Name, team.Email, team.OrgID, team.MemberCount})
    70  					if team.MemberCount > 0 {
    71  						for _, member := range members {
    72  							rootCmd.TableObj.AppendRow(table.Row{"", "", "", "", "", member.Login, getTeamPermission(member.Permission)})
    73  						}
    74  					}
    75  				}
    76  				rootCmd.Render(cd.CobraCommand, teams)
    77  			}
    78  			return nil
    79  		},
    80  	}
    81  }
    82  func newTeamsDownloadCmd() simplecobra.Commander {
    83  	description := "download teams from grafana"
    84  	return &support.SimpleCommand{
    85  		NameP: "download",
    86  		Short: description,
    87  		Long:  description,
    88  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    89  			cmd.Aliases = []string{"d"}
    90  		},
    91  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    92  			slog.Info("Importing Teams for context", "context", config.Config().GetGDGConfig().GetContext())
    93  			filter := api.NewTeamFilter(parseTeamGlobalFlags(cd.CobraCommand)...)
    94  			savedFiles := rootCmd.GrafanaSvc().DownloadTeams(filter)
    95  			if len(savedFiles) == 0 {
    96  				slog.Info("No teams found")
    97  			} else {
    98  				rootCmd.TableObj.AppendHeader(table.Row{"id", "name", "email", "orgID", "memberCount", "member user ID", "Member Permission"})
    99  				for team, members := range savedFiles {
   100  					rootCmd.TableObj.AppendRow(table.Row{team.ID, team.Name, team.Email, team.OrgID, team.MemberCount})
   101  					for _, member := range members {
   102  						rootCmd.TableObj.AppendRow(table.Row{"", "", "", "", "", member.Login, getTeamPermission(member.Permission)})
   103  					}
   104  				}
   105  				rootCmd.Render(cd.CobraCommand, savedFiles)
   106  			}
   107  			return nil
   108  		},
   109  	}
   110  
   111  }
   112  func newTeamsUploadCmd() simplecobra.Commander {
   113  	description := "upload teams to grafana"
   114  	return &support.SimpleCommand{
   115  		NameP: "upload",
   116  		Short: description,
   117  		Long:  description,
   118  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
   119  			cmd.Aliases = []string{"u"}
   120  		},
   121  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
   122  			slog.Info("Exporting Teams for context", "context", config.Config().GetGDGConfig().GetContext())
   123  			filter := api.NewTeamFilter(parseTeamGlobalFlags(cd.CobraCommand)...)
   124  			savedFiles := rootCmd.GrafanaSvc().UploadTeams(filter)
   125  			if len(savedFiles) == 0 {
   126  				slog.Info("No teams found")
   127  			} else {
   128  				rootCmd.TableObj.AppendHeader(table.Row{"id", "name", "email", "orgID", "created", "memberCount", "member Login", "member Permission"})
   129  				for team, members := range savedFiles {
   130  					rootCmd.TableObj.AppendRow(table.Row{team.ID, team.Name, team.Email, team.OrgID, team.MemberCount})
   131  					if team.MemberCount > 0 {
   132  						for _, member := range members {
   133  							rootCmd.TableObj.AppendRow(table.Row{"", "", "", "", "", member.Login, getTeamPermission(member.Permission)})
   134  						}
   135  					}
   136  				}
   137  				rootCmd.Render(cd.CobraCommand, savedFiles)
   138  			}
   139  			return nil
   140  		},
   141  	}
   142  }
   143  func newTeamsClearCmd() simplecobra.Commander {
   144  	description := "Delete All Team from grafana"
   145  	return &support.SimpleCommand{
   146  		NameP: "clear",
   147  		Short: description,
   148  		Long:  description,
   149  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
   150  			cmd.Aliases = []string{"c"}
   151  		},
   152  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
   153  			slog.Info("Deleting teams for context", "context", config.Config().GetGDGConfig().GetContext())
   154  			filter := api.NewTeamFilter(parseTeamGlobalFlags(cd.CobraCommand)...)
   155  			rootCmd.TableObj.AppendHeader(table.Row{"type", "team ID", "team Name"})
   156  			teams, err := rootCmd.GrafanaSvc().DeleteTeam(filter)
   157  			if err != nil {
   158  				slog.Error(err.Error())
   159  			} else {
   160  				for _, team := range teams {
   161  					rootCmd.TableObj.AppendRow(table.Row{"team", team.ID, team.Name})
   162  				}
   163  				rootCmd.Render(cd.CobraCommand, teams)
   164  			}
   165  			return nil
   166  		},
   167  	}
   168  }