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

     1  package backup
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/bep/simplecobra"
     7  	"github.com/esnet/gdg/cli/support"
     8  	"github.com/esnet/gdg/internal/config"
     9  	"github.com/esnet/gdg/internal/service"
    10  	"github.com/jedib0t/go-pretty/v6/table"
    11  	"log/slog"
    12  
    13  	"github.com/spf13/cobra"
    14  )
    15  
    16  func newConnectionsCommand() simplecobra.Commander {
    17  	description := "Manage connections (formerly Data Sources)"
    18  	return &support.SimpleCommand{
    19  		NameP: "connections",
    20  		Short: description,
    21  		Long:  description,
    22  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    23  			cmd.Aliases = []string{"connection", "ds", "c", "datasource", "datasources"}
    24  			connections := cmd
    25  			connections.PersistentFlags().StringP("connection", "", "", "filter by connection slug")
    26  		},
    27  		CommandsList: []simplecobra.Commander{
    28  			newClearConnectionsCmd(),
    29  			newUploadConnectionsCmd(),
    30  			newDownloadConnectionsCmd(),
    31  			newListConnectionsCmd(),
    32  			newConnectionsPermissionCmd(),
    33  		},
    34  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    35  			return cd.CobraCommand.Help()
    36  		},
    37  	}
    38  }
    39  
    40  func newClearConnectionsCmd() simplecobra.Commander {
    41  	description := "clear all connections for the given Organization"
    42  	return &support.SimpleCommand{
    43  		NameP: "clear",
    44  		Short: description,
    45  		Long:  description,
    46  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    47  			cmd.Aliases = []string{"c"}
    48  		},
    49  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    50  			slog.Info("Delete connections", slog.String("Organization", GetOrganizationName()))
    51  			dashboardFilter, _ := cd.CobraCommand.Flags().GetString("datasource")
    52  			filters := service.NewConnectionFilter(dashboardFilter)
    53  			savedFiles := rootCmd.GrafanaSvc().DeleteAllConnections(filters)
    54  			rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})
    55  			for _, file := range savedFiles {
    56  				rootCmd.TableObj.AppendRow(table.Row{"datasource", file})
    57  			}
    58  			rootCmd.Render(cd.CobraCommand, savedFiles)
    59  			return nil
    60  		},
    61  	}
    62  }
    63  
    64  func newUploadConnectionsCmd() simplecobra.Commander {
    65  	description := "upload all connections to grafana for the given Organization"
    66  	return &support.SimpleCommand{
    67  		NameP: "upload",
    68  		Short: description,
    69  		Long:  description,
    70  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    71  			cmd.Aliases = []string{"u"}
    72  		},
    73  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    74  			slog.Info("Uploading connections", slog.String("Organization", GetOrganizationName()))
    75  			dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
    76  			filters := service.NewConnectionFilter(dashboardFilter)
    77  			exportedList := rootCmd.GrafanaSvc().UploadConnections(filters)
    78  			rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})
    79  			for _, file := range exportedList {
    80  				rootCmd.TableObj.AppendRow(table.Row{"datasource", file})
    81  			}
    82  			rootCmd.Render(cd.CobraCommand, exportedList)
    83  			return nil
    84  		},
    85  	}
    86  }
    87  
    88  func newDownloadConnectionsCmd() simplecobra.Commander {
    89  	description := "download all connections from grafana for the given Organization"
    90  	return &support.SimpleCommand{
    91  		NameP: "download",
    92  		Short: description,
    93  		Long:  description,
    94  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
    95  			cmd.Aliases = []string{"d"}
    96  		},
    97  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
    98  			slog.Info("Importing connections for context",
    99  				slog.String("Organization", GetOrganizationName()),
   100  				"context", config.Config().GetGDGConfig().GetContext())
   101  			dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
   102  			filters := service.NewConnectionFilter(dashboardFilter)
   103  			savedFiles := rootCmd.GrafanaSvc().DownloadConnections(filters)
   104  			rootCmd.TableObj.AppendHeader(table.Row{"type", "filename"})
   105  			for _, file := range savedFiles {
   106  				rootCmd.TableObj.AppendRow(table.Row{"datasource", file})
   107  			}
   108  			rootCmd.Render(cd.CobraCommand, savedFiles)
   109  			return nil
   110  		},
   111  	}
   112  }
   113  func newListConnectionsCmd() simplecobra.Commander {
   114  	description := "List all connections for the given Organization"
   115  	return &support.SimpleCommand{
   116  		NameP: "list",
   117  		Short: description,
   118  		Long:  description,
   119  		WithCFunc: func(cmd *cobra.Command, r *support.RootCommand) {
   120  			cmd.Aliases = []string{"l"}
   121  		},
   122  		RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
   123  			rootCmd.TableObj.AppendHeader(table.Row{"id", "uid", "name", "slug", "type", "default", "url"})
   124  			dashboardFilter, _ := cd.CobraCommand.Flags().GetString("connection")
   125  			filters := service.NewConnectionFilter(dashboardFilter)
   126  			dsListing := rootCmd.GrafanaSvc().ListConnections(filters)
   127  			slog.Info("Listing connections for context",
   128  				slog.String("Organization", GetOrganizationName()),
   129  				slog.String("context", GetContext()))
   130  			if len(dsListing) == 0 {
   131  				slog.Info("No connections found")
   132  			} else {
   133  				for _, link := range dsListing {
   134  					url := fmt.Sprintf("%s/datasource/edit/%d", config.Config().GetDefaultGrafanaConfig().URL, link.ID)
   135  					rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, link.Name, service.GetSlug(link.Name), link.Type, link.IsDefault, url})
   136  				}
   137  				rootCmd.Render(cd.CobraCommand, dsListing)
   138  			}
   139  			return nil
   140  		},
   141  	}
   142  }