datasources.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package api
  2. import (
  3. "sort"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/plugins"
  7. //"github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/middleware"
  9. m "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/util"
  11. )
  12. func GetDataSources(c *middleware.Context) {
  13. query := m.GetDataSourcesQuery{OrgId: c.OrgId}
  14. if err := bus.Dispatch(&query); err != nil {
  15. c.JsonApiErr(500, "Failed to query datasources", err)
  16. return
  17. }
  18. result := make(dtos.DataSourceList, 0)
  19. for _, ds := range query.Result {
  20. dsItem := dtos.DataSource{
  21. Id: ds.Id,
  22. OrgId: ds.OrgId,
  23. Name: ds.Name,
  24. Url: ds.Url,
  25. Type: ds.Type,
  26. Access: ds.Access,
  27. Password: ds.Password,
  28. Database: ds.Database,
  29. User: ds.User,
  30. BasicAuth: ds.BasicAuth,
  31. IsDefault: ds.IsDefault,
  32. JsonData: ds.JsonData,
  33. }
  34. if plugin, exists := plugins.DataSources[ds.Type]; exists {
  35. dsItem.TypeLogoUrl = plugin.Info.Logos.Small
  36. } else {
  37. dsItem.TypeLogoUrl = "public/img/icn-datasource.svg"
  38. }
  39. result = append(result, dsItem)
  40. }
  41. sort.Sort(result)
  42. c.JSON(200, result)
  43. }
  44. func GetDataSourceById(c *middleware.Context) Response {
  45. query := m.GetDataSourceByIdQuery{
  46. Id: c.ParamsInt64(":id"),
  47. OrgId: c.OrgId,
  48. }
  49. if err := bus.Dispatch(&query); err != nil {
  50. if err == m.ErrDataSourceNotFound {
  51. return ApiError(404, "Data source not found", nil)
  52. }
  53. return ApiError(500, "Failed to query datasources", err)
  54. }
  55. ds := query.Result
  56. dtos := convertModelToDtos(ds)
  57. return Json(200, &dtos)
  58. }
  59. func DeleteDataSource(c *middleware.Context) {
  60. id := c.ParamsInt64(":id")
  61. if id <= 0 {
  62. c.JsonApiErr(400, "Missing valid datasource id", nil)
  63. return
  64. }
  65. cmd := &m.DeleteDataSourceCommand{Id: id, OrgId: c.OrgId}
  66. err := bus.Dispatch(cmd)
  67. if err != nil {
  68. c.JsonApiErr(500, "Failed to delete datasource", err)
  69. return
  70. }
  71. c.JsonOK("Data source deleted")
  72. }
  73. func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
  74. cmd.OrgId = c.OrgId
  75. if err := bus.Dispatch(&cmd); err != nil {
  76. if err == m.ErrDataSourceNameExists {
  77. c.JsonApiErr(409, err.Error(), err)
  78. return
  79. }
  80. c.JsonApiErr(500, "Failed to add datasource", err)
  81. return
  82. }
  83. c.JSON(200, util.DynMap{"message": "Datasource added", "id": cmd.Result.Id})
  84. }
  85. func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) Response {
  86. cmd.OrgId = c.OrgId
  87. cmd.Id = c.ParamsInt64(":id")
  88. err := fillWithSecureJsonData(&cmd)
  89. if err != nil {
  90. return ApiError(500, "Failed to update datasource", err)
  91. }
  92. err = bus.Dispatch(&cmd)
  93. if err != nil {
  94. return ApiError(500, "Failed to update datasource", err)
  95. }
  96. return Json(200, "Datasource updated")
  97. }
  98. func fillWithSecureJsonData(cmd *m.UpdateDataSourceCommand) error {
  99. if len(cmd.SecureJsonData) == 0 {
  100. return nil
  101. }
  102. ds, err := getRawDataSourceById(cmd.Id, cmd.OrgId)
  103. if err != nil {
  104. return err
  105. }
  106. secureJsonData := ds.SecureJsonData.Decrypt()
  107. for k, v := range secureJsonData {
  108. if _, ok := cmd.SecureJsonData[k]; !ok {
  109. cmd.SecureJsonData[k] = v
  110. }
  111. }
  112. return nil
  113. }
  114. func getRawDataSourceById(id int64, orgId int64) (*m.DataSource, error) {
  115. query := m.GetDataSourceByIdQuery{
  116. Id: id,
  117. OrgId: orgId,
  118. }
  119. if err := bus.Dispatch(&query); err != nil {
  120. return nil, err
  121. }
  122. return query.Result, nil
  123. }
  124. // Get /api/datasources/name/:name
  125. func GetDataSourceByName(c *middleware.Context) Response {
  126. query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
  127. if err := bus.Dispatch(&query); err != nil {
  128. if err == m.ErrDataSourceNotFound {
  129. return ApiError(404, "Data source not found", nil)
  130. }
  131. return ApiError(500, "Failed to query datasources", err)
  132. }
  133. dtos := convertModelToDtos(query.Result)
  134. return Json(200, &dtos)
  135. }
  136. // Get /api/datasources/id/:name
  137. func GetDataSourceIdByName(c *middleware.Context) Response {
  138. query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
  139. if err := bus.Dispatch(&query); err != nil {
  140. if err == m.ErrDataSourceNotFound {
  141. return ApiError(404, "Data source not found", nil)
  142. }
  143. return ApiError(500, "Failed to query datasources", err)
  144. }
  145. ds := query.Result
  146. dtos := dtos.AnyId{
  147. Id: ds.Id,
  148. }
  149. return Json(200, &dtos)
  150. }
  151. func convertModelToDtos(ds *m.DataSource) dtos.DataSource {
  152. dto := dtos.DataSource{
  153. Id: ds.Id,
  154. OrgId: ds.OrgId,
  155. Name: ds.Name,
  156. Url: ds.Url,
  157. Type: ds.Type,
  158. Access: ds.Access,
  159. Password: ds.Password,
  160. Database: ds.Database,
  161. User: ds.User,
  162. BasicAuth: ds.BasicAuth,
  163. BasicAuthUser: ds.BasicAuthUser,
  164. BasicAuthPassword: ds.BasicAuthPassword,
  165. WithCredentials: ds.WithCredentials,
  166. IsDefault: ds.IsDefault,
  167. JsonData: ds.JsonData,
  168. }
  169. if len(ds.SecureJsonData) > 0 {
  170. dto.TLSAuth.CACertSet = len(ds.SecureJsonData["tlsCACert"]) > 0
  171. dto.TLSAuth.ClientCertSet = len(ds.SecureJsonData["tlsClientCert"]) > 0
  172. dto.TLSAuth.ClientKeySet = len(ds.SecureJsonData["tlsClientKey"]) > 0
  173. }
  174. for k, v := range ds.SecureJsonData {
  175. if len(v) > 0 {
  176. dto.EncryptedFields = append(dto.EncryptedFields, k)
  177. }
  178. }
  179. return dto
  180. }