datasource.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/codegangsta/cli"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/log"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/sqlstore"
  9. "github.com/grafana/grafana/pkg/setting"
  10. "os"
  11. "text/tabwriter"
  12. )
  13. var (
  14. ListDataSources = cli.Command{
  15. Name: "datasource",
  16. Usage: "list datasources",
  17. Description: "Lists the datasources in the system",
  18. Action: listDatasources,
  19. }
  20. DescribeDataSource = cli.Command{
  21. Name: "datasource:info",
  22. Usage: "describe the details of a datasource",
  23. Description: "Describes the details of a datasource",
  24. Action: describeDataSource,
  25. }
  26. )
  27. func listDatasources(c *cli.Context) {
  28. setting.NewConfigContext()
  29. sqlstore.NewEngine()
  30. sqlstore.EnsureAdminUser()
  31. if !c.Args().Present() {
  32. log.ConsoleFatal("Account name arg is required")
  33. }
  34. name := c.Args().First()
  35. accountQuery := m.GetAccountByNameQuery{Name: name}
  36. if err := bus.Dispatch(&accountQuery); err != nil {
  37. log.ConsoleFatalf("Failed to find account: %s", err)
  38. }
  39. accountId := accountQuery.Result.Id
  40. query := m.GetDataSourcesQuery{AccountId: accountId}
  41. if err := bus.Dispatch(&query); err != nil {
  42. log.ConsoleFatalf("Failed to find datasources: %s", err)
  43. }
  44. w := tabwriter.NewWriter(os.Stdout, 8, 1, 4, ' ', 0)
  45. fmt.Fprintf(w, "ID\tNAME\tURL\tTYPE\tACCESS\tDEFAULT\n")
  46. for _, ds := range query.Result {
  47. fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%t\n", ds.Id, ds.Name, ds.Url, ds.Type,
  48. ds.Access, ds.IsDefault)
  49. }
  50. w.Flush()
  51. }
  52. func describeDataSource(c *cli.Context) {
  53. setting.NewConfigContext()
  54. sqlstore.NewEngine()
  55. sqlstore.EnsureAdminUser()
  56. if len(c.Args()) != 2 {
  57. log.ConsoleFatal("Account and datasource name args are required")
  58. }
  59. name := c.Args().First()
  60. ds := c.Args()[1]
  61. accountQuery := m.GetAccountByNameQuery{Name: name}
  62. if err := bus.Dispatch(&accountQuery); err != nil {
  63. log.ConsoleFatalf("Failed to find account: %s", err)
  64. }
  65. accountId := accountQuery.Result.Id
  66. query := m.GetDataSourceByNameQuery{AccountId: accountId, Name: ds}
  67. if err := bus.Dispatch(&query); err != nil {
  68. log.ConsoleFatalf("Failed to find accounts: %s", err)
  69. }
  70. datasource := query.Result
  71. w := tabwriter.NewWriter(os.Stdout, 8, 1, 4, ' ', 0)
  72. fmt.Fprintf(w, "NAME\t%s\n", datasource.Name)
  73. fmt.Fprintf(w, "URL\t%s\n", datasource.Url)
  74. fmt.Fprintf(w, "DEFAULT\t%t\n", datasource.IsDefault)
  75. fmt.Fprintf(w, "ACCESS\t%s\n", datasource.Access)
  76. fmt.Fprintf(w, "TYPE\t%s\n", datasource.Type)
  77. switch datasource.Type {
  78. case m.DS_INFLUXDB:
  79. fmt.Fprintf(w, "DATABASE\t%s\n", datasource.Database)
  80. fmt.Fprintf(w, "DB USER\t%s\n", datasource.User)
  81. fmt.Fprintf(w, "DB PASSWORD\t%s\n", datasource.Password)
  82. case m.DS_ES:
  83. fmt.Fprintf(w, "INDEX\t%s\n", datasource.Database)
  84. }
  85. w.Flush()
  86. }