api_datasources.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package api
  2. import (
  3. "github.com/torkelo/grafana-pro/pkg/api/dtos"
  4. "github.com/torkelo/grafana-pro/pkg/bus"
  5. "github.com/torkelo/grafana-pro/pkg/middleware"
  6. m "github.com/torkelo/grafana-pro/pkg/models"
  7. )
  8. func GetDataSources(c *middleware.Context) {
  9. query := m.GetDataSourcesQuery{AccountId: c.Account.Id}
  10. err := bus.Dispatch(&query)
  11. if err != nil {
  12. c.JsonApiErr(500, "Failed to query datasources", err)
  13. return
  14. }
  15. result := make([]*dtos.DataSource, len(query.Resp))
  16. for _, ds := range query.Resp {
  17. result = append(result, &dtos.DataSource{
  18. Id: ds.Id,
  19. AccountId: ds.AccountId,
  20. Name: ds.Name,
  21. Url: ds.Url,
  22. Type: ds.Type,
  23. Access: ds.Access,
  24. Password: ds.Password,
  25. User: ds.User,
  26. BasicAuth: ds.BasicAuth,
  27. })
  28. }
  29. c.JSON(200, result)
  30. }
  31. func AddDataSource(c *middleware.Context) {
  32. cmd := m.AddDataSourceCommand{}
  33. if !c.JsonBody(&cmd) {
  34. c.JsonApiErr(400, "Validation failed", nil)
  35. return
  36. }
  37. cmd.AccountId = c.Account.Id
  38. err := bus.Dispatch(&cmd)
  39. if err != nil {
  40. c.JsonApiErr(500, "Failed to add datasource", err)
  41. return
  42. }
  43. c.JsonOK("Datasource added")
  44. }